java - 显示数组中的列表,以数字方式输入数据

标签 java arrays java.util.scanner

我正在尝试获取用户对学生数据的输入。首先,我询问用户他们正在输入多少学生的数据。然后,代码会要求用户提供有关用户在第一个问题中输入的确切学生人数的数据。

下面是我的代码的开头。我在初始变量之后获取用户输入时遇到问题。我需要采用该变量,假设用户输入 5,我需要提示用户 5 次输入学生姓名和成绩。就像这样:

Student 1 last name:
Student 1 first name:
Student 1 grade:

Student 2 last name:

我必须使用数组,我只需要弄清楚如何正确获取用户输入。

import java.util.Scanner;

public class StudentScoresApp {

    public static Score score = new Score();
    private static Student student;

    public static void main(String[] args) {
        System.out.println("Welcome to the Student Scores Application.\n");
        getStudentScores();
    }

    public static void getStudentScores() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of students to enter:   ");
        int num = input.nextInt();
        int [] a = new int[num];
        for (int i = 0 ; i < num ; i++); {
            System.out.print("Enter Student " + (i + 1) + " last name:");
            a[i] = in.nextInt();
        }
    }
}

最佳答案

String [] lastNames = new String [num];
String [] firstNames = new String [num];
int [] grades = new int [num];

for (int i = 0; i < num; i++)
{
    System.out.print ("Enter Student " + (i + 1) + " last name:");
    lastNames [i] = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " first name:");
    firstNames [i] = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " grade:");
    gradess [i] = in.nextInt ();
}

关于java - 显示数组中的列表,以数字方式输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176699/

相关文章:

Java程序无法读取字符

java - 读取由逗号和空格分隔的一行 - java

java - 如何读取仅由空格分隔的一行?

java - 两个内容相同的字符串会存储在同一个内存位置吗?

javascript从字符串动态创建多维数组

arrays - 使用逗号和单引号连接数组元素 - Bash

c++ - 为什么我的银行管理系统不能正常工作?

java - 我是否正确序列化/反序列化?

java - 如何将文本文件内容添加到文本文件包含空格的组合框中?

java - Stream.findAny 是短路操作吗?