codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Need little help with a while loop  gosi at 21:58 on Monday, October 31, 2005
 

Hi, I have another schript which I hope someone can help me with, it's just a minor problem. This is a Hi-low guessing game that continues until you have guessed the correct number, and then count how many tries you make. The only thing that don't work is that after you have guessed a right number you have the opportunitiy to play again, and that problem I haven¨t been able to solve.

here is the code:

import java.util.Random;
import java.util.Scanner;

public class HiLoGame{
//
// Plays the Hi-Lo guessing game with the user
//
public static void main(String[] args) {

boolean done = true;
boolean playAgain = true;
Random numberGenerator = new Random();
int randomNumber;
int numberOfGuesses = 0;


Scanner scan = new Scanner(System.in);

randomNumber = numberGenerator.nextInt(100) + 1;


while (playAgain)
{
for(int i = 0; i<1; i++)
numberOfGuesses++;
final String guessMessage = "Enter your guess (0 to quit): ";
final String tooHigh = "Sorry, the number you entered is higher than my number.";
final String tooLow = "Sorry, the number you entered is lower than my number.";
final String justRight = "Congratulations! You guessed my number!";
final String playAgainMessage = "Play again?";

System.out.print(guessMessage);
String entry = scan.nextLine();

if(entry.equals ("0"))
break;

int userNumber = Integer.parseInt(entry);

if (userNumber > randomNumber)
System.out.println(tooHigh);
else if(userNumber < randomNumber)
System.out.println(tooLow);
else
{
{
System.out.println();
System.out.println(justRight);
playAgain = false;
System.out.println("Your total guess for the round was " + numberOfGuesses +" times.");
}
System.out.println(playAgainMessage);
entry = scan.nextLine();

if(entry.equalsIgnoreCase("n"))
done = false;
}


}
}}

  Re: Need little help with a while loop  crwood at 00:38 on Tuesday, November 01, 2005
 


import java.util.Random;
import java.util.Scanner;

public class HLG {
//
// Plays the Hi-Lo guessing game with the user
//
public static void main(String[] args) {
String guessMessage = "Enter your guess (0 to quit): ";
String tooHigh = "Sorry, the number you entered is higher than my number.";
String tooLow = "Sorry, the number you entered is lower than my number.";
String justRight = "Congratulations! You guessed my number!";
String playAgainMessage = "Play again? <'y' 'n'>";
// boolean done = true;
boolean playAgain = true;
Random numberGenerator = new Random();
// int randomNumber;
int numberOfGuesses = 0;

Scanner scan = new Scanner(System.in);

int randomNumber = numberGenerator.nextInt(100) + 1;

while (playAgain) {
// for(int i = 0; i<1; i++)
numberOfGuesses++;

System.out.print(guessMessage);
String entry = scan.nextLine();

if(entry.equals ("0"))
break;

int userNumber = Integer.parseInt(entry);

if (userNumber > randomNumber)
System.out.println(tooHigh);
else if(userNumber < randomNumber)
System.out.println(tooLow);
else // success
{
System.out.println();
System.out.println(justRight);
playAgain = false;
System.out.println("Your total guess for the round was " +
numberOfGuesses +" times.");

System.out.println(playAgainMessage);
entry = scan.nextLine();
if(entry.equalsIgnoreCase("y"))
{
randomNumber = numberGenerator.nextInt(101);
playAgain = true;
numberOfGuesses = 0;
}
else // exit program...
scan.close();
}
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005