我的老师要我编写代码,要求用户提供所需的行数和列数,然后使用该输入来创建2d数组。但是每次我尝试将变量放在行和列括号中时,都会在第17行收到此错误:
“表达式必须是数组类型,但解析为类”
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行创建一个2d数组,其中用户想要的行r放在行括号内,而列括号内的列却出现错误。
答案 0 :(得分:0)
The new
keyword is what you need to use to create a new instance of something, including an array.
int [] [] seat = new int [r] [c];
答案 1 :(得分:0)
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 = new int [r] [c];
You forgot to put the new Keyword on your array declaration.