簡體   English   中英

將用戶輸入插入二維數組

[英]Inserting user input into a double dimensional array

我的老師要我編寫代​​碼,要求用戶提供所需的行數和列數,然后使用該輸入創建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中的行位於行括號中,而列中的列位於列括號中,但出現錯誤。

new關鍵字是用於創建某些事物(包括數組)的新實例所需要的。

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

掃描儀輸入=新的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];

您忘記將新的關鍵字放在數組聲明中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM