我被分配制作一个程序,用户可以在整个大学(每门类(class))的每个部分输入学生。我似乎无法在 for 循环中显示用户输入的值,(我决定每年声明一个特定类(class)的数组)我只是 java 的新手,抱歉,如果它不可读
String[] name1a = new String [2000];
System.out.println("\nDo you want to add students or view student?\nA.ADD\nB.DISPLAY");
System.out.println("Enter the letter of your choice");
view=input.next().charAt(0);
if (view=='a' || view=='A')
{System.out.println("\nThe following are the section/s for "+yearlvl.get(yrlvl-1));
for (int i = 0; i < Sections1a.size(); i++) {
System.out.println((i+1)+"."+Sections1a.get(i));
}//section names display
System.out.println("Enter the number of the section");
int picksection1a=input.nextInt()*60;
input.nextLine();
System.out.println("How many students do you want to add?");
int studnum1a=input.nextInt();
input.nextLine();
for (int counter=picksection1a; counter<(picksection1a+studnum1a); counter++)
{ System.out.println("Student #"+(counter+1)+": ");
name1a[counter]=input.nextLine();
}//for counter
}//if view A
else if (view=='B' || view=='b')
{ System.out.println("\nThe following are the section/s for "+yearlvl.get(yrlvl-1));
for (int i = 0; i < Sections1a.size(); i++) {
System.out.println((i+1)+"."+Sections1a.get(i));
}//section names display
System.out.println("Enter the number of the section");
int viewsection1a=input.nextInt()*60;
input.nextLine();
int dec=1;
for (int display=viewsection1a ; display<(viewsection1a+60); display++)
{
System.out.println("Student #"+dec+": "+name1a[display]);
//it only shows null, the values I inputted doesn't show up
}
}//else if view b
我期望程序显示 60 个值。 (我的意思是输入的值和其余 60 个空值)
最佳答案
更改此行:
name[srt] = input.nextLine();
至:
name[x] = input.nextLine();
关于java - 如何在类注册表中正确使用for循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58788160/