7 Replies - 589 Views - Last Post: 04 May 2013 - 07:56 PM Rate Topic: -----

#1 Vitreous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 13

JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 06:50 PM

Basically I've been asked to include the features mentioned in the title. I've done the counter already and I even have the replay feature working, though it doesn't reset my counter and I'm not sure if it will randomize a new number. Anyways here's what I have been working on:
import java.util.Random;
import java.util.Scanner;




public class MTMpg264p4 {

	public static void main(String[] args) {
		
		Random objRandom = new Random();
		int intValue = 4;
		int intTries = 0;
		boolean Correct = false;
		boolean Replay = false;
		//int intValue = objRandom.nextInt(10) + 1;
		
		
		while (Replay = true) {
			
		Scanner objScanner = new Scanner(System.in);
		int intGuess;
		
	
		while ( Correct == false )
		
			{
			
			System.out.print("Enter your guess between 1-10 (0 to quit): ");
			intGuess = objScanner.nextInt();
			intTries++;
			
			if ( intGuess == intValue ) {
			
			Correct = true;
			
			
			}
			
			else if ( intGuess < intValue )
			
			{
			
			System.out.print("You guessed too low, ");
			
			}
		
			else if ( intGuess > intValue )
		
			System.out.print("You guessed too high, ");
			}
			
			System.out.println("[You win!");
			System.out.println("The number was " + intValue);
			System.out.println("You guessed correctly after " + intTries + " turn!]");
			System.out.print("Do you want to play again? (Y/N) ");
			if (objScanner.next().equalsIgnoreCase("N")){ 
                Replay = true; 
			}
		 else{ 
                Correct = false; 
}

}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: JAVA Guessing Game using replay/quit/counter

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:01 PM

Try to use standard naming conventions, it makes life easier. That means camelCase variable names, and none of the Hungarian. Hungarian notation is always a code smell: it means you're having trouble determining the type of a variable. This can only mean that you're writing bad code, so it you're ever tempted to prefix your variable names with a hint as to the type, go back and figure out what you've written that's terrible and fix that instead.

Quote

while (Replay = true)


This is an assignment, not a comparison...

Quote

though it doesn't reset my counter


Show me where you try to do this...
Was This Post Helpful? 0
  • +
  • -

#3 Vitreous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 13

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:08 PM

The output I get after running it looks like this:
[code]
Enter your guess between 1-10 (0 to quit): 1
You guessed too low, Enter your guess between 1-10 (0 to quit): 4
[You win!
The number was 4
You guessed correctly after 2 turn!]
Do you want to play again? (Y/N) Y
Enter your guess between 1-10 (0 to quit): 4
[You win!
The number was 4
You guessed correctly after 3 turn!]
Do you want to play again? (Y/N)
[code]
It continued counting after prompting the user to replay and after the user had accepted. Also are you saying if I am using camelCase then I should stick to it?
Ex. instead of while ( Replay = true) I should use while ( booReplay = true) or something similar?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:16 PM

What's "boo"? Does the programmer reading this code not know how to determine that they're looking at a boolean? Just call it replay, make your code readable. objScanner? Why are you tormenting us with this? Does anyone not know that a scanner is an object? Get rid of that hungarian notation crap, it just makes a mess.



Show me in your code where you expect your counter to be reset. What line of the code resets the counter? I only see that variable being set to zero once, before the loop.
Was This Post Helpful? 0
  • +
  • -

#5 Vitreous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 13

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:21 PM

Reason behind using such notation is because in the class I'm taking our professor stresses we use it as a good programming etiquette so that it's easier to read. I'm attending a basic programming course so I have very little knowledge on Java. I guess my point is I have no clue where to start to set my code up to reset the counter or allow the user to quit. I have nothing in my book to reference off and my professor has not posted any examples of how to do so, that's why I am here looking for help.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:27 PM

Use it to make your professor happy then, but that's not good Java etiquette. Good java etiquette calls for using good names that communicate your intent and keeping your methods small enough that you're never confused about what any variable means. The only time you'll ever use that is if you're working on a project led by a C# refugee, who writes it into the project coding standards.

Anyway, enough about that.

You have a while loop. When the user says they want to repeat, you go back to the top of that loop. The top of that loop is a good place to reset the counter. You do that by setting it to zero.
Was This Post Helpful? 0
  • +
  • -

#7 Vitreous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 13

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:47 PM

Ok! So that actually worked for me. I revised my code a bit and I've got the counter reset working along with the random number being different each time. Now the issue I have is whenever the user inputs anything besides Y/y it will print out my "thanks for playing" message but will also run the game again as if they had won. The code looks like this:
import java.util.Random;
import java.util.Scanner;




public class MTMpg264p4 {

	public static void main(String[] args) {
		
		Random objRandom = new Random();
		//int intValue = 4;
		
		boolean Correct = false;
		boolean Replay = true;
		int intGuess;
		
		
		
		while ( Replay = true ) {
		
		int intValue = objRandom.nextInt(10) + 1;	
		Scanner objScanner = new Scanner(System.in);
		//int intGuess;
		int intTries = 0;
	
		while ( Correct == false )
		
			{
			
			System.out.print("Enter your guess between 1-10 (0 to quit): ");
			intGuess = objScanner.nextInt();
			intTries++;
			
			if ( intGuess == intValue ) {
			
			Correct = true;
			
			
			}
			
			else if ( intGuess < intValue )
			
			{
			
			System.out.print("You guessed too low, ");
			
			}
		
			else if ( intGuess > intValue )
		
			System.out.print("You guessed too high, ");
			}
			
			System.out.println("[You win!");
			System.out.println("The number was " + intValue);
			System.out.println("You guessed correctly after " + intTries + " turns!]");
			System.out.print("Do you want to play again? (Y/N) ");
		
			if (objScanner.next().equalsIgnoreCase("Y")){ 
                Replay = true; 
                Correct = false;
			}
		 else{ 
			 	System.out.print("Thanks for playing!");

}
		}
		}
}


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: JAVA Guessing Game using replay/quit/counter

Posted 04 May 2013 - 07:56 PM

Quote

while ( Replay = true )


This sets Replay to true. You want a comparison, not an assignment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1