import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.lang.Math;
public class diceGame {
public static void main(String[] arg) {
int alydarPace;
int affirmedPace;
boolean wantsToPlay = true;
ImageIcon INPUTPIC = new ImageIcon("INPUTPIC.jpg");
while(wantsToPlay == true){
JOptionPane.messageDialog(null,"Welcome to watch the greatest rivalry in horse racing history... Affirmed vs Alydar.", "", 0, INPUTPIC);
{
int alydarPace; alydarPace = (int)(Math.random()*6+1);
int affirmedPace; affirmedPace = (int)(Math.random()*6+1);
if (alydarPace > affirmedPace) {
JOptionPane.showMessageDialog(null, "And away they go with Alydar taking the Lead!", "", 0, INPUTPIC);
} else if (alydarPace } else if (alydarPace =affirmedPace) { } int alydarPace; alydarPace = (int)(Math.random()*6+1) + alydarPace;
int 确认Pace;确认步数 = (int)(Math.random()*6+1) + 确认步数; JOptionPane.showMessageDialog(null, "他们来了,获胜者是 Alydar!", "", 0, INPUTPIC); } else if (alydarPace } else if (alydarPace =affirmedPace) { } int answer = JOptionPane.showConfirmDialog(null, "您想再玩一次吗?"); } JOptionPane.showMessageDialog(null, "And away they go with Affirmed taking the Lead!", "", 0, INPUTPIC);
JOptionPane.showMessageDialog(null, "And away they go with both horses evenly running neck and neck!", "", 0, ChildSpainFlag);
JOptionPane.showMessageDialog(null, "Down the stretch they come and the winner is Affirmed!", "", 0, INPUTPIC);
JOptionPane.showMessageDialog(null, "Down the stretch they come.. is a photo finish and the horses have tied!", "", 0, INPUTPIC);
if(answer != JOptionPane.YES_OPTION){
wantsToPlay = false;
} else {
System.exit(0);
}
最佳答案
虽然你所拥有的有点困惑,但我怀疑你的实际问题是你正在使用赋值运算符(=
),而你实际上需要一个比较运算符( ==
)。
这是 Java 初学者的一个典型陷阱(就此而言,其他语言也是如此)。单个等号意味着将变量放在符号左侧,并将通过计算符号右侧得到的值分配给它。
a = b + 3
将值 b + 3
赋给 a
。
double等号表示比较两个值。
a == b + 3
提出问题“a
比b
多3”。
在 Java 中的 if
语句的条件内,您需要提出这样的问题(您需要一个 boolean
)。如果使用错误的错误消息,您应该得到类似“无法从 int 转换为 boolean”之类的错误消息。
关于java骰子游戏,我是java初学者,正在苦苦挣扎..任何建议都会很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23434539/