java - 如何在类注册表中正确使用for循环?

标签 java for-loop

我被分配制作一个程序,用户可以在整个大学(每门类(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/

相关文章:

java - Java构建MySQL查询的有效方法

java - JButton 打开新的 JFrame?

javascript - 为什么使用 "for...in"进行数组迭代是个坏主意?

javascript - 找出 Javascript 中给定数字中的哪一个与其他数字不同

python - 将字符串中的字符与其索引号相乘

sql - 事务处理 SQL : insert into xyz ( select * from abc )

java - 用 Java 在数据库中保存 XML 文档(Hibernate 和 JAXB 的组合)

java - 如何测量分布式系统中的请求时间?

java - 将段落分解为字符串标记

java - 从另一个数组的值创建一个数组?