基本上我想做的是:
Get the user to enter a 5 digit number. If all of the digits in the number are Odd, it will print out "All of the digits are odd" and if all of the digits are not Odd, then it will print out "Not all of the digits are odd", and then it will ask the user if they want to Enter a new Number by pressing Y or N and then it will ask them for another 5 digit number and repeat.
这是我的问题:
当用户没有输入 5 位数字时,例如输入一个字符串/字符,然后我想让它说“这不是一个数字,请重试”,我尝试使用 try 和 catch 来做到这一点,我可以做到,所以它说重试,但我不确定如何使代码在抛出错误后返回到开头。抱歉,我的java不好:-/ 当用户不输入 Y 或 N 并输入数字时,我也遇到此问题。
代码:
package veryoddnumber;
import java.util.*;
public class VeryOddNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a 5 digit number...");
boolean yesno;
do {
try {
int number = scan.nextInt();
int length = String.valueOf(number).length();
if (length == 5) {
String digits = String.valueOf(number);
char number1 = digits.charAt(0);
char number2 = digits.charAt(1);
char number3 = digits.charAt(2);
char number4 = digits.charAt(3);
char number5 = digits.charAt(4);
if (number1 % 2 != 0 && number2 % 2 != 0 && number3 % 2 != 0 && number4 % 2 != 0 && number5 % 2 != 0) {
System.out.println("All of the numbers are odd...");
} else {
System.out.println("Not all of the numbers are odd...");
}
System.out.println("Would you like to enter another number? (Y/N)");
} else {
System.out.println("You did not enter a 5 digit number! Try again...");
System.out.println("Enter a 5 digit number...");
}
} catch (Exception e) {
System.out.println("That was not a number! Try again...");
}
try {
char letter = scan.next().charAt(0);
if (letter == 'Y' || letter == 'y') {
yesno = true;
} else if (letter == 'N' || letter == 'n') {
break;
} else {
System.out.println("I will take that as a no!");
break;
}
System.out.println("Enter a 5 digit number...");
} catch (Exception e) {
System.out.println("You did not enter a letter...");
}
} while (yesno = true);
System.out.println("Goodbye!");
}
}
最佳答案
我刚刚重新安排了您的一些逻辑,并添加了一些 continue 语句。你们已经非常接近了,只是需要重新安排一些事情。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean yesno = true;
while (yens == true) {
System.out.println("Enter a 5 digit number...");
try {
int number = scan.nextInt();
int length = String.valueOf(number).length();
if (length == 5) {
String digits = String.valueOf(number);
char number1 = digits.charAt(0);
char number2 = digits.charAt(1);
char number3 = digits.charAt(2);
char number4 = digits.charAt(3);
char number5 = digits.charAt(4);
if (number1 % 2 != 0 && number2 % 2 != 0 && number3 % 2 != 0 && number4 % 2 != 0 && number5 % 2 != 0) {
System.out.println("All of the numbers are odd...");
} else {
System.out.println("Not all of the numbers are odd...");
}
} else {
System.out.println("You did not enter a 5 digit number! Try again...");
continue;
}
} catch (Exception e) {
System.out.println("That was not a number! Try again...");
//A newline might still be stuck in the scanner, so clear it
scan.nextLine();
continue;
}
try {
System.out.println("Would you like to enter another number? (Y/N)");
char letter = scan.next().charAt(0);
if (letter == 'Y' || letter == 'y') {
continue;
} else if (letter == 'N' || letter == 'n') {
yesno = false;
} else {
System.out.println("I will take that as a no!");
yesno = false;
}
} catch (Exception e) {
System.out.println("You did not enter a letter...");
scan.nextLine();
}
}
System.out.println("Goodbye!");
}
顺便说一句,代码中还有很多其他问题需要解决,但我只是回答了您提出的问题,使执行在抛出异常后返回到循环顶部。
关于java - 请输入数字,这不是数字,请重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193955/