用户使用扫描仪和数组进行多个输入



我正在尝试编写一个程序,该程序依赖于用户输入来创建学生和他们的分数。

import java.util.Scanner;
public class Marks {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("How many students are there? ");
int n = sc.nextInt();
String [] student = new String[n];   
for(int i = 0; i <student.length; i++){
int nextI = i + 1;
System.out.print("Enter name of student " + nextI + ": ");
student[i] = sc.next();
}

我的目标是让程序在用户输入学生姓名后提示一条消息,"输入分数:",但找不到任何网站/帖子来帮助我做到这一点。

您可以有另一个标记数组,并将标记存储在另一个数组中。以下代码可能会有所帮助。

Scanner sc = new Scanner(System.in);
System.out.print("How many students are there? ");
int n = sc.nextInt();
String [] student = new String[n];   
String [] marks =new String[n]; // use another array to store marks
for(int i = 0; i <student.length; i++){
int nextI = i + 1;
System.out.print("Enter name of student " + nextI + ": ");
student[i] = sc.next();
System.out.print("Enter Marks for " + student[i] + ": ");
marks[i]= sc.next(); // store marks for the student
}
System.out.println("StudentName      Marks");
for(int i=0;i<student.length;i++) {
System.out.println(student[i]+"                "+marks[i]);
}

输出:

How many students are there? 2
Enter name of student 1: back
Enter Marks for back: 20
Enter name of student 2: door
Enter Marks for door: 30
StudentName      Marks
back                20
door                30

CCD_ 1可以更有效地做到这一点。探索自己以了解更多关于Map的信息尝试使用Map实现上述代码

最新更新