如何在窗口上打开该程序? [英] How to open this program on a window?

查看:73
本文介绍了如何在窗口上打开该程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用if和else语句帮助我吗?每当输入内容时,它只会说退出再见",只有在输入-0时才会发生.我的老师缺席了一周,所以我没有任何人可以寻求帮助.

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

推荐答案

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

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

一些提示.如果您要"semi-gui"程序.您可以仅使用JOptionPane s作为输入.假设您要输入数字.你会做这样的事情

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

您可以这样做以显示一些结果.在上述情况下,当您只想显示一条消息时,无需使其等于任何内容.因此,可以使用JOPtionpane.showMesageDialog()代替System.out.println(),而可以使用JOptionPane.showInputDialog()

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.

另请参见 JOptionPane的文档看看还有其他可能的弹出对话框.

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

这篇关于如何在窗口上打开该程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