473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Guessing a number help.

KoreyAusTex
I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in the main method after the method calls. This is not the way I was supposed to do it, any thoughts?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class GuessNumber 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.        Scanner read = new Scanner(System.in);
  7.        System.out.print("Do you want to play a game? ");//War Games!
  8.        String collegeTry = read.next();
  9.        collegeTry = collegeTry.toLowerCase();
  10.        while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
  11.        {
  12.            game();
  13.            break;
  14.        }
  15.     }
  16.  
  17.     public static void game() 
  18.     {
  19.         Scanner read = new Scanner(System.in);
  20.         Random generator = new Random();
  21.         int randNum = generator.nextInt(100) + 1; // pick a number between 1 & 100
  22.         System.out.println("\nWelcome to the High-Low Guessing Game!");
  23.         System.out.println("Try to guess the number I've chosen between 1 and 100.");
  24.         System.out.print("\nWhat is your guess? ");
  25.         int guessNum = read.nextInt();
  26.         int count = 1;
  27.  
  28.         while (guessNum != randNum) 
  29.         {
  30.             if (guessNum > randNum)
  31.             {
  32.                 System.out.println(guessNum + " is too high.");
  33.             }
  34.             else
  35.             {
  36.                 System.out.println(guessNum + " is too low.");
  37.             }
  38.             count++;
  39.             System.out.print("What is your guess? ");
  40.             guessNum = read.nextInt();
  41.         }
  42.  
  43.         System.out.println("\nCorrect! You only needed " + count + " guesses to get it!");    
  44.     }
  45.  
  46. }
  47.  
Also, can this be done using a for loop? As always I appreciate your advice and guidance!
Mar 5 '08 #1
9 3053
JosAH
11,448 Expert 8TB
Look at your main() method: you only read collegeTry once before the while loop
starts and it keeps its value (it is never changed), so if the value is one of the
affirmative values the while loop keeps on looping.

kind regards,

Jos
Mar 5 '08 #2
Look at your main() method: you only read collegeTry once before the while loop
starts and it keeps its value (it is never changed), so if the value is one of the
affirmative values the while loop keeps on looping.

kind regards,

Jos
Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class GuessNumber 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.        Scanner read = new Scanner(System.in);
  7.        System.out.print("Do you want to play a game? ");//War Games!
  8.        String collegeTry = read.next();
  9.        collegeTry = collegeTry.toLowerCase();
  10.        while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
  11.        {
  12.            game();
  13.            System.out.print("\nDo you want to play a game? ");
  14.            collegeTry = read.next();
  15.            collegeTry = collegeTry.toLowerCase();
  16.        }
  17.     }
  18.  
  19.     public static void game() 
  20.     {
  21.         Scanner read = new Scanner(System.in);
  22.         Random gen = new Random();
  23.         int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
  24.         System.out.println("\nWelcome to the High-Low Guessing Game!" + 
  25.         "Try to guess the number I've chosen between 1 and 100.");
  26.         System.out.print("\nWhat is your guess? ");
  27.         int guessNum = read.nextInt();
  28.         int count = 1;
  29.  
  30.         while (guessNum != randNum) 
  31.         {
  32.             if(guessNum > randNum)
  33.             {
  34.                 System.out.println(guessNum + " is too high.");
  35.             }
  36.             else if(guessNum < randNum)
  37.             {
  38.                 System.out.println(guessNum + " is too low.");
  39.             }
  40.             else if(guessNum == -1)
  41.             {
  42.                 System.out.println("You stopped the game after " + count + " guess(es)!");
  43.             }
  44.  
  45.             count++;
  46.             System.out.print("What is your guess? ");
  47.             guessNum = read.nextInt();
  48.         }
  49.  
  50.         System.out.println("Correct! You only needed " + count + " guesses to get it!");    
  51.     }
  52.  
  53. }
  54.  
Mar 6 '08 #3
Try using

try{

} catch

block
Mar 7 '08 #4
Try using

try{

} catch

block
Actually you are using concepts we have yet to learn
Mar 7 '08 #5
nomad
664 Expert 512MB
Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class GuessNumber 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.        Scanner read = new Scanner(System.in);
  7.        System.out.print("Do you want to play a game? ");//War Games!
  8.        String collegeTry = read.next();
  9.        collegeTry = collegeTry.toLowerCase();
  10.        while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
  11.        {
  12.            game();
  13.            System.out.print("\nDo you want to play a game? ");
  14.            collegeTry = read.next();
  15.            collegeTry = collegeTry.toLowerCase();
  16.        }
  17.     }
  18.  
  19.     public static void game() 
  20.     {
  21.         Scanner read = new Scanner(System.in);
  22.         Random gen = new Random();
  23.         int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
  24.         System.out.println("\nWelcome to the High-Low Guessing Game!" + 
  25.         "Try to guess the number I've chosen between 1 and 100.");
  26.         System.out.print("\nWhat is your guess? ");
  27.         int guessNum = read.nextInt();
  28.         int count = 1;
  29.  
  30.         while (guessNum != randNum) 
  31.         {
  32.             if(guessNum > randNum)
  33.             {
  34.                 System.out.println(guessNum + " is too high.");
  35.             }
  36.             else if(guessNum < randNum)
  37.             {
  38.                 System.out.println(guessNum + " is too low.");
  39.             }
  40.             else if(guessNum == -1)
  41.             {
  42.                 System.out.println("You stopped the game after " + count + " guess(es)!");
  43.             }
  44.  
  45.             count++;
  46.             System.out.print("What is your guess? ");
  47.             guessNum = read.nextInt();
  48.         }
  49.  
  50.         System.out.println("Correct! You only needed " + count + " guesses to get it!");    
  51.     }
  52.  
  53. }
  54.  
