java dice game, im a beginner in java and struggling .. any suggestions would be nice [closed]

StackOverflow https://stackoverflow.com/questions/23434539

  •  14-07-2023
  •  | 
  •  

Pregunta

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 < affirmedPace) {

 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 affirmedPace; affirmedPace = (int)(Math.random()*6+1) + affirmedPace;

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

} else if (alydarPace < affirmedPace) {

 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, "Would you like to play again?");

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

}

¿Fue útil?

Solución

While what you have is a bit of a mess, I suspect that your actual problem is that you're using an assignment operator (=) where you actually need a comparison operator (==).

It's a classic beginner's trap in Java (and other languages, too, for that matter). The single equals sign means take the variable to the left of the sign, and assign it the value you get by evaluating the right side of the sign.

a = b + 3

Would assign the value b + 3 to a.

The double equals sign means to compare the two values.

a == b + 3

Asks the question "is a 3 more than b".

Inside an if statement's condition in Java, you are required to ask such a question (you need a boolean). The error message you should get from using the wrong one would be something like "Cannot convert from int to boolean."

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top