Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2012, 11:04 PM   PM User | #1
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
Is this correct

Hi guys,

I'm fairly new to Java, so please excuse my lack of knowledge on it :P

Anyway, I recently wrote a script; it didn't take me very long and it is far, far from complicated, but still, I'm just wondering whether if the code I've actually written is correct.

By that I mean, are the pragmatics of the code correct, is there a way to simplify it etc. It would be fantastic from my point of view if you could give me some feedback on it, as it'll greatly allow me to improve. I haven't commented it yet, but as it's not particularly complicated, I don't think that's necessary. Basically, it's a script where the computer guesses a number between 0 and 50, and you have 6 tries to try and guess it. If you don't succeed in guessing it within those 6 tries, then you lose, and vice versa. Also, the "TextIO.getln();" methods are from a foreign class, supplied by the book - it's basically a much superior scanner method.

Anyway, below is the code:

Code:
package TestWork;

public class GuessGame {
	
	public static String id;
	
	public static void main(String[] argv) {
		
		System.out.println("Hello, it's time to play the guessing game!");
		System.out.println("What's your name?");
		id = TextIO.getlnString();
		
		start();
	}
		
		public static void start() {

		
		int userGuess, guessCount = 0, remain = 6, realNo, test;
		realNo = (int)(50 * Math.random()) + 1;
		System.out.println("Right, " + id + ", I will now guess a number between 1-50.");
		System.out.println("If you get the right number within 6 tries, then you win!");
		System.out.println("Otherwise, you lose! Ok " + id + ", let's begin!");
		System.out.println();
		
		System.out.println("Try and guess the number I'm thinking of! ");
		
		
		while (true) {
			userGuess = TextIO.getlnInt();
			if (userGuess != realNo) {
				guessCount++ ;
				remain-- ;
				System.out.println("Wrong! You have " + remain + " tries left.");
			}
			else if (userGuess == realNo) {
				if (guessCount == 0) {
				System.out.println("Unbelievable, you guessed my number in the first go! You are truly magnificent!");
				System.out.println("Would you like to try again? Type '1' to restart or any other character to exit.");
				test = TextIO.getlnInt();
				if (test == 1) {
					start();
					}
				else {
					System.exit(0);
					}
				}
				else if (guessCount != 0) {
					System.out.println("Congratulations, you guessed my number, " + "'" + realNo + "' , in " + guessCount + " tries!");
					System.out.println("Would you like to try again? Type '1' to restart or any other character to exit.");
					test = TextIO.getlnInt();
					if (test == 1) {
						start();
						}
					else {
						System.exit(0);
						}
					}
					
				}
			
		
			if (remain == 0) {
				System.out.println("You're all out of guesses! You lose! ");
				System.exit(0);
			}

		}
		
		
		
		
	}

}
Thanks,

James

Last edited by Jemdt; 07-23-2012 at 12:53 AM..
Jemdt is offline   Reply With Quote
Old 07-23-2012, 08:19 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Does it compile and run as expected (after checking each potential condition)? If so, then yes its fine.

I wouldn't have done it this way. I refuse to use forced exits or breaks within loops. I would have instead used a while (!bCorrect && guessCount++ <= remain) as my condition.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.