java骰子游戏,我是java初学者,正在苦苦挣扎..任何建议都会很好

标签 java dice

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

 JOptionPane.showMessageDialog(null, "And away they go with Affirmed taking the Lead!", "", 0, INPUTPIC);

} else if (alydarPace =affirmedPace) {

 JOptionPane.showMessageDialog(null, "And away they go with both horses evenly running neck and neck!", "", 0, ChildSpainFlag);

}

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

 JOptionPane.showMessageDialog(null, "Down the stretch they come and the winner is Affirmed!", "", 0, INPUTPIC);

} else if (alydarPace =affirmedPace) {

 JOptionPane.showMessageDialog(null, "Down the stretch they come.. is a photo finish and the horses have tied!", "", 0, INPUTPIC);

}

int answer = JOptionPane.showConfirmDialog(null, "您想再玩一次吗?");

if(answer != JOptionPane.YES_OPTION){
  wantsToPlay = false;
} else {
  System.exit(0);
}

}

最佳答案

虽然你所拥有的有点困惑,但我怀疑你的实际问题是你正在使用赋值运算符(=),而你实际上需要一个比较运算符( ==)。

这是 Java 初学者的一个典型陷阱(就此而言,其他语言也是如此)。单个等号意味着将变量放在符号左侧,并将通过计算符号右侧得到的值分配给它。

a = b + 3

将值 b + 3 赋给 a

double等号表示比较两个值。

a == b + 3

提出问题“ab多3”。

在 Java 中的 if 语句的条件内,您需要提出这样的问题(您需要一个 boolean)。如果使用错误的错误消息,您应该得到类似“无法从 int 转换为 boolean”之类的错误消息。

关于java骰子游戏,我是java初学者,正在苦苦挣扎..任何建议都会很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23434539/

相关文章:

c++ - 掷 2 个骰子 1000 次

python - 如何让函数重复自身而不生成退出代码 0?

java - 在类的方法中初始化后,类的字段为空

r - 掷骰子数直到达到停止值

java - 从 jdk8 更改为 jdk11 后,Sonarqube 7.8 Docker 镜像无法运行

java - 如何使用 spring aop 定义在循环内调用的回滚事务?

JAVA,棋盘游戏。随机骰子数字不是随机的

dice - K个N面骰子的不同掷数

java - 这个 double 是什么格式以及如何转换它?

java - 在 Java 中获取特定时间段后的日期