1 回答
TA贡献1830条经验 获得超3个赞
阅读越来越混乱。要使用扫描仪读取整数,我选择了 nextInt。那有帮助。我采用了您不将事情分解成更小块的方法。并且(修订版)我只使用扫描仪进行阅读,甚至是供选择的字符。我还在你不得不按下选项之前给出了提示,这样你就会知道。
public static void main(String[] args)
throws java.io.IOException {
int hundred; //my compiler was fussy about having the extra class
int tens;
int ones;
char input = ' '; //initialize outside loop
Scanner s = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int wholeNumber = s.nextInt();
ones = wholeNumber % 10;
tens = (wholeNumber / 10) % 10;
hundred = (wholeNumber / 100) % 10;
System.out.println("show (W)hole number.");
System.out.println("show (O)nes place number.");
System.out.println("show (T)ens place number.");
System.out.println("show (H)undreds place number.");
System.out.println("(Q)uit");
System.out.print("Enter your choice: ");
input = s.next().trim().charAt(0); //using scanner only
//System.out.println("Enter your choice: " + input);
if (input == 'W' || input == 'w') {
System.out.println(wholeNumber);
} else if (input == 'O' || input == 'o') {
System.out.println(ones);
} else if (input == 'T' || input == 't') {
System.out.println(tens);
} else if (input == 'H' || input == 'H') {
System.out.println(hundred);
}
} while ((input != 'q') && (input != 'Q'));
}
}
添加回答
举报