Java小實驗之數據轉換
阿新 • • 發佈:2017-10-29
n) ann + - close pri oid image int() 數據
看到有人問如圖的程序,就去寫了幾行代碼,順便復習一下條件語句和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~9 22 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 else 33 { 34 System.out.println("非法字符"); 35 } 36 } 37 }
作者:耑新新,發布於 博客園
轉載請註明出處,歡迎郵件交流:[email protected]
Java小實驗之數據轉換