该程序将询问用户学生姓名和成绩,然后显示两者。用户输入的值存储在数组名称& 数组等级。我使用一个计数器控制的循环来将用户输入收集到数组中。如果我想为每个学生输入多个年级怎么办? 编程相当新,任何输入或想法将非常感谢如何这样做...
public class Five {
public static void main(String[] args) {
int students;
Scanner input = new Scanner(System.in); //created input scanner
System.out.println("How many students are on your roster? If you wish to exit please type 00: ");// Initializing statement for program************
students = input.nextInt();
if(students == 00) { //Exit program****************
System.out.println("Maybe Next Time!!");
System.exit(0);
}
String[] names = new String[students];// Array names*******
String[]grade = new String[students]; //Array grade********
// Use Counter to go through Array**************************
for(int counter =0; counter < students; counter++){
System.out.println("Enter the name of a student: " + (counter +1));
names [counter] = input.next();
System.out.println("Now enter that students grade A, B, C, D OR F: ");
grade [counter] = input.next();
}
input.close();// End Scanner object
//Use For loop for Printing names and grades entered by the user**************
System.out.println("Your students names and grades are as follows: ");
for(int counter =0; counter < students; counter++){
System.out.println("Name: " + names[counter]);
System.out.println("Grade: " + grade[counter]);
}
}
}
答案 0 :(得分:3)
您可以使用参差不齐的数组来输入多个等级 你需要声明它,所以这是一种方法。
String[] names = new String[students];// Array names*******
String[][]grade = new String[names][]; //Array grade********
for(int i=0; i<names[i];i++)
{System.out.println("How many grades will you enter for "+names[i]+"?")
int temp=input.nextInt();
for(int j=0; j<names[i][temp-1];j++){
grad[i][j]=input.next();
}}
答案 1 :(得分:2)