java - java try/catch 的问题

标签 java try-catch message

我在使用此 try/catch 语句时遇到问题。我试图用它来抛出一条错误消息:“请输入一个整数!”如果用户输入一个字母。我使用的第一个有效,但它最终需要两行用户输入而不是一行,所以它基本上跳过了一个应该问的问题。之后,其他的都不起作用,它们只是被完全跳过。我还需要对用户输入做同样的事情,如果用户在应该是字母的地方输入整数,它会抛出一条错误消息,提示“请输入字符串!”。我知道我已经很接近了!

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean validInput = false;
        int val = 0;


        System.out.print("Enter your first name: ");    
        String firstName = input.nextLine();
        System.out.print("Enter your last name: ");
        String lastName = input.nextLine();
        System.out.print("Enter your address: ");
        String address = input.nextLine();
        System.out.print("Enter your city: ");
        String city = input.nextLine();
        System.out.print("Enter your state: ");
        String state = input.nextLine();
        System.out.print("Enter your zip code + 4: ");
        String zip = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.nextLine();
    }
}
        System.out.print("Enter amount owed: ");
        String amount = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.print("Enter your payment amount: ");
        String payment = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.print("Enter the date of payment: ");
        String date = input.nextLine();
        while(!validInput) {
             try {
        val = input.nextInt();
        validInput = true;
           } catch(Exception e) {
        System.out.println("Please enter an integer!");
        input.next();
    }
}
        System.out.println("\t\t\t\t\t" + "XYZ Hospital");
        System.out.println();
        System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
        System.out.println();
        System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
        System.out.println();
        System.out.println(lastName + " " + firstName + "\t" + address + " " + city + ", " + state + " " + zip + "\t" + amount + "\t\t" + payment +"\t\t"+ date);
        System.out.print("Would you like to enter abother patient? Type Yes or No: ");
        String userInput = input.nextLine();
        if (userInput.equals("Yes")) {
            while (userInput.equals("Yes")) {
                System.out.print("Enter your first name: ");    
                String firstName2 = input.nextLine();
                System.out.print("Enter your last name: ");
                String lastName2 = input.nextLine();
                System.out.print("Enter your address: ");
                String address2 = input.nextLine();
                System.out.print("Enter your city: ");
                String city2 = input.nextLine();
                System.out.print("Enter your state: ");
                String state2 = input.nextLine();
                System.out.print("Enter your zip code + 4: ");
                String zip2 = input.nextLine();
                System.out.print("Enter amount owed: ");
                String amount2 = input.nextLine();
                System.out.print("Enter your payment amount: ");
                String payment2 = input.nextLine();
                System.out.print("Enter the date of payment: ");
                String date2 = input.nextLine();
                System.out.println("\t\t\t\t\t" + "XYZ Hospital");
                System.out.println();
                System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
                System.out.println();
                System.out.println("Last" +"\t"+ "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
                System.out.println();
                System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 +"\t\t"+ date2);
                System.out.print("Would you like to enter another patient? Type Yes or No: ");
                userInput = input.nextLine();
            }

        }
        else if (userInput.equals("No")) {
            System.out.println("Goodbye");
        }

最佳答案

在第一个 while 循环之后,您不会将 validInput 重置为 False。所以就不进入下一个了。

关于java - java try/catch 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844244/

相关文章:

javascript - 'try' 'catch' 在此 JavaScript 代码中不起作用

javascript - 将消息从弹出窗口发送到内容脚本?

stream - 写流协议(protocol) : Message size field or Message delimiter?

java - JAXB - 如何在绑定(bind) Java 类中指定 xml 属性

java - 如何反转 SeekBar 值?

java - Java 7 中的 multi-catch 是如何实现的?

scala - 为什么在 for 理解中使用 `Try`?

php - PHP 应用中的未读消息计数

Java for 循环未运行完整循环

java - 我们可以创建具有动态键和动态值的 HashMap 吗?