在Java中使用二维数组填充表时,出现错误,这段代码有什么问题?
我将从 Java 中的二维数组开始我的第一步。我需要一些实际例子的帮助。下面的代码应将学生及其 3 门不同课程的成绩存储在表中,但代码会引发错误。该错误是由行 int StudentResults[x][y] = sc.nextInt();
引起的 有什么想法吗?谢谢。
import java.util.*;
class CalcAverage
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("how many students?: ");
int nbrOfStudents = sc.nextInt();
int[][] studentResults ;
studentResults = new int[nbrOfStudents][3] ;
for (int x = 0 ; x < nbrOfStudents ; x++)
{
System.out.println("Student " + (x + 1) + " : ");
for (int y =0 ; y < 3 ; y++)
{
System.out.println("enter result course " + (y + 1) + " : ");
int studentResults[x][y] = sc.nextInt();
}
}
}// end main
}//end class
I'm starting my first steps with 2D arrays in Java. I require some help on a practical example. The below code should store students and their results for the 3 different courses in a table but the code throws an error. The error is caused by the line int studentResults[x][y] = sc.nextInt();
Any ideas? Thanks.
import java.util.*;
class CalcAverage
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("how many students?: ");
int nbrOfStudents = sc.nextInt();
int[][] studentResults ;
studentResults = new int[nbrOfStudents][3] ;
for (int x = 0 ; x < nbrOfStudents ; x++)
{
System.out.println("Student " + (x + 1) + " : ");
for (int y =0 ; y < 3 ; y++)
{
System.out.println("enter result course " + (y + 1) + " : ");
int studentResults[x][y] = sc.nextInt();
}
}
}// end main
}//end class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需说
studentResults[x][y] = sc.nextInt();
,前面没有int
。仅当首次声明变量时才将类型放在变量名称前面。
Just say
studentResults[x][y] = sc.nextInt();
with noint
in front of it.You only put the type in front of a variable name when you are first declaring it.