变量在不是Java时充当指针

变量在不是Java时充当指针,java,Java,我有一个java应用程序,它模拟“垃圾”游戏。 在应用程序中,我有一个方法rollDice(),它返回两个随机数之和。我还有一个按钮,当它点击时调用一个名为botonjugar_presionado(ActionEvent)的方法 关于botonjugar_perionado假设发生的休闲方法 调用方法rollDice(),并将其返回值存储在sumOfDice上 交换机结构使用sumOfDice值根据sumOfDice值执行不同的操作 当我点击按钮时,我的应用程序并不像它想象的那样工作。因此,在

我有一个java应用程序,它模拟“垃圾”游戏。 在应用程序中,我有一个方法rollDice(),它返回两个随机数之和。我还有一个按钮,当它点击时调用一个名为botonjugar_presionado(ActionEvent)的方法

关于botonjugar_perionado假设发生的休闲方法

  • 调用方法rollDice(),并将其返回值存储在sumOfDice上
  • 交换机结构使用sumOfDice值根据sumOfDice值执行不同的操作
  • 当我点击按钮时,我的应用程序并不像它想象的那样工作。因此,在尝试调试时,我使用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(); 
    
    }  
    }
    

    您必须更改比较两个字符串的所有if,如下所示:

    if (gameStatus == "CONTINUE")
    
    为此:

    if (gameStatus.equals("CONTINUE"))
    
    您必须记住,要比较两个
    字符串,必须使用
    .equals()
    函数,而不是
    =


    我希望它会对你有帮助

    在您的代码中,您将在
    if(gameStatus==“CONTINUE”)
    中再次调用
    rollDice()
    实际上,若要再次掷骰子,用户将再次单击按钮,整个过程将重复。你不需要在那里再打电话

    只要从那里移除
    rollDice()
    ,它就可以正常运行

    i、 e

    将此代码修改为

    //while game is not complete
    if (gameStatus == "CONTINUE") {
        // determine game status
        if (sumOfDice == myPoint) // win by making point
        {
    ....
    

    我仍然看到与中相同的错误,例如将字符串与
    ==
    而不是
    equals()
    进行比较,并且在
    开关中没有
    break
    语句。我只是在这里编辑的。但是你知道为什么会这样吗。这太奇怪了,太令人沮丧了,我想不出来。不管怎样,谢谢你的再次帮助。如果你能投入一点精力,那就太棒了。如果你的问题是你的方法的结果的
    System.out.println()
    正在做一些有趣的事情,那么你应该给我们的就是这个方法和一个简单的
    main()
    来演示这个问题。211行代码对于您声称存在的问题来说太过分了,不是吗?并且在格式化方面也付出了努力。让我不想读任何东西。你有这个
    sumOfDice=rollDice()在代码中重复两次。在这个
    if
    语句中
    if(chequear==true)
    然后在这个
    if
    语句中
    if(gameStatus.equals(“CONTINUE”)
    。尝试在两个
    if
    s外部执行一次,然后使用变量
    sumOfDice
    两次。
    //while game is not complete
    if (gameStatus == "CONTINUE") {
        // determine game status
        if (sumOfDice == myPoint) // win by making point
        {
    ....