我正在做一个简单的程序,这是一个猜谜游戏,你需要做的就是猜测随机数。只要你没有得到正确的答案,它就会不断要求输入。
我创建了两个异常,如果输入值高或低,就会引发异常。
我还需要使用另一个异常,即InputMismatchException,但当它使用时,会给我一个无限循环。它不是要求用户输入,而是直接跳到InputMismatchException。如何使它与我的自定义异常一起工作?
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int min = 1, max = 50;
final int random = (int) Math.floor(Math.random() * (max - min + 1) + min);
boolean status = false;
int count = 0;
while (status != true) {
try {
System.out.println("Guess a number from 1 to 50: ");
int answer = scan.nextInt();
if (answer < random) {
throw new InputTooLowException("");
} else if (answer > random) {
throw new InputTooHighException("");
} else if (answer == random) {
System.out.println("\n" + "Congratulations! You got it in " + count + " attempt/s");
status = true;
}
} catch (InputTooLowException e) {
System.out.println("Too low. Try again.");
status = false;
count = count + 1;
} catch (InputTooHighException e) {
System.out.println("Too high. Try again.");
status = false;
count = count + 1;
} catch (InputMismatchException e) {
System.out.println("Invalid Input");
status = false;
}
}
}
}发布于 2021-10-03 10:46:04
这个程序对我有用。我想再次强调,这是异常的糟糕/非惯用用法。
我强烈推荐约书亚·布洛赫的名著“有效Java”。第10章第69项是关于这一点的:“只在例外情况下使用例外情况”。
卡内基梅隆大学软件工程研究所()在其编码准则中也有这一点。
关于无限循环:我没有意识到InvalidInputException不是--您的自定义异常之一,而是来自java.util并由scan.nextInt抛出的。
扫描仪文件说:
当扫描器抛出一个InputMismatchException时,扫描器将不会传递导致异常的令牌,以便可以通过其他方法检索或跳过它。
这意味着,如果你输入文本,而不是一个数字,扫描仪会让你知道,但不是简单地扔掉输入。你得自己处理。例如,打电话给next。我在这里添加了一张支票hasNext。用户可以按Ctrl+d,这意味着“文件的结束”(EOF)。这将关闭输入流。我的检查在这里防止一个错误,但是您对nextInt的调用可能会抛出一个NoSuchElementException。这应该用真正的代码来处理,但我怀疑你的教授会注意到。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int min = 1, max = 50;
final int random = (int) Math.floor(Math.random() * (max - min + 1) + min);
boolean status = false;
int count = 0;
while (status != true) {
try {
System.out.println("Guess a number from 1 to 50: ");
int answer = scan.nextInt();
// Note that this is a _terrible_ use of Exceptions
// and _bad practice_.
if (answer < random) {
throw new InputTooLowException("");
} else if (answer > random) {
throw new InputTooHighException("");
} else if (answer == random) {
System.out.println("\n" + "Congratulations! You got it in " + count + " attempt/s");
status = true;
}
} catch (InputTooLowException e) {
System.out.println("Too low. Try again.");
count = count + 1;
} catch (InputTooHighException e) {
System.out.println("Too high. Try again.");
count = count + 1;
} catch (InputMismatchException e) {
System.out.println("Invalid Input");
// Check if there actually is an invalid token
if (scan.hasNext()) {
// Discard invalid token
scan.next();
}
}
}
}
// Defining Exceptions here, but could do it in their own files.
private static class InputTooHighException extends Exception {
public InputTooHighException(String msg) { super(msg); }
}
private static class InputTooLowException extends Exception {
public InputTooLowException(String msg) { super(msg); }
}
}https://stackoverflow.com/questions/69423910
复制相似问题