我hava java App模拟游戏" craps"。 在应用程序上,我有一个方法rollDice(),它返回两个随机数的总和。我还有一个按钮,当它点击它时会调用一个名为botonjugar_presionado(ActionEvent事件)的方法。
在方法botonjugar_perionado上,假设发生了
当我点击按钮时,我的应用程序无法正常工作。因此,在尝试调试时,我使用System.out.println(Integer.toString(sumOfDice));看看sumOfDice的数量是多少。但它似乎第二次调用rollDice()?不知道为什么会发生这种情况。代码如下所示。 先感谢您 !
//import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.*;
public class HelloWorld extends JFrame
{
private JButton botonjugar;
private JLabel texto1;
private JLabel texto2;
private JLabel texto3;
private JLabel texto4;
private JLabel textoinfo;
private static JTextField caja1;
private static JTextField caja2;
private static JTextField caja3;
private JTextField caja4;
// create random number generator for use in method rollDice
private static final Random randomNumbers = new Random();
// enumeration with constants that represent the game status
//private enum Status { CONTINUE, WON, LOST };
// constants that represent common rolls of the dice
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_LEVEN = 11;
private static final int BOX_CARS = 12;
public boolean chequear = true;
public int myPoint;
public int sumOfDice;
public String gameStatus;
public String prueba = "";
public HelloWorld()
{
qui();
}
public void qui()
{
gameStatus="";
myPoint =0;
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
texto1= new JLabel("Dado 1");
texto1.setBounds(30,30,50,15);
contentPane.add(texto1);
caja1= new JTextField();
caja1.setBounds(80,30,60,15);
caja1.setEditable(false);
contentPane.add(caja1);
texto2= new JLabel("Dado 2");
texto2.setBounds(160,30,50,15);
contentPane.add(texto2);
caja2= new JTextField();
caja2.setBounds(210,30,60,15);
caja2.setEditable(false);
contentPane.add(caja2);
texto3= new JLabel("Suma");
texto3.setBounds(290,30,50,15);
contentPane.add(texto3);
caja3= new JTextField();
caja3.setBounds(340,30,60,15);
caja3.setEditable(false);
contentPane.add(caja3);
texto4= new JLabel("Punto");
texto4.setBounds(420,30,50,15);
contentPane.add(texto4);
caja4= new JTextField();
caja4.setBounds(470,30,60,15);
caja4.setEditable(false);
contentPane.add(caja4);
botonjugar = new JButton("Tirar los dados");
botonjugar.setBounds(250,60,150,30 );
contentPane.add(botonjugar);
botonjugar.addActionListener(
new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
botonjugar_presionado( event );
}
}
);
textoinfo = new JLabel("Lanzar los dados para iniciar el juego");
textoinfo.setBounds(30,170,400,30);
contentPane.add(textoinfo);
setTitle("Deber Tres Programacion");
setVisible(true);
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void botonjugar_presionado( ActionEvent event)
{
if (chequear == true)
{
// puede contener CONTINUE, WON or LOST
sumOfDice = rollDice(); // primera vez que se lanzan los dados
System.out.println("Inicio");
System.out.println(Integer.toString(sumOfDice));
chequear = false;
switch (sumOfDice)
{
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
System.out.println("it got here 7,11");
gameStatus = "WON";
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus = "LOST";
System.out.println("it got 2,3,12");
case 4: // did not win or lose, so remember point
case 5:
case 6:
case 8:
case 9:
case 10:
gameStatus = "CONTINUE"; // game is not over
System.out.println("it got here 4,5,6,8,9,10");
myPoint = sumOfDice; // remember the point
prueba = Integer.toString(myPoint);
caja4.setText(prueba);
textoinfo.setText("Lanzar los dados para continuar el juego");
System.out.println(Integer.toString(myPoint));
break;
}
}
//while game is not complete
if (gameStatus.equals("CONTINUE"))
{
sumOfDice = rollDice(); //roll dice again
// determine game status
if ( sumOfDice == myPoint ) // win by making point
gameStatus = "WON";
else
if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
gameStatus = "LOST";
} // end while
// display won or lost message
if( gameStatus.equals("WON"))
{
textoinfo.setText("El jugador Gana.Lanzar los dados para iniciar otro juego");//System.out.println( "Player wins" );
caja4.setText("");
chequear = true;
}
else if (gameStatus == "LOST")
{
textoinfo.setText("El jugador Pierde.Lanzar los dados para iniciar otro juego");//System.out.println( "Player loses" );
caja4.setText("");
chequear = true;
}
} // end main
public static int rollDice()
{
//pick random die values
int die1 = 1 + randomNumbers.nextInt(6);// Primer dado tirado
int die2 = 1 + randomNumbers.nextInt(6);// segundo dado tirado
caja1.setText(Integer.toString(die1));
caja2.setText(Integer.toString(die2));
int sum = die1 + die2;
caja3.setText(Integer.toString(sum));
return sum;
}
public static void main(String[] args)
{
//Create and set up the window.
new HelloWorld();
}
}
答案 0 :(得分:1)
你必须改变所有你在比较两个字符串的if:
if (gameStatus == "CONTINUE")
到此:
if (gameStatus.equals("CONTINUE"))
您必须记住,为了比较两个Strings
,您必须使用.equals()
函数,而不是==
。
我希望它会对你有所帮助!
答案 1 :(得分:1)
在您的代码中,您在rollDice()
中再次致电if (gameStatus == "CONTINUE")
实际上再次滚动骰子用户将再次点击按钮,整个过程将重复。你不需要在那里再打电话。
只需从那里删除rollDice()
即可正常运行。
即。
//while game is not complete
if (gameStatus == "CONTINUE") {
sumOfDice = rollDice(); //roll dice again !!! remove this line !!!
// determine game status
if (sumOfDice == myPoint) // win by making point
{
....
将此代码修改为此
//while game is not complete
if (gameStatus == "CONTINUE") {
// determine game status
if (sumOfDice == myPoint) // win by making point
{
....