所以我正在使用try/catch块来捕获(这是作业)的猜数游戏。如果用户输入非整数值或低于或高于给定范围的数字(在本例中为1-10)。实现try/catch块
但我有理解/正确放置try catch块,我读了它是如何工作的,但是当我尝试将它实现到我的简单代码中时,它似乎被忽略。
下面是代码
//import statements
import java.util.*; //for scanner class
// class beginning
class Guess {
public static void main(String[] args) {
//Declare variables area
int secretNumber, guess;
secretNumber = (int) (Math.random() * 10 + 1);
Scanner keyboard = new Scanner(System.in);
// beginning message to user to explain the program
System.out.println("Welcome to my guess a number program!");
System.out.println("Please enter in a number to begin guess what the secret number is(1-10): ");
//Collect inputs from user or read in data here
System.out.println("Enter a guess (1-10): ");
try {
guess = keyboard.nextInt();
if (guess < 1 || guess > 10){
}
} catch (InputMismatchException e) {
guess= keyboard.nextInt();
System.out.println("Not a valid integer");
}
//Echo input values back to user here
//main code and calculations to do
do {
if (guess < 1 || guess > 10) {
System.out.println("Your guess is not in the corre try again.");
guess = keyboard.nextInt();
}
if (guess == secretNumber) {
System.out.println("Your guess is correct. Congratulations!");
guess = keyboard.nextInt();
} else if (guess < secretNumber) {
System.out
.println("Your guess is smaller than the secret number.");
guess = keyboard.nextInt();
} else if (guess > secretNumber) {
System.out
.println("Your guess is greater than the secret number.");
guess = keyboard.nextInt();
}
} while (guess != secretNumber);
//End program message
System.out.println();
System.out.println("Hope you enjoyed using this program!");
}// end main method
}// end class
它不显示"Your guess is correct. Congratulations!"
当你猜对数。
我正在实现try catch块吗?如果不是,我怎么能解释它,所以我可以做第二个捕获浮点数,字符串和任何不是int的东西。
它所做
- 它正确地从1-10
- 随机化程序没有检查,看它是否在给定范围
编辑下面 是内更新的代码我现在唯一的问题是,我无法弄清楚如何应用另一个捕获,如果用户只需输入没有输入任何内容我假设我需要从用户的输入变成一串然后比较?
//import statements
import java.util.*; //for scanner class
// class beginning
public class Guess {
public static void main(String[] args) {
//Declare variables area
int guess, secretNumber = (int) (Math.random() * 10 + 1), lowGuess,highGuess;
Scanner keyboard = new Scanner(System.in);
// beginning message to user to explain the program
System.out.println("Welcome to my guessing number program!");
System.out.println("Please enter in a number to start guessing(enter in a number between 1-10): ");
//main code and calculations to do
guess = 0;
lowGuess = 0;
highGuess = 11;
do {
try {
guess = keyboard.nextInt();
if (guess < 1 || guess >10){
System.out.println("Your guess is not in the correct range try again.");
}
else if(guess == secretNumber){
System.out.println("Your guess is correct. Congratulations!");
}
else if(guess < secretNumber && guess <= lowGuess){
System.out.println("The number you entered is either the same entered or lower please re-enter");
}
else if (guess < secretNumber && guess > lowGuess){
lowGuess = guess;
System.out.println("Your guess is smaller than the secret number.");
}
else if (guess > secretNumber && guess >= highGuess){
System.out.println("The number you entered is either the same entered or higher please re-enter");
}
else if (guess > secretNumber && guess < highGuess){
highGuess = guess;
System.out.println("Your guess is greater than the secret number.");
}
} catch (InputMismatchException e) {
System.out.println("Not a valid input please re-enter");
keyboard.next();
guess = 0;
}
} while (guess != secretNumber);
//End program message
System.out.println();
System.out.println("Hope you enjoyed using this program!");
}// end main method
}// end class
你只会第一次捕捉到异常,并且会丢失所有其他'nextInt()'。我建议你在do/while里只做一次nextInt()。 – m0skit0
您的缩进不会让您的代码更容易阅读。也许让你的IDE为你的代码格式化(在Eclipse中你可以使用'Source'菜单以及'Format'或'Correct Indentation')。 – Pshemo
你告诉我们它不*做什么。它会做什么? –