gpt4 book ai didi

java - 这是否正确地将名称和等级存储到数组中?

转载 作者:行者123 更新时间:2023-12-01 15:53:34 26 4
gpt4 key购买 nike

这是否正确地将名称和成绩存储到数组中?

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;

}
}
}

最佳答案

是的。为什么运行此代码后不能打印数组来检查是否正常?

要打印数组,假设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

关于java - 这是否正确地将名称和等级存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5518818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com