我会构建一个程序,该程序采用不确定数量的输入,将其存储在阵列清单中,并仅在退出程序之前打印

发布于 2025-02-12 19:04:11 字数 3316 浏览 1 评论 0原文

我无法将头缠住:我要求循环,最后要求用户想重复该过程。但是这样做,我从根本上重置了循环的计数器,当用户想插入另一组数据时,我无法将数组填写在索引0之外,并引入一个全局变量,以管理重复代码的时间也无济于事。 这是代码:

  public class Main  {
    public static Scanner input = new Scanner(System.in);
    public static int counter = 0; 
    public static void main (String[] args) {
        inputdata();
        
    }
    public static void repeat (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(names, ages, passinggrades);
            System.exit(0);
        }
        else if (goon.equalsIgnoreCase("y")) {
            
            inputdata();
        }
        else {
            System.out.println("Wrong input: " + goon + " is not y or n");
            repeat(names, ages, passinggrades);
        }
    }
    public static void inputdata () {
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> ages = new ArrayList<Integer>();
        ArrayList<Double> passinggrades = new ArrayList<Double>();
        
        for (int i = counter; i != -1; i++) {
            System.out.print("insert name here: ");
            String name = input.nextLine();
            System.out.print("insert age here: ");
            int age = input.nextInt();
            System.out.print("insert passing grade here: ");
            double passinggrade = input.nextDouble();
            Student student = new Student(name, age, passinggrade);
            names.add(i, student.name);
            ages.add(i, student.age);
            passinggrades.add(i, student.passinggrade);
            input.nextLine();
            System.out.println(counter);
            counter ++;
            repeat(names, ages, passinggrades);
        }
    }
    public static void showdata (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.print("Name |");
        System.out.print("Age |");
        System.out.print("Grade |");
        System.out.println();
        for (int i = 0; i < names.size(); i++) {
            System.out.print(names.get(i)+ " |");
            System.out.print(ages.get(i)+ " |");
            System.out.print(passinggrades.get(i)+ " |");
            System.out.println();
        }
    }
}

学生类:

public class Student {
    String name;
    int age;
    double passinggrade;
    
    
    Student(String name, int age, double passinggrade){
        this.name = name;
        this.age = age;
        this.passinggrade = passinggrade;
    }
}

编译器在尝试将元素插入第一个以外的元素时的错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:756)
    at java.base/java.util.ArrayList.add(ArrayList.java:481)
    at simpleconstructor2.Main.inputdata(Main.java:42)
    at simpleconstructor2.Main.repeat(Main.java:22)
    at simpleconstructor2.Main.inputdata(Main.java:48)
    at simpleconstructor2.Main.main(Main.java:9)

I can't wrap my head around it: i call for a loop, that at the end asks the user wether they want to repeat the process. But in doing so i fundamentally reset the counter of the loop as i call the entire method, if the user wants to insert another set of data Therefore i cannot fill my array beyond the index 0, and introducing a global variable that manages the amount of times the code has been repeated doesn't help either.
Here is the code:

  public class Main  {
    public static Scanner input = new Scanner(System.in);
    public static int counter = 0; 
    public static void main (String[] args) {
        inputdata();
        
    }
    public static void repeat (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(names, ages, passinggrades);
            System.exit(0);
        }
        else if (goon.equalsIgnoreCase("y")) {
            
            inputdata();
        }
        else {
            System.out.println("Wrong input: " + goon + " is not y or n");
            repeat(names, ages, passinggrades);
        }
    }
    public static void inputdata () {
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> ages = new ArrayList<Integer>();
        ArrayList<Double> passinggrades = new ArrayList<Double>();
        
        for (int i = counter; i != -1; i++) {
            System.out.print("insert name here: ");
            String name = input.nextLine();
            System.out.print("insert age here: ");
            int age = input.nextInt();
            System.out.print("insert passing grade here: ");
            double passinggrade = input.nextDouble();
            Student student = new Student(name, age, passinggrade);
            names.add(i, student.name);
            ages.add(i, student.age);
            passinggrades.add(i, student.passinggrade);
            input.nextLine();
            System.out.println(counter);
            counter ++;
            repeat(names, ages, passinggrades);
        }
    }
    public static void showdata (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.print("Name |");
        System.out.print("Age |");
        System.out.print("Grade |");
        System.out.println();
        for (int i = 0; i < names.size(); i++) {
            System.out.print(names.get(i)+ " |");
            System.out.print(ages.get(i)+ " |");
            System.out.print(passinggrades.get(i)+ " |");
            System.out.println();
        }
    }
}

The Student class:

public class Student {
    String name;
    int age;
    double passinggrade;
    
    
    Student(String name, int age, double passinggrade){
        this.name = name;
        this.age = age;
        this.passinggrade = passinggrade;
    }
}

The error that the compiler gives when I try to insert an element beyond the first:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:756)
    at java.base/java.util.ArrayList.add(ArrayList.java:481)
    at simpleconstructor2.Main.inputdata(Main.java:42)
    at simpleconstructor2.Main.repeat(Main.java:22)
    at simpleconstructor2.Main.inputdata(Main.java:48)
    at simpleconstructor2.Main.main(Main.java:9)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2025-02-19 19:04:12

我为InputData()起草了您的代码的快速版本,该版本减少了重复()的需求。

    ArrayList<Student> students = new ArrayList<>();  //declare and initialize your arrayList outside the loop so it is not redeclared and replaced in each iteration.
    while (true){
        System.out.print("insert name here: ");
        String name = input.nextLine();
        System.out.print("insert age here: ");
        int age = input.nextInt();
        System.out.print("insert passing grade here: ");
        double passinggrade = input.nextDouble();
        Student student = new Student(name, age, passinggrade);
        //redundant-------------------------------------
        //names.add(i, student.name);
        //ages.add(i, student.age);
        //passinggrades.add(i, student.passinggrade);
        //----------------------------------------------
        students.add(student);
        input.nextLine();

        //you can probably fit this in a boolean method that will break or return depending on value of goon
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(students);  //modify your showdata() method accordingly for an ArrayList of Student
            break;
        }else if (goon.equalsIgnoreCase("y")) {
            continue;
        }else {
            System.out.println("Wrong input: " + goon + " is not y or n");
            continue;
        }
    }

I drafted a quick version of your code for inputdata() which cuts out the need for repeat().

    ArrayList<Student> students = new ArrayList<>();  //declare and initialize your arrayList outside the loop so it is not redeclared and replaced in each iteration.
    while (true){
        System.out.print("insert name here: ");
        String name = input.nextLine();
        System.out.print("insert age here: ");
        int age = input.nextInt();
        System.out.print("insert passing grade here: ");
        double passinggrade = input.nextDouble();
        Student student = new Student(name, age, passinggrade);
        //redundant-------------------------------------
        //names.add(i, student.name);
        //ages.add(i, student.age);
        //passinggrades.add(i, student.passinggrade);
        //----------------------------------------------
        students.add(student);
        input.nextLine();

        //you can probably fit this in a boolean method that will break or return depending on value of goon
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(students);  //modify your showdata() method accordingly for an ArrayList of Student
            break;
        }else if (goon.equalsIgnoreCase("y")) {
            continue;
        }else {
            System.out.println("Wrong input: " + goon + " is not y or n");
            continue;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文