The GuessProgram allows the user to input a guess number. The program will confirm the user whether his/her guess is correct. In this Java program, you learn to read a number from keyboard by using Scanner class, to generate random number by using Random class, to read a character from keyboard by using the read() method of the System.in class, to use conditional statement if else to make decision, and to use a while loop to repeat a block of code.
GuessProgram source code:
1. import java.util.Random;
2. import java.util.Scanner;
3. import java.io.IOException;
4. class GuessProgram{
5. public static void main(String[] args){
6. int ynum,rnum;
7. char con='y';
8. Scanner sc=new Scanner(System.in);
9. Random rn=new Random();
10. while(con=='y'){
11. System.out.print("Enter your number:");
12. ynum=sc.nextInt();
13. rnum=1+rn.nextInt(6);
14. if(ynum==rnum)
15. System.out.println("Correct!\n");
16. else
17. System.out.format("Incorrect! The correct number is %d.\n",rnum);
18. try{
19. System.out.print("Press y to continue or other key to exit:");
20. con=(char)System.in.read();
21. }catch(IOException ie){System.out.println("Error: Can not receive key input");}
22. }
23. }
24.}
Program Output
Enter your number:2
Incorrect! The correct number is 4.
Press y to continue or other key to exit: y
Incorrect! The correct number is 4.
Press y to continue or other key to exit: y
Code Explanation:
1. Include Random class to the program.
2. Include Scanner class to the program.
3. Include IOException class to the program.
4. Open GuessProgram class block.
5. Open the main method. The program begins from the main method.
6. Declare local variables ynum, and rnum to store a number input by user, and a number randomly generated.
7. Declare local con variable to store a 'y' character used to continue the program.
8. Create Scanner object sc.
9. Create Random object rn.
10. Open the while loop. The while is used to repeat a block of Java code. It exits when the condition is false.
The while loop general form is :
while(condition)
{
//code to do some thing
update
}
11. Tell the user to input a number.
12. Get the number from keyboard.
13. Get a random number (from 1 to 6).
14. Open if block to compare the number of user and the random number by using an if statement.
The general form of if statement is:
if(test)
//do something if the test result is true
else
//do something if the test result is false
If you want to do more than one test, you will use the alternative form below:
if(test1)
//do something if the test result is true
elseif(test2)
//do something
.............
else
//do something
If you have multiple statements in if or else block, you must put these statements in brackets({}).
15. Display "Correct!" message when the result of comparison is true.
16. open else block
17. Display "Incorrect! The correct number is..." when result of comparison is false.
18. Open try block. The read() method used to read a character from keyboard has to be placed in try block so that an error can be thrown.
19. Tell the user whether his/her wants to continues the program.
20. Read a character from keyboard by using read() method of System.in input stream class.
21. The catch block is to display the error message if any error was captured in the try block.
21. Close while loop block.
22. Close the main method block.
23. Close the GuessProgram class block.
No comments:
Post a Comment