繁体   English   中英

需要一些二维数组列表的帮助

[英]need some assistance with 2d arraylists

班级注册是大学生的重要方面。 注册课程时,您必须知道课程参考号(CRN)。

下表列出了一些CRN及其相应的课程名称。

CRN   | COURSE TITLE   
12451 | Introduction to IT Problem Solving Using Computer Programming  
15349 | Object-Oriented Techniques for IT Problem Solving   
18467 | Applied IT Programming  
16890 | Database Fundamentals  
13334 | Database Programming

我需要创建一个程序来招收学生参加这些课程。 用户输入CRN时,单次注册课程。 在注册服务商(用户)指示他们完成输入CRN之前,请继续提示用户输入CRN。 您必须验证CRN,提供错误消息并在输入无效的CRN时重新提示用户。 跟踪每个课程的注册人数。 特定CRN的课程注册人数不能超过其最大注册人数,否则将显示错误消息,并且不会处理该注册人数。

一旦用户表明他们已完成输入CRN,则显示格式正确的报告,其中包含每个课程的列表及其CRN和标题,注册的学生人数以及剩余的座位数。

运行代码后,1.退出在最初的几次中什么都不做2.我仍然没有弄清楚如何忽略重叠的输入3.我需要将限制设置为30

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

class  TwoD
{

        private static boolean typeInt(String a) throws IOException {
  try {
     Integer.parseInt(a);
     return true;
    } catch (NumberFormatException e) {
     return false;
  }
    }

         public static void main(String[] args) throws IOException  
{
     {
         System.out.println("These are the courses that you can choose for the students to enroll. \n 12451 | Introduction to IT Problem Solving Using Computer Programming \n 15349 | Object-Oriented Techniques for IT Problem Solving \n 18467 | Applied IT Programming \n 16890 | Database Fundamentals  \n 13334 | Database Programming");

           ArrayList<String> name = new ArrayList<String>();
                Scanner scan = new Scanner(System.in);

        System.out.print("What is your student's name? ");      
    String student = scan.next();
    String a;

    while(!student.equalsIgnoreCase("quit")) {
        name.add(student); //after a cycle of while, ask user whether he or she would quit and print the arraylist of students.
        System.out.print("What is your student's name? ");
        student = scan.next();

    }

    ArrayList< ArrayList<String> > main = new ArrayList< ArrayList<String> >();
    for(int i = 0; i<name.size(); i++){
       ArrayList<String> course = new ArrayList<String>();
       for (int j = 0; j < name.size(); j++){

            System.out.print("The course the student is currently enrolling is : ");
        a = scan.next();

    while (!a.equalsIgnoreCase("quit"))
    {

  while(!typeInt(a) || Integer.parseInt(a) != 12451 && Integer.parseInt(a) != 15349 && Integer.parseInt(a) != 16890 && Integer.parseInt(a) != 13334 && Integer.parseInt(a) != 18467) {
            System.out.println("Check the courses again.");                     
         a = scan.next();
    }
        course.add(a);

        if (a.equalsIgnoreCase("quit")!=true)
        {
        System.out.print("The course the student is currently enrolling is : ");
        a = scan.next();
        }
        else break;
    }

       }
       main.add(course);
     }

    for(int i = 0; i < name.size(); i++){
        System.out.print("Name " + i + ": ");
        for(int j = 0; j < main.get(i).size(); j++){
            System.out.print(main.get(i).get(j) + " ");
        }
        System.out.println();
    }
}
}
}

这是有关如何调试的教程的链接。 我过去经常花费数小时试图找出我的代码出了什么问题,并在互联网上寻找答案。 最后,我不得不学习最后一个项目何时不能在截止日期之前的两个小时内完成。

此技能将为您提供良好的服务,并在整个职业生涯中节省数百小时。

上述链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM