-2
我试图在do-while循环内插入一个do-while循环来检查输入是整数。我在我想插入do-while循环的地方添加了一条评论。除了那部分,一切都很好。JAVA-验证输入是整数
public static void main(String[] args){
int num1, num2;
char response;
Scanner in = new Scanner(System.in);
Scanner reader = new Scanner(System.in);
do {
System.out.print("Enter number: ");
num1 = in.nextInt();
response = (char) reader.nextLine().charAt(0);
//I want to check if the input is integer here
do{
System.out.println("Invalid input!\nPlease input integers only!");
} while (response != /*something*/);
for(num2=0; num2 < 11; num2++) {
System.out.println(num1 + "X" + num2 + "=" + (num1 * num2));
}
do {
System.out.println("\nDo you want to try again? [y/n]");
response = (char) reader.nextLine().charAt(0);
if (response != 'n'&& response != 'N' && response != 'Y' && response != 'y')
System.out.println("Input invalid!");
} while (response != 'n' && response != 'N' && response != 'y' && response != 'Y');
} while (response != 'N' && response != 'n');{
System.out.println("Thank you for using the table");}
}
欢迎来到Stack Overflow!请参考https://stackoverflow.com/help/how-to-ask,特别关注相关信息(已由@StephenC编辑)。 –
你可以粗体预先编码的btw。指向某一特定行的典型方法是在代码中发表评论,以显示您所谈论的位置。 – Carcigenicate
Integer.parseInt在这里可能会有帮助:) – Lemonov