java基礎。輸入一位十六進位制數,轉化成十進位制數。
package lo;
import java.util.Scanner;
public class Test { public static void main(String[] args){ //輸入一個十六進位制數字,顯示它所對應的十進位制數 for(int i = 1 ;i < 100; i++){ Scanner input = new Scanner(System.in); System.out.print("\n請輸入一個十六進位制數: "); String value = input.nextLine(); char ch = value.charAt(0); if(value.length() != 1){ System.out.println(" 你只能輸入一個字元!"); } else { if(Character.isDigit(ch)){ System.out.println(value + "的十進位制數是 " + ch); } else if(Character.isLetter(ch)){ if(ch >= 'A' && ch <= 'F'){ int answer = ch - 'A' + 10; System.out.println(value + "的十進位制數是 " + answer); } else System.out.println("Error:Please input the letter for A~F "); } else System.out.println("Error input! "); } } } }