在Java中使用二维数组填充表时,出现错误,这段代码有什么问题?

发布于 2024-09-30 13:49:02 字数 875 浏览 0 评论 0原文

我将从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-10-07 13:49:02

只需说 studentResults[x][y] = sc.nextInt(); ,前面没有 int

仅当首次声明变量时才将类型放在变量名称前面。

Just say studentResults[x][y] = sc.nextInt(); with no int in front of it.

You only put the type in front of a variable name when you are first declaring it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文