我正在尝试理解和学习如何从一个简单的ifBD输入到使用JOption输入/消息功能。原始代码如下:
导入java.util.Scanner;
公共课Guess2 {
public static void main(String[] args)
{
//keyboard scanner
Scanner keyBd = new Scanner( System.in );
//declare variables
int myGuess;
final int NUMBER_TO_GUESS = 13;
//show introductory message
System.out.print("\nWelcome to Guess-My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");
//prompt to enter guess
System.out.print("Enter a number between 1 and 25: ");
myGuess = keyBd.nextInt();
while( myGuess != NUMBER_TO_GUESS)
{
//good or bad guess??
if(myGuess < NUMBER_TO_GUESS) //too low
System.out.printf("Your guess [ %d ] is too low...\nTry Again!\n", myGuess);
else //too high
System.out.printf("Your guess [ %d ] is too high...\nTry Again!\n", myGuess);
//prompt to enter guess
System.out.print("\nEnter a number between 1 and 25: ");
myGuess = keyBd.nextInt();
}//end while
//good guess
System.out.printf(
"Your guess [ %d ] is the number...\nCongratulations!\n",
myGuess);
}//end main()
} //结束猜测1
我需要用JOptionPane替换keyBD输入,并用JOptionPane替换打印输出。我也知道,输入的任何内容都作为字符串完成,必须转换为int。我想我已经接近了,但是我不知道该转换语句。这是我更新的代码
import javax.swing.JOptionPane;
公共课Guess2 {
public static void main(String[] args)
{
//declare variables
final int NUMBER_TO_GUESS = 13;
//show introductory message
JOptionPane.showMessageDialog(null, "\nWelcome to Guess-My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");
//prompt to enter guess
JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
int myGuess = nextInteger.parseInt(myGuess);
while( myGuess != NUMBER_TO_GUESS)
{
//good or bad guess??
if(myGuess < NUMBER_TO_GUESS)
//too low
JOptionPane.showMessageDialog(null, "Your guess [ %d ] is too low...\nTry Again!\n", myGuess);
else//too high
JOptionPane.showMessageDialog(null, "Your guess [ %d ] is too high...\nTry Again!\n", myGuess);
//prompt to enter guess
JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
int myGuess = nextInteger.parseInt(myGuess);
}//end while
//good guess
JOptionPane.showMessageDialog(null, "Your guess [ %d ] is the number...\nCongratulations!\n", myGuess);
}
}
答案 0 :(得分:0)
JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
int myGuess = nextInteger.parseInt(myGuess);
如何转换“ myGuess”?您是从哪里获得初始“ myGuess”值的?
好答案是您可以从选项窗格中获得答案。阅读JOPtionPane
API,以获取有关如何使用showInputDialog(...)
方法的信息。您将看到该方法返回一个值。因此,您需要将String转换为int:
String answer = (String)JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
int myGuess = nextInteger.parseInt(answer);
阅读How to Make Dialogs的Swing教程中的部分,以获取更多信息和工作示例。
答案 1 :(得分:0)
这是我最终为了编译和执行的代码。作为Java的新手,如果您提出的问题将来可能会导致我出现问题或不是最佳实践,那么我会对反馈感兴趣。谢谢。
import javax.swing.JOptionPane;
公共课Guess2 {
public static void main(String[] args)
{
//declare variables
String answer;
int myGuess;
final int NUMBER_TO_GUESS = 13;
//show introductory message
JOptionPane.showMessageDialog(null, "\nWelcome to Guess-My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");
//prompt to enter guess
answer = JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
myGuess = Integer.parseInt(answer);
while( myGuess != NUMBER_TO_GUESS)
{
//good or bad guess??
if(myGuess < NUMBER_TO_GUESS)
//too low
JOptionPane.showMessageDialog(null, "Your guess is too low...\nTry Again!\n");
else//too high
JOptionPane.showMessageDialog(null, "Your guess is too high...\nTry Again!\n");
//prompt to enter guess
answer = JOptionPane.showInputDialog(null, "Enter a number between 1 and 25: ");
myGuess = Integer.parseInt(answer);
}//end while
//good guess
JOptionPane.showMessageDialog(null, "Your guess is the number...\nCongratulations!\n");
}
}