任何人都可以帮助我处理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");
}
}
}
}
答案 0 :(得分:2)
如果您以前从未制作过GUI程序,那么尝试将控制台程序转移到GUI程序并不是一件容易的事。您需要了解事件驱动的编程。我建议您查看Swing tutorials
但有些提示。你想要一个“半gui”程序。您只需使用JOptionPane
s作为输入。假设你想得到一个数字输入。你会做这样的事情
String numberString = JOptionPane.showInputDialog(null, "Enter a Number");
int number = Integer.parseInteger(numberString);
执行第一行后,会自动弹出输入窗格。要求输入。结果是一个String,所以你有来解析它以得到一个数字。
此外,如果您只想 diplay 一条消息,请使用
JOptionPane.showMessageDialog(null, message);
你可以这样做,显示一些结果。在上述情况下,当您只想显示消息时,您不需要使其等于任何内容。因此,您可以使用System.out.println()
代替JOPtionpane.showMesageDialog()
而不是reader.readLine()
,而不是JOptionPane.showInputDialog()
,而是使用{{1}}
尝试一下,如果你被卡住了就回来。
另请参阅documentation for JOptionPane以查看其他可能的弹出对话框。
答案 1 :(得分:0)
尝试这是在我的电脑上运行
/**
*
* @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.