java - 将用户输入插入二维数组

标签 java multidimensional-array user-input

我的老师希望我编写代码,询问用户他们想要的行数和列数,然后使用该输入创建一个二维数组。但每次我尝试将变量放入行和列括号中时,第 17 行都会出现此错误:

"The expression must be an array type but it resolved to a class"

package Theatre;

import java.util.Scanner;

public class BoxOffice {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in); 

        System.out.print("How many rows do you want in your Theatre");
        int r = input.nextInt(); input.nextLine();
        System.out.print("How many Columns do you want in your Theatre");
        int c = input.nextInt(); input.nextLine();
        System.out.printf("You want %d rows and %d columns in your Theatre", r, c);
        int [] [] seat = int [r] [c];  // <-- line 17

我希望第 17 行创建一个二维数组,其中用户想要变量 r 的行位于行括号中,列位于列括号中,但出现错误。

最佳答案

扫描仪输入 = new Scanner(System.in);

    System.out.print("How many rows do you want in your Theatre");
    int r = input.nextInt(); input.nextLine();
    System.out.print("How many Columns do you want in your Theatre");
    int c = input.nextInt(); input.nextLine();
    System.out.printf("You want %d rows and %d columns in your Theatre", r, c);


    int [] [] seat = new int [r] [c];

您忘记将 new 关键字放在数组声明中。

关于java - 将用户输入插入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717114/

相关文章:

java - 像 netbeans 那样获取函数的参数名称

c# - 正确打印二维数组?

java - 将用户输入添加到 ArrayList

java - 我不知道如何将用户输入限制为数字和逗号

java - 我们可以使用 Java Web 应用程序在客户端计算机上打开共享驱动器吗?

java - 如何删除 ThreadPoolExecutor 中的旧排队任务并插入新任务?

java - 在主方法中保持扫描仪运行/监听(两个线程)

c++ - 将多维数组传递给 C++ 中的函数

c - 在 C 中对二维字符串数组进行排序

c - 在不阻塞无限循环的情况下获取用户输入