我会构建一个程序,该程序采用不确定数量的输入,将其存储在阵列清单中,并仅在退出程序之前打印
我无法将头缠住:我要求循环,最后要求用户想重复该过程。但是这样做,我从根本上重置了循环的计数器,当用户想插入另一组数据时,我无法将数组填写在索引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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为InputData()起草了您的代码的快速版本,该版本减少了重复()的需求。
I drafted a quick version of your code for inputdata() which cuts out the need for repeat().