Java Guessing Game Option to Try Again
#1
guessing game with play again option using only for loop in coffee
Posted 26 Oct 2012 - 03:58 AM
//i am new in java i accept been learning for near 2 months but my assignment was to program a guessing game where the reckoner randomly chooses a number from 1 to 100 and it gives y'all hints higher or lower and afterward you lot gauge it correctly it tells you lot in how many guesses and after that it says "play again" and if you enter anything except 'y' then information technology should just say bye but if you enter 'y' then the game plays once more. information technology has to utilize the for loop. my trouble is that afterwards the play over again question when i dont enter 'y' it outputs "adieu" and "it took you lot # guesses". information technology should just say "cheerio" and end the programme. ive been working for hours and i dont know what i am doing incorrect. any aid would be great. thank you!
import coffee.util.Scanner;
import java.util.Random;
public course forloop
{
public static void main(Cord[] args)
{
final int UPPER_LIMIT = 100;
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int target, guess, numberOfGuesses;
char playAgain = 'y';
numberOfGuesses=0;
target = rand.nextInt(UPPER_LIMIT) + ane;
Arrangement.out.println("Noreail Nissan, cs-200-1, October ten, 2012.");
System.out.println("Assignment 4b: Guessing Game using for loop program.");
System.out.println();
System.out.println("I am thinking of an integer betwixt 1 and " + UPPER_LIMIT + ".");
System.out.println("Try to guess information technology. I will give you hints as you become.");
System.out.println();
for (guess = one; guess <= 100; approximate=target)
{
while (approximate != target)
{
numberOfGuesses++;
Organization.out.impress("What is your guess " + numberOfGuesses + " ?: ");
guess = keyboard.nextInt();
keyboard.nextLine();
if (judge < target)
System.out.println("higher");
else if (guess > target)
System.out.println("lower");
}
while (estimate != target);
{
if (judge < target)
Arrangement.out.println("higher");
else if (guess > target)
Arrangement.out.println("lower");
{
System.out.println("It took " + numberOfGuesses + " guesses");
Organisation.out.println();
Organization.out.impress("play once again?: ");
playAgain = keyboard.nextLine().charAt(0);
Organisation.out.println();
while (playAgain == 'y')
{
numberOfGuesses=0;
target=rand.nextInt(UPPER_LIMIT) + 1;
while (estimate != target)
{
numberOfGuesses++;
Organization.out.print("What is your guess " + numberOfGuesses + " ?: ");
guess = keyboard.nextInt();
keyboard.nextLine();
if (guess < target)
System.out.println("higher");
else if (guess > target)
Arrangement.out.println("lower");
}
while (judge != target);
{
if (guess < target)
Arrangement.out.println("higher");
else if (estimate > target)
Organization.out.println("lower");
}
Arrangement.out.println("It took " + numberOfGuesses + " guesses");
Organisation.out.println();
Organization.out.print("play once more?: ");
playAgain = keyboard.nextLine().charAt(0);
System.out.println();
}
Organisation.out.print("bye");
}
}
}
} // end principal()
} // end public class forloop
#ii
Re: guessing game with play once again option using only for loop in java
Posted 26 October 2012 - 04:12 AM
It's so difficult to help with code that isn't properly indented and posted in code tags. Please post your code in lawmaking tags. Don't start a new thread, just add a reply with the code in code tags. Instructions are in the edit surface area watermark.
#3
Re: guessing game with play over again option using but for loop in coffee
Posted 26 Oct 2012 - x:16 AM
Your lawmaking should exist placed in code tags like this.
import java.util.Scanner; import java.util.Random; public form forloop { public static void primary(String[] args) { last int UPPER_LIMIT = 100; Scanner keyboard = new Scanner(System.in); Random rand = new Random(); int target, guess, numberOfGuesses; char playAgain = 'y'; numberOfGuesses=0; target = rand.nextInt(UPPER_LIMIT) + one; System.out.println("Noreail Nissan, cs-200-1, October 10, 2012."); Organization.out.println("Assignment 4b: Guessing Game using for loop plan."); Organization.out.println(); Arrangement.out.println("I am thinking of an integer betwixt i and " + UPPER_LIMIT + "."); System.out.println("Try to judge information technology. I will give y'all hints equally you become."); System.out.println(); for (guess = 1; guess <= 100; approximate=target) { while (guess != target) { numberOfGuesses++; System.out.print("What is your guess " + numberOfGuesses + " ?: "); judge = keyboard.nextInt(); keyboard.nextLine(); if (gauge < target) System.out.println("higher"); else if (guess > target) System.out.println("lower"); } while (estimate != target); { if (judge < target) Organisation.out.println("higher"); else if (gauge > target) Arrangement.out.println("lower"); { Organisation.out.println("It took " + numberOfGuesses + " guesses"); System.out.println(); Arrangement.out.print("play again?: "); playAgain = keyboard.nextLine().charAt(0); System.out.println(); while (playAgain == 'y') { numberOfGuesses=0; target=rand.nextInt(UPPER_LIMIT) + one; while (guess != target) { numberOfGuesses++; Arrangement.out.print("What is your guess " + numberOfGuesses + " ?: "); gauge = keyboard.nextInt(); keyboard.nextLine(); if (estimate < target) System.out.println("higher"); else if (judge > target) System.out.println("lower"); } while (guess != target); { if (guess < target) Organization.out.println("higher"); else if (gauge > target) System.out.println("lower"); } Organization.out.println("Information technology took " + numberOfGuesses + " guesses"); Organisation.out.println(); System.out.impress("play once again?: "); playAgain = keyboard.nextLine().charAt(0); System.out.println(); } Organisation.out.impress("bye"); } } } } // end main() } // finish public grade forloop Anyways, yous tin't compare a Cord and any other Object using the == operator. So you lot demand to use the equals() method which will look like this.
while (playAgain.equalsIgnoreCase("Y")) The IgnoreCase later on the equals just makes information technology so that if the user inputs "y" or "Y" information technology'll still run. Hopefully, that volition work for you.
#iv
Re: guessing game with play again option using just for loop in coffee
Posted 26 October 2012 - 07:33 PM
thank you for showing how to put the code similar that but it yet didnt work
Source: https://www.dreamincode.net/forums/topic/297222-guessing-game-with-play-again-option-using-only-for-loop-in-java/
Enregistrer un commentaire for "Java Guessing Game Option to Try Again"