我正在尝试做Yahtzee计划。我开始尝试在继续休息之前让卡对完成。
public class PlayEngine {
public PlayEngine(){}
public int evaluate(int[] dicePoints){
double dice1 = Math.random()*6+1;
int diceint1 = (int) dice1;
double dice2 = Math.random()*6+1;
int diceint2 = (int) dice2;
double dice3 = Math.random()*6+1;
int diceint3 = (int) dice3;
double dice4 = Math.random()*6+1;
int diceint4 = (int) dice4;
double dice5 = Math.random()*6+1;
int diceint5 = (int) dice5;
/* int[] dicePoints = {dice1, dice2, dice3, dice4, dice5}; */
int par = 0;
if(dice1==dice2 || dice1==dice3 || dice1==dice4 || dice1==dice5 || dice2==dice3 || dice2==dice4 || dice2==dice5 || dice3==dice4 || dice3==dice5 || dice4==dice5)
{
par = 1;
return par;
} else {
return par;
}
} //dicePoints
} //PlayEngine
当我运行时,我没有错误。我只是起床了#34;没有主要方法......",这就是应该如何。
当我尝试运行该程序时,我们从学校来测试是否得分:
import javax.swing.*;
import java.util.*;
public class TestScore {
public static void main(String[] args) {
int[] dicePoints = new int[5];
int[] playPoints = new int[15];
PlayEngine player = new PlayEngine ();
String[] kategori = {"Ettor ",
"Tvåor ",
"Treor ",
"Fyror ",
"Femmor ",
"Sexor ",
"Par ",
"Två Par ",
"Triss ",
"Fyrtal ",
"Liten stege ",
"Stor stege ",
"Kåk ",
"Chans ",
"Yatzy "};
String indata, utdata;
while(true) {
indata = JOptionPane.showInputDialog("Ange tärningarnas värden: ");
if (indata == null) // Avsluta genom att trycka Cancel
break;
StringTokenizer ordSeparator = new StringTokenizer(indata, " ,\t");
if (ordSeparator.countTokens() == 5) {
int index = 0;
while(ordSeparator.hasMoreTokens() && index < 5) {
String ettOrd = ordSeparator.nextToken();
dicePoints[index] = Integer.parseInt(ettOrd);;
index = index + 1;
}
playPoints = player.evaluate(dicePoints);
utdata = "Tärningarnas värden: ";
for (int i = 0; i < dicePoints.length; i = i + 1)
utdata = utdata + dicePoints[i] + " ";
utdata = utdata + "\n";
for (int i = 0; i <playPoints.length; i = i + 1) {
utdata = utdata + kategori[i] + playPoints[i] + "\n";
}
JOptionPane.showMessageDialog(null, utdata);
}
else
JOptionPane.showMessageDialog(null, "Du har angivt felaktigt antal värden!!!");
}
}//main
} //TestScore
我收到此错误:
TestScore.java:36: error: incompatible types: int cannot be converted to int[]
playPoints = player.evaluate(dicePoints);
^
为什么?有什么问题?
更新的代码:
import javax.swing.*;
import java.util.*;
public class TestScore {
public static void main(String[] args) {
int[] dicePoints = new int[5];
int playPoints;
PlayEngine player = new PlayEngine ();
String[] kategori = {"Ettor ",
"Tvåor ",
"Treor ",
"Fyror ",
"Femmor ",
"Sexor ",
"Par ",
"Två Par ",
"Triss ",
"Fyrtal ",
"Liten stege ",
"Stor stege ",
"Kåk ",
"Chans ",
"Yatzy "};
String indata, utdata;
while(true) {
indata = JOptionPane.showInputDialog("Ange tärningarnas värden: ");
if (indata == null) // Avsluta genom att trycka Cancel
break;
StringTokenizer ordSeparator = new StringTokenizer(indata, " ,\t");
if (ordSeparator.countTokens() == 5) {
int index = 0;
while(ordSeparator.hasMoreTokens() && index < 5) {
String ettOrd = ordSeparator.nextToken();
dicePoints[index] = Integer.parseInt(ettOrd);;
index = index + 1;
}
playPoints = player.evaluate(dicePoints);
utdata = "Tärningarnas värden: ";
for (int i = 0; i < dicePoints.length; i = i + 1)
utdata = utdata + dicePoints[i] + " ";
utdata = utdata + "\n";
for (int i = 0; i <playPoints.length; i = i + 1) {
utdata = utdata + kategori[i] + player.evaluate(dicePoints) + "\n";
}
JOptionPane.showMessageDialog(null, utdata);
}
else
JOptionPane.showMessageDialog(null, "Du har angivt felaktigt antal värden!!!");
}
}//main
} //TestScore
答案 0 :(得分:0)
evaluate
方法返回int
(定义为public int evaluate(int[] dicePoints){
)而不是int[]
。您正尝试将evaluate方法返回的值保存为数组:
playPoints = player.evaluate(dicePoints);
将playPoints
变量定义为int[] playPoints = new int[15];
将其更改为int playPoints;