编写一个JAVA,输入一个字符,输出这个字符及其ASCII值,并输出其前一个字符和后一个字符及其长度
2个回答
展开全部
//编写一个JAVA,输入一个字符,输出这个字符及其ASCII值,并输出其前一个字符和后一个字符及其长度
public class baidu {
public static void main(String args[]) {
System.out.println("请输入一个字符");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
// char[] c=str.toCharArray();
int cc = str.charAt(0);
// char c=str.charAt(0);
int bb = cc - 1;
int dd = cc + 1;
System.out.println("字符是:" + (char) cc + "; ASCII值为" + cc);
System.out.println("前一个");
System.out.println("字符是:" + (char) bb + "; ASCII值为" + bb);
System.out.println("后一个");
System.out.println("字符是:" + (char) dd + "; ASCII值为" + dd);
}
}
展开全部
import java.util.Scanner;
public class test{
public static void main(String[] args) {
char str,nextStr,preStr;
String tempNext,tempPre,temp = "";
int countNext = 0,countPre = 0;
System.out.print("请输入一个字符:");
str = new Scanner(System.in).next().charAt(0);
nextStr = (char)((int)str+1);
preStr = (char)((int)str-1);
tempNext = ""+nextStr+"";
tempPre = ""+preStr+"";
for (int i = 0; i < tempNext.length(); i++) {
temp+=tempNext.substring(i,i+1);
countNext++;
}
temp = "";
for (int i = 0; i < tempPre.length(); i++) {
temp+=tempPre.substring(i,i+1);
countPre++;
}
System.out.println(" ");
System.out.println("字符:"+str+",对应的ASCII码为:"+((int)str));
System.out.println("上一个字符是:"+preStr+",有"+countNext+"个字符,下一个字符是:"+nextStr+",有"+countPre+"个字符。");
}
}
public class test{
public static void main(String[] args) {
char str,nextStr,preStr;
String tempNext,tempPre,temp = "";
int countNext = 0,countPre = 0;
System.out.print("请输入一个字符:");
str = new Scanner(System.in).next().charAt(0);
nextStr = (char)((int)str+1);
preStr = (char)((int)str-1);
tempNext = ""+nextStr+"";
tempPre = ""+preStr+"";
for (int i = 0; i < tempNext.length(); i++) {
temp+=tempNext.substring(i,i+1);
countNext++;
}
temp = "";
for (int i = 0; i < tempPre.length(); i++) {
temp+=tempPre.substring(i,i+1);
countPre++;
}
System.out.println(" ");
System.out.println("字符:"+str+",对应的ASCII码为:"+((int)str));
System.out.println("上一个字符是:"+preStr+",有"+countNext+"个字符,下一个字符是:"+nextStr+",有"+countPre+"个字符。");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询