我如何创建一个 4x9 矩阵来存储 36 个随机生成的值并将其显示到矩阵?

how can i create a 4x9 matrix to store 36 randomly generated values and display it to the matrix?

我试过了,但代码仍然有问题。无法真正弄清楚该怎么做,所以我将不胜感激任何帮助。它一直显示我真的不明白的错误消息,我无法做任何其他事情。 这就是我所做的:

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<>();

这行代码使用了 java 7 类型推断的特性:

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

我的猜测是您正在使用 java 6 编译器(或合规性)进行编译 您可以使用 java 7 兼容编译器或修改该行以符合 Java 6 编译器:

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