int n2 = (int) (Math.random() * 3+1); //generates numbers 1-3 only
GUESSING GAME
#1 Guess the random number from 1-3 only.
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct'
- ask the user to Try Again (recursion)
input - validates user's input (1-3 only) (apply recursion)
solution:
public static void main(String args[])
{ guess( );
}
static void guess( )
{ System.out.println("Input 1-3 only");
int u = input( );
int r = (int) (Math.random() * 3+1);
System.out.println("Random number is "+r);
if (u == r)
System.out.println("Correct");
else
System.out.println("Not Correct");
System.out.println("Try Again? Press 'Y' to continue.");
char ch = sc.next( ).toUpperCase( ).charAt(0);
if (ch == 'Y')
guess( );
else
return;
}
static int input( )
{ int u = sc.nextInt( );
if (u<1 || u>3)
input( );
else
return u;
}
#2 Scoring the guessing game
methods:
main - calls method guess
guess - generates a random number
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (1-3 only) (apply recursion for invalid input)
HIGH/LOW
methods:
main - calls method highlow
guess - generates a random number from 0-10 only
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (0-1 only, 0 means below 6, 1 means above 5 ) (apply recursion for invalid input)
guess - generates a random number from 0-10 only
- accepts user's input
- determines if the user's guess is 'correct' or 'not correct', update the score if 'correct'
- ask the user to Try Again (apply recursion),
- before exit, display the total score/the number of tries
input - validates user's input (0-1 only, 0 means below 6, 1 means above 5 ) (apply recursion for invalid input)
Walang komento:
Mag-post ng isang Komento