/**
*
* Gregory B Shavers Jr
* CSC 225 - Online
* Lab 6
*/
import java.util.*;
public class GuessingGame
{
public static void main (String[] args)
{
double guess;
guess = 0 ;
double ans;
int num = 0;
Scanner scan = new Scanner(System.in);
do
{
num = (int)(1+Math.random()*100);
getGuess(guess);//runs getguess method
processGuess(guess, num);//runs process guess method
do
{
System.out.print( " Correct! ");
System.out.println( " You guess correctly guessed " + num);
System.out.print( " Play again? 1 for YES: 0 for NO: "); //ask user if he/she wants to play again
ans = scan.nextDouble();
System.out.println( " " );
}while(ans != 1 && ans != 0);
}while (ans == 1);
System.out.println( " Thanks for playing. : )" ); //tells user thanks for playing
}
public static double getGuess(double guess)
{
Scanner scan = new Scanner(System.in);
System.out.print( " Lets play a guesing game!" ); //tell user we are playing a game
System.out.print( " I'm thinking of a number between 1-100. Pick your number user! " ); //ask user to pick a number between 1-100
guess = scan.nextDouble();
return guess;
}
public static void processGuess ( double guess, double num)
{
Scanner scan = new Scanner(System.in);
int count = 1 ;
do //evaluate num and guess operation
{
if( guess < num)
{
System.out.println( " The number is larger. " + num);//tells user guess under num
System.out.print( " Please guess again. ");
guess = scan.nextDouble();
}
else if ( guess > num)
{
System.out.println( " The number is smaller. " + num );//tell user guess is over num
System.out.print( " Please guess again. ");
guess = scan.nextDouble();
}
/** else
{
System.out.println( " You are right. " );
System.out.println( " The number was " + num );//displays correct guess
System.out.println( " The number of guesses was " + count ); //display user guesses
*/
count++;
}while (guess !=num );
}
}
I know why my code was not working. And liked you guys said it was my logic.(Its hard for me to learn code with out really writing it) When I was creating if else statements I notice my do while loop was in conflict with my if else statements. What I wanted to know was if there was another better why of writing this loop. I use the do while loop cause it seems more logical to me then a while or for statement. Here is my output if you wanted to see what happens. Thanks Again everyone.
Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 60
The number is larger. 43.0
Please guess again. 44
The number is smaller. 43.0
Please guess again. 42
The number is larger. 43.0
Please guess again. 43
Correct! You guess correctly guessed 43
Play again? 1 for YES: 0 for NO: 1
Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 100
The number is larger. 34.0
Please guess again. 33
The number is larger. 34.0
Please guess again. 35
The number is smaller. 34.0
Please guess again. 34
Correct! You guess correctly guessed 34
Play again? 1 for YES: 0 for NO: 0
Thanks for playing. : )