程序需要一直循环直到输入键 "Q"/"q"
The program needs to keep looping till the key "Q"/"q" is typed
程序提取数字,我希望它一直循环直到用户键入键 "Q" / "q"。例如,当用户按下 "O" 键时,程序应该打印他们输入的数字的个位数字,对于用户输入的任何 3 位数字,依此类推。当我 运行 现在的代码时,没有输出但也没有错误。
import java.util.Scanner;
public class DigitExtractor {
public static void main(String[] args)
throws java.io.IOException{
char input;
input = (char) System.in.read();
Scanner s = new Scanner(System.in);
variables Num = new variables();
do {
System.out.print("Enter an integer: ");
String wholeNumber = s.nextLine();
Num.ones = wholeNumber.charAt(2);
Num.tens = wholeNumber.charAt(1);
Num.hundred = wholeNumber.charAt(0);
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.");
input = (char) System.in.read();
System.out.println("Enter your choice: " + input);
if(input == 'W' || input == 'w') {
System.out.println(Num.WholeNum);
}
else if(input == 'O' || input == 'o') {
System.out.println(Num.ones);
}
else if(input == 'T' || input == 't') {
System.out.println(Num.tens);
}
else if(input == 'H' || input == 'H') {
System.out.println(Num.hundred);
}
} while (input == 'q');
}
}
class variables {
char hundred;
char tens;
char ones;
char WholeNum;
}
阅读越来越混乱。要使用扫描仪读取整数,我选择了 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'));
}
}
程序提取数字,我希望它一直循环直到用户键入键 "Q" / "q"。例如,当用户按下 "O" 键时,程序应该打印他们输入的数字的个位数字,对于用户输入的任何 3 位数字,依此类推。当我 运行 现在的代码时,没有输出但也没有错误。
import java.util.Scanner;
public class DigitExtractor {
public static void main(String[] args)
throws java.io.IOException{
char input;
input = (char) System.in.read();
Scanner s = new Scanner(System.in);
variables Num = new variables();
do {
System.out.print("Enter an integer: ");
String wholeNumber = s.nextLine();
Num.ones = wholeNumber.charAt(2);
Num.tens = wholeNumber.charAt(1);
Num.hundred = wholeNumber.charAt(0);
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.");
input = (char) System.in.read();
System.out.println("Enter your choice: " + input);
if(input == 'W' || input == 'w') {
System.out.println(Num.WholeNum);
}
else if(input == 'O' || input == 'o') {
System.out.println(Num.ones);
}
else if(input == 'T' || input == 't') {
System.out.println(Num.tens);
}
else if(input == 'H' || input == 'H') {
System.out.println(Num.hundred);
}
} while (input == 'q');
}
}
class variables {
char hundred;
char tens;
char ones;
char WholeNum;
}
阅读越来越混乱。要使用扫描仪读取整数,我选择了 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'));
}
}