P. 1
mid prog

mid prog

|Views: 12|Likes:
Published by Nerissa Borja

More info:

Published by: Nerissa Borja on Sep 05, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/05/2010

pdf

text

original

The following program randomly generates an integer greater than or equal to 0 and less than 100.

The program then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message. Otherwise, the program checks whether the guessed number is less than the random number. If the guessed number is less that the random number generated by the program, the program outputs the message, “Your guess is lower than the number”; otherwise, the program outputs the message, “Your guess is higher than the number”. The program then prompts the user to enter another number. The user is prompted to guess the random number until the user enters the correct number. The program uses the method random of the class Math to generate a random number. To be specific, the expression: Math.random()returns a type double greater than or equal to 0.0 and less than 1.0. To convert it to an integer greater than or equal to 0 and less than 100, the program uses the following expression: (int) (Math.random() * 100); Furthermore, the program uses the boolean visible done to control the loop. The boolean variable done is initialized to false. It is set to true when the user guesses the correct number. //Sample program guessing the number game import java.util.*; public class SampleProgram{ static Scanner console = new Scanner(System.in); public static void main (String[] args){ //declare the variable int num; //variable to store the random number int guess; //variable to store the number guessed by the user boolean done; //boolean variable to control the loop //Line1 //Line2

num = (int) (Math.random() * 100); done = false;

while(!done) //Line3 { //Line4 System.out.println(“Enter an integer greater than or equal to 0 and less than 100: ”); //Line5 guess = console.nextInt(); //Line6 System.out.println(); //Line7 if(guess==num) //Line8 { //Line9 System.out.println(“You guessed the correct number.”); //Line10 done=true; //Line11 } //Line12 else if (guess < num){ //Line13 System.out.println(“Your guess is lower than the number.\n Guess again.”); }else //Line15 System.out.println(“Your guess is higher than the number.\n Guess again.”); }//end while } }

//Line14 //Line16 //Line17 //Line18

. If the expression in the if statement in Line13 evaluates to false. The control then goes back to Line3. in this case. starting at Line13 and ending at Line16. Enter an integer greater than or equal to 0 and less than 100: 5 Your guess is lower than the number. Guess again. Because done is true. then the statement in Line16 executes. Enter an integer greater than or equal to 0 and less than 100: 6 Your guess is lower than the number. Enter an integer greater than or equal to 0 and less than 100: 7 You guessed the correct number. that is. The expression in the if statement in Line8 determines whether the value of guess is the same as num. The preceding program works as follows: The statement in Line1 creates an integer greater than or equal to 0 and less than 100 and stores this number in the variable num. !done is false and the while loop terminates. Guess again. . If the value of guess is the same as num. The statement part of this else is an if.else statement. The while loop starts at Line3 and ends at Line17. Guess again. The statement in Line5 prompts the user to enter an integer greater than or equal to 0 and less than 100. The program then prompts the user to enter an integer greater than or equal to 0 and less than 100. The if statement in Line13 determines whether the value of guess is less than num. then the else statement in Line13 executes. Enter an integer greater than or equal to 0 and less than 100: 10 Your guess is higher than the number. Enter an integer greater than or equal to 0 and less than 100: 8 Your guess is higher than the number. which outputs the message: Your guess is higher than the number. The statement in Line2 sets the boolean variable done to false. The statement in Line6 stores the number entered by the user in the variable guess. If the expression in Line8 evaluates to false. Guess again. then !done is true and the body of the while loop executes. then the statements in Lines 10 and 11 execute. Guess again. if done is true. then !done is false. so the while loop terminates. if the user guessed the number correctly. the statement in Line14 outputs the message: Your guess is lower than the number.Sample runs: Enter an integer greater than or equal to 0 and less than 100: 25 Your guess is higher than the number. Guess again. Guess again. The expression in the while loop at Line3 evaluates the expression !done. The statement in Line11 sets the variable done to true. if done is false. The statement in Line 10 outputs the message: You guessed the correct number.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->