2015-12-07 84 views
-1

enter image description here如何創建一個4x9矩陣來存儲36個隨機生成的值並將其顯示到矩陣?

我試過了,但仍然有問題的代碼。不能真正弄清楚如何做到這一點,所以我會很感激任何幫助。它不斷顯示我真的不明白的錯誤消息,我無法做任何其他事情。 這是我做了什麼:

import javax.swing.*; 

int [] player = new int [] {"Player 1", "Player 2", "Player 3", "Player 4"}; 
int [] hole = new int [9]; 
String [][] golf = new String [][] {player[], hole[]}; 

void setup() 
{ 
    JOptionPane.showMessageDialog(null, "Welcome to Golf Simulator" + '\n'); 
    int input = 0; 
    do { 
    String in = JOptionPane.showInputDialog("1. See the final results table"  + '\n' + 
     "2. Enter the golf scores for Player 1 " + '\n' + "3. Enter the golf scores for Player 2" + 
    '\n' + "4. Enter the golf scores for Player 3" + '\n' + "5. Enter the golf scores for Player 4" + 
    '\n' + "6. Results for holes" + '\n' + "7. Exit program", null); 
input = Integer.parseInt(in); 

char c = in.charAt(0); 

switch(c) { 
case '1': 
    showWin(); 
    break; 

case '2': 
    for (int i = 0; i < golf[0].length; i++) { 
    String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 1", null); 
    int n = Integer.parseInt(inn); 
    golf[0][i] = n; 
    } 
    break; 

case '3': 
    for (int i = 0; i < golf[0].length; i++) { 
    String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 2", null); 
    int n = Integer.parseInt(inn); 
    golf[1][i] = n; 
    } 
    break; 

case '4': 
    for (int i = 0; i < golf[0].length; i++) { 
    String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 3", null); 
    int n = Integer.parseInt(inn); 
    golf[2][i] = n; 
    } 
    break; 

case '5': 
    for (int i = 0; i < golf[0].length; i++) { 
    String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 4", null); 
    int n = Integer.parseInt(inn); 
    } 
    break; 

case '6': 
showWin(hole); { 
    int score = 0; 
    ArrayList<Integer> tied = new ArrayList<>(); 
    for (int i = 0; i < golf.length; i++) { 
     if (golf[i][hole] > score) { // new winnger 
     score = golf[i][hole]; // adjust high score 
     tied.clear(); // throw out every tied player 
     tied.add(winner); // add the winner 
     } else if (golf[i][hole] == score) { // we have a new tied player 
     tied.add(i); // add him 
     } 
    } 
    if (tied.size() <= 1) { 
     str += "Winner: " + winner + " with score: " + score; 
    } else { 
     str += "Tied: "; 
     for (int i : tied) { // add all tied players to the output 
     str += i + " "; 
     } 
     str += "with score: " + score; 
     } 
     } 
    } 
    } while (input != 6); 
} 

void showWin() 
{ 
    String str = ""; 
    for (int i = 0; i < win.length; i++) 
    { 
    str = str + win[i] + '\n'; 
    } 
    JOptionPane.showMessageDialog(null, str); 
} 

錯誤消息我得到了說:意外的標記:>在那裏說ArrayList<Integer> tied = new ArrayList<>();

+0

您是否可以包含來自編譯的stackstrace或錯誤消息。我們不是在這裏猜測你卡在哪裏。 – SomeJavaGuy

回答

0

這行代碼使用了類型推斷的Java 7的特性:

ArrayList<Integer> tied = new ArrayList<>(); 

我的猜測是,你使用的是Java 6的編譯器(或complience)您可以使用Java 7的兼容編譯或修改行以符合Java 6的編譯器編譯:

ArrayList<Integer> tied = new ArrayList<Integer>();