Java 如何正确使用try-catch?
我正在尝试处理如何使用try-catch。我知道它将“尝试”主代码,如果它不工作,它将捕获它并执行不同的操作。我还希望不断提示用户输入正确的值 我不断得到inputmismatch异常错误,即使我将catch设置为在其块中 澄清一下:当我向用户询问他们计划停留多长时间,以及他们希望呆在哪一层楼的INT时,try-catch就会出现。因此,我想处理的错误包括非整数,以及它们是否超出“hotel”的范围 这是我的密码:Java 如何正确使用try-catch?,java,for-loop,exception-handling,try-catch,Java,For Loop,Exception Handling,Try Catch,我正在尝试处理如何使用try-catch。我知道它将“尝试”主代码,如果它不工作,它将捕获它并执行不同的操作。我还希望不断提示用户输入正确的值 我不断得到inputmismatch异常错误,即使我将catch设置为在其块中 澄清一下:当我向用户询问他们计划停留多长时间,以及他们希望呆在哪一层楼的INT时,try-catch就会出现。因此,我想处理的错误包括非整数,以及它们是否超出“hotel”的范围 这是我的密码: public class Hotel{ public static vo
public class Hotel{
public static void main(String[] args) throws IOException {
int choice = 0;
String guestName = " ";
int stayTime = 0;
int floorPref = 0;
System.out.println("Welcome to the Hotel California.");
Scanner sc = new Scanner(System.in);
Room[][] hotel = new Room[8][20];
for(int i = 0; i< hotel.length; i++){
for(int j = 0; j<hotel[i].length;j++){
hotel[i][j] = new Room(0,false,"none",0.00,0);
int roomNum = ((i+1) * 100) + (j + 1);
hotel[i][j].setRoom(roomNum);
int roomCheck = hotel[i][j].getRoomNumber();
if(roomCheck > 500){
hotel[i][j].setPrice(350.00);
}
else if(roomCheck < 500){
hotel[i][j].setPrice(200.00);
}
}
}
// Guest check-in interface.
do{
System.out.println("What business have you today?");
System.out.println("1. Guest Registration");
System.out.println("2. Guest Checkout");
System.out.println("3. Show me occupied rooms");
System.out.println("4. Exit");
choice = sc.nextInt();
if(choice == 1){
System.out.println("Tell us about yourself.");
System.out.println("Please input your name:");
guestName = sc.next();
System.out.print("How long are you planning to stay?");
try{
stayTime = sc.nextInt();
}
catch(InputMismatchException e){
System.out.println("Please input a valid integer.");
stayTime = sc.nextInt();
}
System.out.println("Great. What floor would you like to be on? Enter a number 1-8, 0 for no preference.");
floorPref = sc.nextInt();
System.out.println("The following rooms are available based on your floor preference (floors 1-8, 0 for no preference: ");
}
if(floorPref > 0){
for(int i = 0; i < hotel[floorPref].length; i++){
if(hotel[floorPref][(i)].getOccupation() == false){
System.out.print("Rooms " + hotel[floorPref-1][i].getRoomNumber() + ", ");
}
}
System.out.println("Are available today.");
}
else if(floorPref == 0){
for(int i = 0; i < hotel.length; i++){
for(int j = 0; j < hotel[i].length; j++){
System.out.print("Room " + hotel[i][j].getRoomNumber() + ", ");
}
}
System.out.println("Is available.");
}
}while(choice != 4);
}
}
公共级酒店{
公共静态void main(字符串[]args)引发IOException{
int-choice=0;
字符串guestName=“”;
int statime=0;
int floorPref=0;
System.out.println(“欢迎来到加利福尼亚酒店”);
扫描仪sc=新的扫描仪(System.in);
房间[]酒店=新房间[8][20];
对于(int i=0;i0){
对于(int i=0;i
试着把它放在代码的顶部
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.IOException;
在您的
public static void抛出IOException main
deletethrows IOException
中,它只保留public static void main
希望它能工作您现在拥有的
try-catch
块有缺陷,因为一旦进入catch
块,用户所要做的就是输入非整数的内容以使整个程序崩溃
相反,要获取stayTime
和从扫描仪
提取的所有其他int,请创建一个单独的函数,在用户输入int之前阻塞该函数:
private static int parseIntFromScanner(Scanner sc) {
while(true) {
try {
int toReturn = sc.nextInt();
return toReturn;
} catch(InputMismatchException ime) {
//Continue back to the top of the loop, ask again for the integer
}
}
}
下面是要划分的一小段代码。如果用户输入零,它将转到catch,您也可以输入number。 注意 代码只会被捕获一次。我使用了去种族化的方法。这只是一个符合您要求的示例
import java.io.DataInputStream;
import java.io.IOException;
public class test1 {
public static void main(String args[]) throws NumberFormatException, IOException
{
DataInputStream d=new DataInputStream(System.in);int x;
try {
x=Integer.parseInt(d.readLine());
int z=8;
System.out.println(z/x);
} catch (Exception e)
{
System.out.println("can not divide by zero");
x=Integer.parseInt(d.readLine());
}
}
}
要回答您关于它为什么仍然失败的问题:对
nextInt()
的所有调用都会抛出InputMistmatchException
,而不仅仅是放在try-catch块的try部分的调用。这就是现在发生的情况
另一个问题是在catch
块内第二次调用nextInt
。catch块内抛出的异常需要自己处理,与它们所属的try-catch块分开。因此,如果catch块内的nextInt
抛出inputmaschException
,它将离开try-catch块并从那里开始工作。对于发布的代码,这意味着main将在异常时终止
正如musical_coder指出的,代码需要循环验证。否则,如果第二次尝试读取值失败,代码将无法设置值
顺便说一句,请参阅以下内容:,以获取有关扫描仪使用的提示,这将导致更多的麻烦。事实上,它说明了为什么您现在使用的try-catch处理最终总是会终止程序。(这是扫描仪如何工作的问题)
公共静态void抛出IOException main(字符串[]args)
检查这一点。我在抛出部分输入,认为这会有所帮助。即使没有它,仍然崩溃。你到底从哪里得到这个错误?第70行,下面是出现的问题:你打算停留多少天?jon请输入一个有效的整数。线程“main”java.util.InputMismatchException中的异常位于java.util.Scanner.throwFor(Scanner.java:909)在Hotel.main(Hotel.java:77)的java.util.Scanner.nextInt(Scanner.java:2160)在java.util.Scanner.nextInt(Scanner.java:2119)在java.util.Scanner.next(Scanner.java:1530)在java.util.Scanner.nextInt(Scanner.java:2160)请显示您收到的错误消息。并尝试使用System.out.println语句,以便您可以跟踪您从何处获得异常引发异常没有错!我更希望有一个程序,其main
方法引发异常,