2016-05-21 7 views
0

私はJavaで基本的な "Deal or Deal"ゲームを作ろうとしています。私は、私の多次元配列作成者に追加したり削除したりすることに問題があります。 問題は、shuffleBoxes()の7行目とplayerBox()の9行目で発生します。取引や取引ゲームのJava ArrayListの問題

package Deal; 

import java.util.*; 
import java.io.*; 
import javax.swing.*; 

public class sample { 
    public static ArrayList <ArrayList<Integer>> boxes = new ArrayList<ArrayList<Integer>>(22); 

    public static void main (String [] args) throws IOException { 
     playerBox(); 
     dealerOffer(); 
    } 

    public static void shuffleBoxes() { 
     int [] prizes = {1,2,3,4,5,6,10,50,100,250,500,750,1000,3000,10000,15000,20000,35000,50000,75000,100000,250000}; 
     for (int i = 0; i < boxes.size(); i++) { 
      boxes.get(i).add(i+1); 
     } 
     for (int j = 0; j < boxes.size(); j++) { 
      boxes.get(j).get(1).add(prizes[j]); 
     } 
     Collections.shuffle(boxes); 
    } 

    public static int playerBox() { 
     String[] boxChoice = {"1", "2", "3", "4", "5", "6", "7", "8", "9" ,"10", "11", "12", "13", 
     "14", "15", "16", "17", "18", "19", "20", "21", "22"}; 
     String input = (String)JOptionPane.showInputDialog(null, "Choose a box...", "Choose carefully", 
     JOptionPane.QUESTION_MESSAGE, null, boxChoice, boxChoice[0]); 
     int chosenBox = Integer.parseInt(input); 
     for (int i = 0; i < boxes.size(); i++) { 
      if (chosenBox == boxes.get(i).get(0)) 
       boxes.get(i).get(0).remove(chosenBox); 
     } 
     return chosenBox; 
    } 

    public static void dealerOffer() { 
     int average; 
     int sum = 0; 
     for (int i = 0; i < boxes.size(); i++) { 
      sum = sum + (boxes.get(i).get(1)); 
     } 
     average = sum/boxes.size(); 
    } 
} 
+2

あなたは何をしたいですか? –

+2

'私は多次元配列作成者に追加したり削除したりすることに問題があります。問題が何であるかを説明すると良いでしょう。どのようなエラー?あなたは何を期待していますか? –

答えて

3

あなたは

ArrayList <ArrayList<Integer>> boxes = new ArrayList<ArrayList<Integer>>(22); 

を作成するが、これはArrayListに何も入れていません。あなたのコードにboxes.add(...)の参照が見当たらないので、boxes.get()を使用しようとすると例外がスローされます。

List<List<Integer>>が必要と思われる理由はまったくわかりません。多次元リストは一般的にコードの匂いです。 99%のケースでは、カスタムオブジェクトを使用する別のデータ構造が適切です。

+1

タイトな修正をしてくれてありがとう@Tom –