On the part:
Do you want to play a game?
you might want to print on the screen enter yeah, sure or ok. If you don't that person will never get in.
Mar 7 '08 #6
nomad
664 Expert 512MB
Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class GuessNumber 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.        Scanner read = new Scanner(System.in);
  7.        System.out.print("Do you want to play a game? ");//War Games!
  8.        String collegeTry = read.next();
  9.        collegeTry = collegeTry.toLowerCase();
  10.        while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
  11.        {
  12.            game();
  13.            System.out.print("\nDo you want to play a game? ");
  14.            collegeTry = read.next();
  15.            collegeTry = collegeTry.toLowerCase();
  16.        }
  17.     }
  18.  
  19.     public static void game() 
  20.     {
  21.         Scanner read = new Scanner(System.in);
  22.         Random gen = new Random();
  23.         int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
  24.         System.out.println("\nWelcome to the High-Low Guessing Game!" + 
  25.         "Try to guess the number I've chosen between 1 and 100.");
  26.         System.out.print("\nWhat is your guess? ");
  27.         int guessNum = read.nextInt();
  28.         int count = 1;
  29.  
  30.         while (guessNum != randNum) 
  31.         {
  32.             if(guessNum > randNum)
  33.             {
  34.                 System.out.println(guessNum + " is too high.");
  35.             }
  36.             else if(guessNum < randNum)
  37.             {
  38.                 System.out.println(guessNum + " is too low.");
  39.             }
  40.             else if(guessNum == -1)
  41.             {
  42.                 System.out.println("You stopped the game after " + count + " guess(es)!");
  43.             }
  44.  
  45.             count++;
  46.             System.out.print("What is your guess? ");
  47.             guessNum = read.nextInt();
  48.         }
  49.  
  50.         System.out.println("Correct! You only needed " + count + " guesses to get it!");    
  51.     }
  52.  
  53. }
  54.  
try this instead...
if(guessNum == -1);

also you will need to stop the program some how after you type -1
and move this
System.out.println("Correct! You only needed " + count + " guesses to get it!");
somewhere...


Do you know why?


good luck


nomad
Mar 7 '08 #7
try this instead...
if(guessNum == -1);

also you will need to stop the program some how after you type -1
and move this
System.out.println("Correct! You only needed " + count + " guesses to get it!");
somewhere...


Do you know why?


good luck


nomad
Do I know why what? That I have to move it?
Mar 7 '08 #8
nomad
664 Expert 512MB
Do I know why what? That I have to move it?

why you need to do this
if(guessNum == -1);
in stead of this
else if(guessNum == -1);


Did you do a test on your program. I think it's still not right.
What do you need to move is for you to figure out.


I probably should have made you figure it out by giving you hints.

nomad
Mar 7 '08 #9
why you need to do this
if(guessNum == -1);
in stead of this
else if(guessNum == -1);


Did you do a test on your program. I think it's still not right.
What do you need to move is for you to figure out.


I probably should have made you figure it out by giving you hints.

nomad
Sorry I think you misunderstood me, I got confused, I understand I need to move it out, but I wasn't clear on what you were referring too.
Mar 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
4
by: moopower | last post by:
I have to do the following: For this project, you are to create a simple number guessing game. The program should begin by asking the user for a range (i.e. a minimum number and a maximum number)...
1
by: pramodmc4u | last post by:
hi, please help me,,,,,, a newbie.......send me code for a programme in which the computer tell u the number that u guessed............
8
by: halo combat22 | last post by:
I am looking on some help on a program where i have the user enter a random number from say 1-20. I do it until number is chosen. Meanwhile it tells the user it it has gotten cooler or warmer from...
0
by: Dan Upton | last post by:
On Wed, May 21, 2008 at 12:42 PM, garywood <woodygar@sky.comwrote: How about you check tries 10 in the condition for your while loop, and then after the loop you use the value of tries to decide...
7
by: dseto200 | last post by:
I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? # Guess My Number # The computer picks a random number ...
6
by: falconsx23 | last post by:
Hi, I am making a guessing game from the java language. The user is asked to input a number. The number is 1 through a random max number. If the number guessed is correct, then the program should...
3
by: sbdalecia | last post by:
In C++ I have to create a guessing game that has three loops and and I have to make a flowchart. Now mind you I am a beginner at all of this. Someone help!
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.