Java 如何创建4x9矩阵来存储36个随机生成的值并将其显示到矩阵?
我试过这样做,但代码仍然有问题。我真的不知道怎么做,所以我会感激任何帮助。它不断显示错误消息,我真的不明白,我只是不能做任何其他事情。 这就是我所做的:Java 如何创建4x9矩阵来存储36个随机生成的值并将其显示到矩阵?,java,arrays,matrix,2d,max,Java,Arrays,Matrix,2d,Max,我试过这样做,但代码仍然有问题。我真的不知道怎么做,所以我会感激任何帮助。它不断显示错误消息,我真的不明白,我只是不能做任何其他事情。 这就是我所做的: 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[],
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);
}
import javax.swing.*;
int[]player=新int[]{“player 1”、“player 2”、“player 3”、“player 4”};
int[]孔=新int[9];
字符串[][]高尔夫=新字符串[][]{玩家[],洞[]};
无效设置()
{
showMessageDialog(null,“欢迎使用高尔夫模拟器”+'\n');
int输入=0;
做{
字符串in=JOptionPane.showInputDialog(“1.参见最终结果表”+'\n'+
“2.输入球员1”+“\n”+“3.输入球员2”+的高尔夫分数
“\n”+“4.输入球员3的高尔夫成绩”+“\n”+“5.输入球员4的高尔夫成绩”+
'\n'+“6.孔的结果“+”\n'+“7.退出程序”,null);
输入=整数.parseInt(in);
字符c=英寸字符(0);
开关(c){
案例“1”:
showWin();
打破
案例“2”:
对于(int i=0;i得分){//new winnger
得分=高尔夫[i][hole];//调整高分
打成平局。清除();//把所有打成平局的球员都赶出去
平局。添加(赢家);//添加赢家
}否则,如果(高尔夫[i][hole]==得分){//我们有一个新的打成平局的球员
并列。加上(i);//加上他
}
}
如果(tied.size(),其中表示ArrayList tied=new ArrayList();
这行代码使用java 7的类型推断功能:
ArrayList<Integer> tied = new ArrayList<>();
ArrayList tied=new ArrayList();
我的猜测是,您正在使用java 6编译器(或Compliance)进行编译。您可以使用java 7兼容编译器,也可以修改该行以符合java 6编译器:
ArrayList<Integer> tied = new ArrayList<Integer>();
ArrayList tied=new ArrayList();
能否将stackstrace或编译过程中的错误消息包括在内。我们不是来猜测您被困在哪里的。