我的异常中的打印语句永远不会打印出来。如果我不输入任何内容,它只会继续要求我输入游轮名称。如果有人输入空字符串或字符串以外的任何内容,我需要它抛出异常打印语句。
do {
try {
System.out.println("Enter name of cruise ship: ");
cruiseShipName = scnr.nextLine();
}
catch(Exception e){
e.printStackTrace();
System.out.println("Invalid Input. Please try again.");
}
} while (cruiseShipName.equals(""));
最佳答案
如果 scnr.nextLine() 为空,也许您的代码不会抛出任何异常。
在您的情况下,如果您想要求用户重试,您可以执行以下操作:
`
do {
System.out.println("Enter name of cruise ship: ");
cruiseShipName = scnr.nextLine();
if (cruiseShipName.equals("")) {
System.out.println("Invalid Input. Please try again.");
}
} while (cruiseShipName.equals(""));
`
关于java - 为什么我的异常不会打印出它的打印语句? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60347355/