基本上我要做的是:
让用户输入一个5位数字。如果所有的数字 数字是奇数,它将打印出“所有数字都是奇数”,如果 所有的数字都不是奇数,然后它会打印出“不是全部的数字 数字是奇数“,然后它会询问用户是否要输入a 按Y或N键新的数字,然后它会要求他们另外5 数字和重复。
这是我的问题:
当用户没有输入一个5位数时,EG输入一个字符串/字符,然后我想让它说“那不是一个数字,请再试一次”,我尝试用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!");
}
}
答案 0 :(得分:2)
注意:我的回答只能解决他最初的问题。在我没有解决的代码帽中还有很多其他问题。我想要达到目的,只能解决OP提出的问题。
我刚刚重新安排了一些逻辑,并添加了几个继续声明。你很亲密,你只需要重新排列一些东西。
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!");
}
顺便说一下,代码中还有很多其他问题需要解决,但我只回答了你提出的问题,在抛出异常后让执行返回到循环的顶部。
为什么人们在没有给出解释的情况下盲目地投票。我知道我在他/她的代码中没有解决每个问题。他/她问了一个问题,我解决了这个问题。请问一个详细的,具体的问题,而不是丰富代码并要求解决每个问题,这不是重点吗?问多个问题是不是违反了规则?我解决了他提出的问题,不多也不少,所以请解释为什么我的答案值得投票。