Results 1 to 7 of 7

Thread: Easier way?

  1. #1
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Easier way?

    Just finished a small guessing game. Was just wondering, is there a simpler way to write this? It seems so...ugly.
    Java Code:
    import java.util.Scanner;
    
    
    public class Guess {
    	public static void main(String args[]) {
    		Scanner in = new Scanner(System.in);
    		int num;
    		System.out.println("Guess the number. It's a whole number between 1-100. You get 5 tries.");
    		num = in.nextInt();
    		// The number is 42. The meaning of life. :)
    		if (num == 42) {
    			System.out.println("You're right!");
    		} else {
    			System.out.println("You're wrong! Try again. You've got 4 more guesses.");
    			num = in.nextInt();
    			if (num == 42) {
    				System.out.println("You're right!");
    			} else {
    				System.out.println("You're wrong! Try again. You've got 3 more guesses.");
    				num = in.nextInt();
    				if (num == 42) {
    					System.out.println("You're right!");
    				} else {
    					System.out.println("You're wrong! Try again. You've got 2 more guesses.");
    					num = in.nextInt();
    					if (num == 42) {
    						System.out.println("You're right!");
    					} else {
    						System.out.println("You're wrong! Try again. This is your last try!");
    						num = in.nextInt();
    						if (num == 42) {
    							System.out.println("You're right!");
    						} else {
    							System.out.println("You're wrong. Sorry, you're out of tries!");
    						}
    					}
    				}
    			}
    		}
    	in.close();
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Easier way?

    Looks like a job for a while loop or a do-while loop

    Recommended reading: http://docs.oracle.com/javase/tutori...lts/while.html
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Easier way?

    Hah, thanks! Wrote this after reading that:
    Java Code:
    import java.util.Scanner;
    
    
    public class GuessRevamped {
    	public static void main(String args[]) {
    		Scanner in = new Scanner(System.in);
    		int num;
    		System.out.println("Guess the number. It's a whole number between 1-100. You get 5 tries.");
    		num = in.nextInt();
    		while (num != 42) {
    			System.out.println("You're wrong! Try again!");
    			num = in.nextInt();
    		}
    		if (num == 42) {
    			System.out.println("You're right! Here's a cookie!");
    		}
    	}
    }
    How would I set a 5-guess limit, though?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Easier way?

    Use the Random class to randomly generate a number to guess instead of it being 42 all the time.

    You can use more than one condition for the while loop.
    Java Code:
    while(x && y)
    while(x || y)
    while(x && y || z)
    etc

  5. #5
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Easier way?

    I'm not seeing how that would help me set a 5-guess limit?
    Are you saying I set how many times the while loop runs in the condition?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Easier way?

    Yeah, how about 5 times!

  7. #7
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Easier way of creating multiple buttons
    By alexix in forum New To Java
    Replies: 6
    Last Post: 04-21-2013, 01:54 PM
  2. Is there an easier way to get position out of array
    By codyjava in forum New To Java
    Replies: 5
    Last Post: 12-02-2012, 10:05 PM
  3. Replies: 8
    Last Post: 04-10-2010, 02:17 AM
  4. hope there is an easier way.
    By IYIaster in forum New To Java
    Replies: 6
    Last Post: 10-14-2009, 08:26 AM
  5. what about thinkink in easier way for making tables
    By soharto in forum AWT / Swing
    Replies: 3
    Last Post: 09-09-2008, 01:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •