How to open this program on a window?

2019-09-20 01:25发布

问题:

Can anyone help me with my if and else statements? It's only saying "Exit Goodbye" whenever I input something, which should only happen when I enter -0. My teacher is gone for the week, so I don't have anyone to ask for help.

package game;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;

import javax.swing.JOptionPane;

public class GameFrame {

/**
 * @param args
 */
public static void main(String[] args) {
     // num1 - Variable to store the first value
       // num2 - Variable to store the second value
       // answer - Variable to accept user input
        int num1, num2, answer=0;

        /*@reader - The reader which accepts user input*/
        BufferedReader reader = new BufferedReader (new     InputStreamReader(System.in));
        /*@quit - Variable used to exit the program*/
        boolean quit = false;
        /*@generator - The Random number generator*/
        Random generator = new Random();

        while (quit == false)
        {
          //Generate First Random Number between 1-100
          num1 = generator.nextInt(100);
          //Generate First Random Number between 1-100
          num2 = generator.nextInt(100);
          //Displays the math equation
        String input = JOptionPane.showInputDialog(null,num1+ "+" + num2 + " = ");
          //Accepts the user's input and converts it to int value
          int number = Integer.parseInt(input);
          //Lets assume if user enters -99, it means they want to exit the program
          if (answer == -0)
          {
          JOptionPane.showMessageDialog(null, "Exit Program: Good Bye!\n");
              quit = true;
          }else if (answer == (num1+num2))
              JOptionPane.showMessageDialog(null,"Correct Answer!\n");
          else{
              JOptionPane.showMessageDialog(null,"Incorrect Answer\n");
    }
}
 }
}

回答1:

Trying to transfer a console program to a GUI program is not an easy task if you've never made a GUI program before. You need to learn about event-driven programming. I suggest you have a look at the Swing tutorials

Some tips though. It you want a "semi-gui" program. You can just use JOptionPanes for the input. Say you want to get an number input. You would do something like this

String numberString = JOptionPane.showInputDialog(null, "Enter a Number");
int number = Integer.parseInteger(numberString);

once you do the first line, an input pane automatically pops up. asking for an input. The result is a String, so you have to parse it to get a number.

Also if you just want to diplay a message, just use

JOptionPane.showMessageDialog(null, message);

You can do that do display some result. In the above case when you just want to show a message, you don't need to make it equal to anything. So instead of the System.out.println()s, you can just use the JOPtionpane.showMesageDialog() and instead of reader.readLine(), you would use JOptionPane.showInputDialog()

Try it out and come back if you're stuck.


Also see the documentation for JOptionPane to see what other possible popup dialogs there are.



回答2:

Try This this is working am run on my computer

  /**
  *
 * @author sandeepk
 */
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.util.Random;

  import javax.swing.JOptionPane;

  public class GameFrame {

/**
 * @param args
 */
public static void main(String[] args) {
 // num1 - Variable to store the first value
    // num2 - Variable to store the second value
    // answer - Variable to accept user input
    int num1, num2, answer = 0;

    /*@reader - The reader which accepts user input*/
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    /*@quit - Variable used to exit the program*/
    boolean quit = false;
    /*@generator - The Random number generator*/
    Random generator = new Random();

    while (quit == false) {
        //Generate First Random Number between 1-100
        num1 = generator.nextInt(100);
        //Generate First Random Number between 1-100
        num2 = generator.nextInt(100);
        //Displays the math equation
        String input = JOptionPane.showInputDialog(null, num1 + "+" + num2 + " = ");
        //Accepts the user's input and converts it to int value
        int number = Integer.parseInt(input);
        //Lets assume if user enters -99, it means they want to exit the program
        System.out.println("number  " + number);
        if (number == 0) {
            JOptionPane.showMessageDialog(null, "Exit Program: Good Bye!\n");
            quit = true;
        } else if (number == (num1 + num2)) {
            JOptionPane.showMessageDialog(null, "Correct Answer!\n");
        } else {
            JOptionPane.showMessageDialog(null, "Incorrect Answer\n");
        }
    }
}
 }



 if my code is run and you satisfy then please vote me thanks.