Я пытаюсь понять и узнать, как перейти от простого ввода keyBD к использованию функции ввода / сообщения JOption.Исходный код выглядит следующим образом:
import 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()
} // end Guess1
Мне нужно заменить входные данные keyBD на JOptionPane, а распечатки на 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);
}
}