我正在努力在JOptionPane中创建一个程序,其中计算机从0-999开始猜测用户编号然后与用户交互,最后猜测数字(或标识用户作弊)然后输出一个祝贺屏幕它的猜测和消息。
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuessMyNumber
{
public static void main(String[] args)
{
//Prepare
Object[] options1 = { "Yes", "No",
"I'm Thinking" };
JPanel Name = new JPanel();
Name.add(new JLabel("Do you want to play a game? \n"+"\n Please Enter Your Name"));
JTextField textField = new JTextField(10);
Name.add(textField);
int person = JOptionPane.showOptionDialog(null, Name, null,
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, null);
if (person == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Awesome! Nice to meet you,"+textField.getText()+",Select 'OK' so I can tell you the rules");
}
if (person == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Ok dude, take it easy! I was just asking, geez");
}
if (person == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null, "Hey dude, we do not have all day... HURRY UP");
}
//Instructions
else if (person==JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "OK "+textField.getText()+ "\nThe name of the game is for me to guess your number.\n"+"First and foremost, I need you to think of a number from 0 to 999.\n"+"I will then provide you with a number, you need to tell me if I am too LOW or too HIGH, or if I am RIGHT and guessed correctly.\n"+"Are you ready? If so, make sure you remember your number!", "RULES", JOptionPane.INFORMATION_MESSAGE);
}
//Game Start
Object[] options2 = { "Too LOW", "Too HIGH",
"You Are CORRECT" };
int result = ((999+0)/2);
int g1=JOptionPane.showOptionDialog(null, "Is your number "+result+"?", null,
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options2, null);
//Guess 1 Low
if (g1==JOptionPane.YES_OPTION)
{
int ga = ((999+result)/2);
int g1a=JOptionPane.showOptionDialog(null, "Is your number "+ga+"?", null,
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options2, null);
}
//Guess 1 High
else if (g1==JOptionPane.NO_OPTION)
{
int gb =((result+0)/2);
int g1b=JOptionPane.showOptionDialog(null, "Is your number "+gb+"?", null,
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options2, null);
}
else if (g1==JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null, "Awesome "+textField.getText()+ ", this was fun!\n"+"For a second there I really thought you had me stumped!\n"+"If you would like to play again, please restart the program.\n"+"Thank you for Playing!", " I GUESSED YOUR NUMBER!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
//Guess 2
}
}