【发布时间】: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 循环将陷入无限循环。我能得到什么建议吗?还是更简单的解决方案?
【问题讨论】: