Java 保存未定义数量的用户输入
我用JAVA创建了一个程序,它有1到5个菜单选项。选项4为“添加学生”。它问了4个问题Java 保存未定义数量的用户输入,java,arrays,methods,Java,Arrays,Methods,我用JAVA创建了一个程序,它有1到5个菜单选项。选项4为“添加学生”。它问了4个问题 Questions: Please Enter student name: Please Enter student course: Please Enter student number: Please Enter student gender: 在用户给出这些详细信息后,它将保存到一个数组中并结束程序。我完全不知道如何将这些细节保存到数组中 这是我的程序,我试图自己找到一个解决方案,但我对数组和方法比
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
}
}
public static void newstudent(字符串[]名称,字符串[]课程,整数[]编号,字符串[]性别)
{
}
公共静态无效选择(int选项)//菜单
{
开关(选件)
{
案例1:
System.out.println(“显示学生选项”);
打破
案例2:
System.out.println(“搜索学生”);
打破
案例3:
System.out.println(“删除学生”);
打破
案例4:
//添加新学生的代码
打破
案例5:
System.out.println(“退出”);
打破
默认值:JOptionPane.showMessageDialog(null,“无效选项!请输入1到5之间的范围。”,“错误”,JOptionPane.Error\u消息);
}
}
公共静态void main(字符串[]args){
//菜单循环语句的开始
int选项1;
做{
String option=JOptionPane.showInputDialog(null,“输入您的选项:\n“+”\n“+”1.显示学生\n“+”2.搜索学生\n“+”3.删除学生\n“+”4.添加学生\n“+”5.退出”,“管理学生”,JOptionPane.QUESTION\u消息);
option1=Integer.parseInt(选项);
选择(选项1);
}while(选项15);
//菜单循环结束语句
}
}
我试着做一个for循环,每当用户输入所有这些细节时,就向这些数组添加+1,但是for循环将被卡在一个无限循环中。我能得到什么建议吗?还是更简单的解决方案?首先,你不应该使用数组(你在这里使用的方式),因为你不想将单个学生的详细信息存储在四个不同的地方,即数组。您可以使用所需的所有详细信息定义一个类学生,然后使用类实例添加详细信息就很简单了。差不多-
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);
}
}
公共班级学生
{
私有字符串m_name;
私人国际博物馆;
私人弦乐硕士课程;
私人字符串m_年;
专用字符串m_段;
公立学生(字符串名称、整数年龄、字符串课程、字符串年份、字符串部分)
{
m_name=名称;
m_年龄=年龄;
m_课程=课程;
m_year=年;
m_截面=截面;
}
公共字符串getName()
{
返回m_名称;
}
公共整数getAge()
{
返回m_年龄;
}
公共字符串getCourse()
{
返回m_课程;
}
公共字符串getYear()
{
返回m_年;
}
公共字符串getSection()
{
返回m_段;
}
公共字符串toString()
{
返回“姓名:+MU姓名+”,年龄:+MU年龄+
,课程:“+m_课程+”,年份:“+m_年”+
,节:“+m_节;
}
公共静态void main(字符串[]args)
{
ArrayList students=新ArrayList();
扫描仪输入=新扫描仪(System.in);
int menuChoice=4;
做{
System.out.println(“\t\t\t学生记录菜单”);
System.out.println(“\t\t1.添加学生\t2.查看学生\t3.搜索学生\t4.退出”);
试一试{
System.out.println(“输入选项:”);
menuChoice=Integer.parseInt(input.nextLine());
}捕获(数字格式){
继续;
}
如果(menuChoice==1)
{
System.out.println(“全名:”);
字符串名称=input.nextLine();
int age=-1;
做{
试一试{
System.out.println(“年龄:”);
age=Integer.parseInt(input.nextLine());
}捕获(数字格式){
System.out.println(“输入一个数字!”);
继续;
}
}while(年龄)