【问题标题】:Saving an undefined number of user input保存未定义数量的用户输入
【发布时间】:2018-12-23 03:06:33
【问题描述】:

我在 JAVA 中创建了一个程序,该程序具有菜单选项 1 到 5。选项 4 是“添加学生”。它问了 4 个问题

Questions: 
Please Enter student name:
Please Enter student course:
Please Enter student number:
Please Enter student gender:

用户给出这些详细信息后,它将保存到一个数组中并结束程序。我不知道如何将这些细节保存到数组中。

这是我自己尝试找到解决方案的程序,但我自己对数组和方法相对较新。

public static void newstudent (String[] name,String[] course,int[] number,String[] gender)
    {  
    }
    public static void selection (int option) // Menu
    {
        switch (option)  
       {
           case 1:
               System.out.println("Display Student option");
               break;
           case 2:
               System.out.println("Search Student");
               break;
           case 3:
               System.out.println("Delete Student");
               break;
           case 4:
               //code for adding new student
               break;
           case 5:
               System.out.println("Exited");
               break;

           default:JOptionPane.showMessageDialog(null, "Invalid option! Please enter in the range from 1 to 5.", "Error", JOptionPane.ERROR_MESSAGE);

       }

    }

    public static void main(String[] args) {
        //Start of Menu loop Statement
        int option1 ;

       do{
       String option = JOptionPane.showInputDialog(null,"Enter your option:\n"+"\n"+"1. Display Students\n"+"2. Search Students\n" + "3. Delete Students\n"+"4. Add Students\n"+"5. Exit ","DMIT Students",JOptionPane.QUESTION_MESSAGE);
       option1 = Integer.parseInt(option);

       selection(option1);


       }while(option1 <1 || option1 > 5);
       // End of Menu Loop statement

    }

}

每次用户输入所有这些详细信息时,我都尝试使用 for 循环为这些数组添加 +1,但 for 循环将陷入无限循环。我能得到什么建议吗?还是更简单的解决方案?

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    首先,您不应该使用数组(您在此处使用的方式),因为您不想将单个学生的详细信息存储在四个不同的位置,即数组。您可以使用您需要的所有详细信息定义一个学生类,然后使用类实例添加详细信息将很简单。有点像 -

    public class Student
    {
        private String m_name;
        private int m_age;
        private String m_course;
        private String m_year;
        private String m_section;
    
        public Student( String name, int age, String course, String year, String section )
        {
            m_name = name;
            m_age = age;
            m_course = course;
            m_year = year;
            m_section = section;
        }
    
        public String getName()
        {
            return m_name;
        }
    
        public int getAge()
        {
            return m_age;
        }
    
        public String getCourse()
        {
            return m_course;
        }
    
        public String getYear()
        {
            return m_year;
        }
    
        public String getSection()
        {
            return m_section;
        }
    
        public String toString()
        {
            return "name: " + m_name + ", age: " + m_age + 
                   ", course: " + m_course + ", year: " + m_year +
                   ", section: " + m_section;
        }
    
        public static void main(String[] args) 
        {
           ArrayList<Student> students = new ArrayList<Student>();
           Scanner input = new Scanner(System.in);
    
           int menuChoice = 4;
           do {
               System.out.println("\t\t\tStudent Record Menu");
               System.out.println("\t\t1. Add Student\t2. View Students\t3. Search Student\t4. Exit");
               try {
                   System.out.println("Enter a choice: ");
                   menuChoice = Integer.parseInt(input.nextLine());
               } catch (NumberFormatException e) {
                   continue;
               }
    
               if (menuChoice==1)
               {
                   System.out.println("Full name:");
                   String name = input.nextLine();
    
                   int age = -1;
                   do {
                       try {
                           System.out.println("Age:");
                           age = Integer.parseInt(input.nextLine());
                       } catch (NumberFormatException e) {
                           System.out.println("Enter a number!");
                           continue;
                       }
                   } while (age <= 0);
    
                   System.out.println("Course:");
                   String course = input.nextLine();
    
                   System.out.println("Year:");
                   String year = input.nextLine();
    
                   System.out.println("Section:");
                   String section = input.nextLine();
    
                   Student student = new Student(name, age, course, year, section);
                   students.add(student);
    
               } else if (menuChoice==2) {
                   System.out.println("Students:");
                   for (Student student : students)
                   {
                       System.out.println(student);
                   }
               }
           } while (menuChoice<4);
        }
    }
    

    以上代码sn-p引用自here

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 1970-01-01
      • 2020-07-19
      • 2023-03-11
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多