【发布时间】:2023-02-23 16:08:47
【问题描述】:
我正在做一个由用户输入的学生信息列表。但是我似乎无法解决在键入“是”后添加另一个信息列表的问题,“是”表示添加另一个学生的代码。
我尝试在开关案例中做开关案例,以防用户希望列出多个学生信息。但我没有想法。我希望它重复整个输入信息并从行中存储它(你想添加更多? 是或否),但它只是重复,一旦我输入“否”,我就无法存储它。
这是我的代码:
import java.util.Scanner;
public class test3d {
public static void main(String[] args)
{
String[][] students = new String[50][50];
Scanner in = new Scanner(System.in);
final String toUpperCase;
String decision ="";
boolean yn = true;
//loop to request to fill array
while(yn){
System.out.println("Enter Student ID Number: ");
while(in.hasNext()){
int row = 0;
students[row][0] = in.nextLine();
System.out.println("Enter Student Fullname: ");
students[row][1] = in.nextLine();
System.out.println("Enter Student College: ");
students[row][2] = in.nextLine();
System.out.println("Enter Student Program: ");
students[row][3] = in.nextLine();
System.out.println("Record Successfully Saved");
System.out.print("Do you want to add more? YES/NO ");
decision=in.nextLine();
switch(decision)
{
case "yes":
yn=true;
System.out.println("Enter Student ID Number: ");
while(in.hasNext()) {
students[row][0] = in.nextLine();
System.out.println("Enter Student Fullname: ");
students[row][1] = in.nextLine();
System.out.println("Enter Student College: ");
students[row][2] = in.nextLine();
System.out.println("Enter Student Program: ");
students[row][3] = in.nextLine();
System.out.println("Do you want to add more? YES/NO ");
}
case "no":
yn=false;
System.out.println();
System.out.println("ID NUMBER STUDENT NAME COLLEGE PROGRAM ");
System.out.println(students[row][0]+" "+students[row][1].toUpperCase()+" "+students[row][2].toUpperCase()+" "+students[row][3].toUpperCase());
break;
}
}
}
}
}
【问题讨论】:
-
使用 IDE 的调试功能,单步执行程序并观察变量,尤其是
row。当转到第二个学生的数据条目时,它是否具有您期望的值?除了编码,调试程序的能力也很重要。如果您的课程没有涵盖它,请自行学习。
标签: java arrays sorting input store