这是否正确地将名称和等级存储到数组中?
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Enter name and grade and save in an array
Scanner input = new Scanner(System.in);
String [] name = new String [50];
int [] grade = new int[50];
for (int i = 0; i < 50; i++){
System.out.println("Enter students name: ");
String studentName = input.nextLine();
name[i] = studentName;
System.out.println("Enter students grade = ");
int studentGrade = input.nextInt();
grade [i] = studentGrade;
}
}
}
答案 0 :(得分:1)
是。为什么在运行此代码后无法打印数组以检查它是否正常?
要打印数组,请说arr
,只需执行:
for(int i = 0; i < arr.length; i++) //go over all i's from 0 to the array length
system.out.print(arr[i]); // and print the value of the array in the i'th place each time
答案 1 :(得分:0)
我是这么认为的,但你应该更好地使用一个hashmap,因为你想存储一个名字和这个名字的等级。
数组中的这些值未链接,例如,您无法对它们进行排序。
答案 2 :(得分:0)
这是一种自己测试这个程序的方法......
for (int i = 0; i < 50; i++){
System.out.println("Enter students name: ");
String studentName = input.nextLine();
name[i] = studentName;
System.out.println("Enter students grade = ");
int studentGrade = input.nextInt();
grade [i] = studentGrade;
// Using print statements to check the values in your array...
System.out.println("Name stored in name array is " + name[i]);
System.out.println("Grade stored in grade array is " + grade[i]);
}
答案 3 :(得分:0)
是的,您的数据已正确存储在数组中。但是,有些笔记可以帮助您学习: