我的老师希望我编写代码,询问用户他们想要的行数和列数,然后使用该输入创建一个二维数组。但每次我尝试将变量放入行和列括号中时,第 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/