Java Guessing Game Option to Try Again
#1
guessing game with play again choice using just for loop in java
Posted 26 October 2012 - 03:58 AM
//i am new in java i have been learning for nearly ii months merely my assignment was to program a guessing game where the computer randomly chooses a number from i to 100 and it gives you hints higher or lower and after you gauge information technology correctly information technology tells y'all in how many guesses and after that it says "play over again" and if yous enter anything except 'y' then it should but say bye but if you enter 'y' then the game plays once more. it has to use the for loop. my trouble is that later on the play again question when i dont enter 'y' it outputs "good day" and "it took you lot # guesses". it should but say "bye" and finish the program. ive been working for hours and i dont know what i am doing wrong. whatsoever help would be bully. thanks!
import java.util.Scanner;
import java.util.Random;
public class forloop
{
public static void primary(String[] args)
{
final int UPPER_LIMIT = 100;
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int target, gauge, 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 programme.");
System.out.println();
System.out.println("I am thinking of an integer between 1 and " + UPPER_LIMIT + ".");
System.out.println("Try to guess information technology. I volition give you hints as you go.");
System.out.println();
for (guess = 1; guess <= 100; guess=target)
{
while (gauge != target)
{
numberOfGuesses++;
Organization.out.print("What is your guess " + numberOfGuesses + " ?: ");
guess = keyboard.nextInt();
keyboard.nextLine();
if (approximate < target)
System.out.println("higher");
else if (approximate > target)
System.out.println("lower");
}
while (guess != target);
{
if (approximate < target)
Arrangement.out.println("higher");
else if (guess > target)
System.out.println("lower");
{
System.out.println("It took " + numberOfGuesses + " guesses");
System.out.println();
Organization.out.impress("play again?: ");
playAgain = keyboard.nextLine().charAt(0);
Arrangement.out.println();
while (playAgain == 'y')
{
numberOfGuesses=0;
target=rand.nextInt(UPPER_LIMIT) + i;
while (guess != target)
{
numberOfGuesses++;
Organisation.out.impress("What is your guess " + numberOfGuesses + " ?: ");
estimate = keyboard.nextInt();
keyboard.nextLine();
if (guess < target)
Arrangement.out.println("college");
else if (gauge > target)
Organization.out.println("lower");
}
while (guess != target);
{
if (guess < target)
System.out.println("higher");
else if (guess > target)
System.out.println("lower");
}
Organisation.out.println("It took " + numberOfGuesses + " guesses");
System.out.println();
System.out.print("play once again?: ");
playAgain = keyboard.nextLine().charAt(0);
System.out.println();
}
Organisation.out.print("bye");
}
}
}
} // finish primary()
} // end public class forloop
#2
Re: guessing game with play again option using simply for loop in java
Posted 26 October 2012 - 04:12 AM
It's so hard to assistance with lawmaking that isn't properly indented and posted in code tags. Please post your code in code tags. Don't start a new thread, but add a reply with the code in lawmaking tags. Instructions are in the edit surface area watermark.
#3
Re: guessing game with play again option using merely for loop in java
Posted 26 October 2012 - 10:16 AM
Your lawmaking should be placed in code tags like this.
import java.util.Scanner; import java.util.Random; public class forloop { public static void principal(String[] 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; Organisation.out.println("Noreail Nissan, cs-200-1, October 10, 2012."); System.out.println("Assignment 4b: Guessing Game using for loop program."); Arrangement.out.println(); System.out.println("I am thinking of an integer between 1 and " + UPPER_LIMIT + "."); System.out.println("Effort to judge it. I volition requite you lot hints as you go."); Organization.out.println(); for (guess = 1; judge <= 100; guess=target) { while (estimate != target) { numberOfGuesses++; System.out.impress("What is your guess " + numberOfGuesses + " ?: "); guess = keyboard.nextInt(); keyboard.nextLine(); if (gauge < target) System.out.println("college"); else if (guess > target) System.out.println("lower"); } while (guess != target); { if (guess < target) System.out.println("higher"); else if (guess > target) System.out.println("lower"); { System.out.println("It took " + numberOfGuesses + " guesses"); System.out.println(); Arrangement.out.print("play over again?: "); playAgain = keyboard.nextLine().charAt(0); System.out.println(); while (playAgain == 'y') { numberOfGuesses=0; target=rand.nextInt(UPPER_LIMIT) + ane; while (guess != target) { numberOfGuesses++; System.out.print("What is your approximate " + numberOfGuesses + " ?: "); guess = keyboard.nextInt(); keyboard.nextLine(); if (approximate < target) Arrangement.out.println("higher"); else if (gauge > target) Arrangement.out.println("lower"); } while (guess != target); { if (approximate < target) Organization.out.println("higher"); else if (gauge > target) Organisation.out.println("lower"); } Organization.out.println("It took " + numberOfGuesses + " guesses"); System.out.println(); Arrangement.out.impress("play once more?: "); playAgain = keyboard.nextLine().charAt(0); System.out.println(); } System.out.print("bye"); } } } } // end main() } // finish public class forloop
Anyways, you can't compare a String and any other Object using the == operator. And then you need to utilise 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 will work for you.
#4
Re: guessing game with play again option using only for loop in java
Posted 26 October 2012 - 07:33 PM
thanks for showing how to put the code like that but it still didnt piece of work
hardenswerseavers.blogspot.com
Source: https://www.dreamincode.net/forums/topic/297222-guessing-game-with-play-again-option-using-only-for-loop-in-java/
0 Response to "Java Guessing Game Option to Try Again"
Postar um comentário