【问题标题】:How can i store the user inputs from repeating the same enter the information and then print all of them after typing "no"我如何通过重复相同的输入信息来存储用户输入,然后在输入“否”后打印所有这些信息
【发布时间】: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


【解决方案1】:

在添加了一个额外的学生之后,你似乎没有增加row

System.out.print("Do you want to add more? YES/NO ");
decision=in.nextLine();
            
switch(decision) 
{
  case "yes":
  yn=true;
  **row++**
  System.out.println("Enter Student ID Number: ");
  while(in.hasNext()) {
  students[row][0] = in.nextLine();

在您收到“否”作为输入后 - 您应该迭代并打印用户添加的所有学生 -

for(int i = 0; i <= row; i++){
  var output = String.format("Id: %s, fullname: %s, collage: %s, program: %s", students[row][0], students[row][1], students[row][2], students[row][3]);
  System.out.println(output);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    相关资源
    最近更新 更多