簡體   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