看到有人问如图的程序,就去写了几行代码,顺便复习一下条件语句和ASCII码
1 import java.util.Scanner; 2 public class test1 { 3 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.print("输入一个ASCII数值:"); 8 int ascii = sc.nextInt(); 9 sc.close();10 char ch = (char)ascii;11 //在‘a’~‘z’转成‘A’~‘Z’12 if(ch >= 'a' && ch <= 'z')13 {14 System.out.println((char)(ch-32));15 }16 //在‘A’~‘Z’转成‘a’~‘z’17 else if(ch >= 'A' && ch <= 'Z')18 {19 System.out.println((char)(ch+32));20 }21 //在‘0’~‘9’输出0~922 else if(ch >= '0' && ch <= '9')23 {24 System.out.println(ch);25 }26 //输出“+ - * /”27 else if(ch == '+' || ch== '-' || ch == '*' || ch == '/')28 {29 System.out.println("属于算术运算符");30 }31 //输出“非法字符”32 else33 {34 System.out.println("非法字符");35 }36 }37 }
作者:耑新新,发布于
转载请注明出处,欢迎邮件交流: