D neezy wrote:Hi all!
I'm taking my first stab at Java
That's very helpfulGary Chike wrote:. . . I took the liberty of adding the requisite libraries, adding an outer class and a main, etc...
jshell
| Welcome to JShell -- Version 21.0.1
| For an introduction type: /help intro
jshell> import javax.swing.JOptionPane;
jshell> Random random = new Random();
...> int randomNumber = random.nextInt( 101 );
...> boolean userCorrect = false;
...> String userInputString;
...> int userGuessedNumber;
...>
...> while( !userCorrect ) {
...> userInputString = JOptionPane.showInputDialog(
...> "Guess the number: ");
...> userGuessedNumber = Integer.parseInt( userInputString);
...> if( userGuessedNumber < randomNumber ) {
...> JOptionPane.showMessageDialog( null,
...> "(" + randomNumber +
...> ")Too,high,please try again" );
...> } else if ( userGuessedNumber < randomNumber ){
...> JOptionPane.showMessageDialog( null,
...> "(" + randomNumber +
...> ") Too low, please try again: ");
...> } else {
...> JOptionPane.showMessageDialog( null,
...> "(" + randomNumber +
...> ") Great Job, you guessed the number. ");
...> userCorrect = true;
...> }
...> }
random ==> java.util.Random@4fca772d
randomNumber ==> 4
userCorrect ==> false
userInputString ==> null
userGuessedNumber ==> 0
... etc ...
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |