【问题标题】:How to open this program on a window?如何在窗口中打开这个程序?
【发布时间】:2014-01-28 07:06:20
【问题描述】:

谁能帮助我处理 if 和 else 语句?每当我输入某些内容时,它只会说“退出再见”,这只会在我输入 -0 时发生。我的老师这周不在,所以我没有人可以寻求帮助。

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");
    }
}
 }
}

【问题讨论】:

  • 如果您以前从未制作过 GUI 程序,那么尝试将控制台程序转换为 GUI 程序并不是一件容易的事。您需要了解事件驱动编程。我建议你看看Swing tutorials
  • 要记住的一点是,当您编辑问题(帖子)时,不要覆盖原始问题,因为这可能会使编辑之前回答的答案无效。相反,在原始帖子下方,只需写 EDIT 并将编辑后的内容附加到帖子中。

标签: java swing jframe jlabel joptionpane


【解决方案1】:

试试这是在我的电脑上运行的工作

  /**
  *
 * @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.

【讨论】:

    【解决方案2】:

    如果您以前从未制作过 GUI 程序,那么尝试将控制台程序转换为 GUI 程序并不是一件容易的事。您需要了解事件驱动编程。我建议你看看Swing tutorials

    虽然有一些提示。你想要一个“半gui”程序。您可以只使用JOptionPanes 作为输入。假设您想输入数字。你会做这样的事情

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

    完成第一行后,会自动弹出一个输入窗格。要求输入。结果是一个字符串,因此您必须对其进行解析以获取一个数字。

    另外,如果您只想显示一条消息,只需使用

    JOptionPane.showMessageDialog(null, message);
    

    你可以这样做显示一些结果。在上述情况下,当您只想显示一条消息时,您不需要使其等于任何东西。因此,您可以使用JOPtionpane.showMesageDialog() 而不是System.out.println()s,而不是reader.readLine(),您可以使用JOptionPane.showInputDialog()

    尝试一下,如果遇到困难就回来。


    另请参阅documentation for JOptionPane 以了解还有哪些其他可能的弹出对话框。

    【讨论】:

    • 谢谢老兄。它部分工作,但现在每当我输入一些东西时,结果只是说“退出!再见!”,我的 if 语句之一。但这只有在我输入 -0 时才会发生
    • while (quit == false) { //生成1-100之间的第一个随机数 num1 = generator.nextInt(100); //生成1-100之间的第一个随机数 num2 = generator.nextInt(100); //显示数学方程 String input = JOptionPane.showInputDialog(null,num1+ "+" + num2 + " = "); //接受用户输入并将其转换为int值 int number = Integer.parseInt(input); //假设如果用户输入-99,这意味着他们想退出程序
    • if (answer == -0) { JOptionPane.showMessageDialog(null, "退出程序:再见!\n");退出=真; }else if (answer == (num1+num2)) JOptionPane.showMessageDialog(null,"正确答案!\n"); else{ JOptionPane.showMessageDialog(null,"Incorrect Answer\n");
    • 不用担心。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多