Java 如何在处理过程中制作高尔夫模拟器程序?
我正在计算机科学课上做一个高尔夫模拟,但我似乎无法完成以下代码:Java 如何在处理过程中制作高尔夫模拟器程序?,java,multidimensional-array,processing,Java,Multidimensional Array,Processing,我正在计算机科学课上做一个高尔夫模拟,但我似乎无法完成以下代码: import javax.swing.*; int [][] golf = new int[4][9]; void setup() { JOptionPane.showMessageDialog(null, "Welcome to Golf Simulator" + '\n'); int input = 0; do { String in = JOptionPane.showInputDialog("1.
import javax.swing.*;
int [][] golf = new int[4][9];
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':
showgolf();
break;
case '2':
for (int i = 0; i < golf.length; i++)
{
String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 1", null);
int n = Integer.parseInt(inn);
golf[i] = n;
}
break;
case '3':
for (int i = 0; i < golf.length; i++)
{
String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 2", null);
int n = Integer.parseInt(inn);
golf[i] = n;
}
break;
case '4':
for (int i = 0; i < golf.length; i++)
{
String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 3", null);
int n = Integer.parseInt(inn);
golf[i] = n;
}
break;
case '5':
for (int i = 0; i < golf.length; i++)
{
String inn = JOptionPane.showInputDialog("Please enter the golf scores for Player 4", null);
int n = Integer.parseInt(inn);
golf[i] = n;
}
break;
case '6':
for (int i = 0; i < golf.length; i++)
{
String inn = JOptionPane.showInputDialog("Please enter the hole number", null);
int n = Interger.parseInt(inn);
golf[i] = n;
}
break;
default:
JOtionPane.showMessageDialog("Thank you for using Golf Simulator");
break;
}
}while(input != 7);
}
void showgin()
{
String str = "";
for(int i = 0; i < golf.length; i++)
{
str = str + win[i] + '\n';
}
JOptionPane.showMessageDialog(null, str);
}
import javax.swing.*;
int[][]高尔夫=新int[4][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”:
showgolf();
打破
案例“2”:
对于(int i=0;i
正如我提到的,我是一个初学者,所以我可能在这方面犯了无数错误。我应该使用一个二维阵列,以便有4个不同的球员9洞。在此模拟器上,需要有一个弹出窗口,显示:
- 整张桌子
- 用户可以插入每个玩家的分数
- 用户搜索洞号,并给出结果(如玩家1获胜或洞2中出现平局)
- 退出程序的能力
感谢您的帮助:)当您有一个2-dim阵列
golf[4][9]
,golf.length
将返回4
。这是因为基本上有一个长度为4的数组和一个长度为9的数组。所以golf[0]。长度将为9。
因此,您的案例2-5应该如下所示:
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; // use 0 for player 1, 1 for player 2, ...
}
break;
在这里,我们必须做一些逻辑来找出两名球员是平手还是一名球员获胜。我是用一份平手名单做的
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
}
}
int评分=0;
ArrayList绑定=新的ArrayList();
对于(int i=0;i得分){//new winnger
得分=高尔夫[i][hole];//调整高分
打成平局。清除();//把所有打成平局的球员都赶出去
平局。添加(赢家);//添加赢家
}否则,如果(高尔夫[i][hole]==得分){//我们有一个新的打成平局的球员
并列。加上(i);//加上他
}
}
现在我们可以看看名单,如果名单上有一个以上的球员,我们有一个平局,否则单人获胜
if(tied.size() <= 1){
str += "Winner: " + winner + " with score: " + score;
} else {
str += "Tied: ";
for(int i : tied){ // add all tied player to the output
str += i + " ";
}
str += "with score: " + score;
}
if(tied.size())到底是什么问题?这在一个问题中有很多。你能说出发生了什么吗?你输入了什么?你期待什么?你得到了什么?放一些tracesvoid showWin(int-hole){:它首先在void上给了我一个错误,然后在int上给了我一个错误。我应该怎么做?看看错误描述并修复它所说的是错误的。如果你不能弄清楚,请发布更多的上下文,比如void showWin(int-hole){
周围的代码和你得到的错误消息。
if(tied.size() <= 1){
str += "Winner: " + winner + " with score: " + score;
} else {
str += "Tied: ";
for(int i : tied){ // add all tied player to the output
str += i + " ";
}
str += "with score: " + score;
}