【问题标题】:How to add an exception to account for invalid user input? User should input a name and I need to throw an exception if input is not a name如何添加例外以说明无效的用户输入?用户应输入名称,如果输入不是名称,我需要抛出异常
【发布时间】:2017-03-29 17:48:05
【问题描述】:

Java 新手,在结束 Javascript 培训后于上周五开始。我有一个练习要求以下内容(我已粗体所有我尚未完成的内容):

  • 提供班级学生的信息

  • 提示用户询问特定学生

  • 根据用户提交的信息给予适当的答复

  • 询问用户是否想了解其他学生

  • 无效用户输入的例外情况

  • 尝试合并 IndexOutOfBoundsException 和 IllegalArgumentException

  • 让用户更容易 - 告诉他们哪些信息可用

  • 使用并行数组保存学生数据

我在 StackOverflow 上搜索了答案,发现了很多有用的东西,但它们似乎都与整数有关,而不是字符串……尽管这可能只是我的愚蠢猴子大脑。

我不相信“尝试合并 IndexOutOfBoundsException 和 IllegalArgumentException" 指令是必需的,因此您不必包含这些指令。

任何有用的提示或方向我都会非常感激...请不要嘲笑我的代码,尽管非常有建设性的批评。

事不宜迟:

import java.util.Arrays;
import java.util.Scanner;

public class Students {
    private static String[] students = {"John", "Ben", "Lisa", "Stewart", "Cora"};
    private static int[] grades = {79, 86, 90, 89, 99};

    public static void main(String args[]) {
        System.out.println(Arrays.toString(students));

        Scanner kb = new Scanner(System.in);
        boolean userChoice = true;
        String userInput;
        String choice;

        while(userChoice) {
            System.out.println("Please enter a Student's name to get their grade: ");

            userInput = kb.nextLine();
            
            getGrades(userInput);

            System.out.println("Continue? (y/n)");

            choice = kb.nextLine();

            if (choice.equalsIgnoreCase("n")) {
                userChoice = false;
            }
        }
        kb.close();
    }

    private static void getGrades(String userInput) {
        int length = students.length;

        for(int i = 0; i < length; i++) {
            if(userInput.equals(students[i])) {
                System.out.println(userInput + "'s " + "grade is: " + grades[i]);
            }
        }
    }
}

这是我的输出示例:

[约翰、本、丽莎、斯图尔特、科拉]

请输入学生姓名以获取他们的成绩:

本的成绩是:86

继续? (是/否)

请输入学生姓名以获取他们的成绩:

丽莎

丽莎的成绩是:90

继续? (是/否)

n

进程以退出代码 0 结束

顺便说一句,虽然我今天刚刚注册了解决这个问题的帮助,但这个社区在我学习 Javascript 时对我来说是天赐之物,所以在此正式感谢所有光荣的混蛋!

【问题讨论】:

    标签: java validation exception input error-handling


    【解决方案1】:

    我自己想出来的:P

    最终从我的学生数组中创建了一个列表,并使用 contains 方法将用户输入与我的学生列表进行比较,如果输入与数组中的学生不匹配,则它会引发异常并打印“无效学生姓名”并询问您是否要输入其他姓名。

    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class Students {
        private static String[] students = {"John", "Ben", "Lisa", "Stewart", "Cora"};
        private static int[] grades = {79, 86, 90, 89, 99};
        private static String[] behavior = {"aggressive", "distractable", "attentive", "disruptive", "angelic"};
        private static List myList = Arrays.asList(students);
    
    
        public static void main(String args[]) {
    
            Scanner kb = new Scanner(System.in);
            boolean userChoice = true;
            String userInput;
            String choice;
    
            while(userChoice) {
    
                System.out.println(myList);
    
                System.out.println("Please enter a student's name (case-sensitive) to get their progress report: ");
    
                userInput = kb.nextLine();
    
                try {
                    if (myList.contains(userInput)) {
                        getGrades(userInput);
                    } else {
                        throw new IllegalArgumentException();
                    }
                } catch (IllegalArgumentException e) {
                    System.out.println("Not a valid student's name");
                }
    
                System.out.println("Would you like to enter another student's name? (y/n)");
    
                choice = kb.nextLine();
    
                if (choice.equalsIgnoreCase("n")) {
                    userChoice = false;
                }
            }
        }
    
        private static void getGrades(String userInput) {
            for(int i = 0; i < students.length; i++) {
                if(userInput.equals(students[i])) {
                    System.out.println(userInput + "'s " + "grade is " + grades[i] + " and they are " + behavior[i]);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2023-04-10
      相关资源
      最近更新 更多