Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Easy game not doing what i want it to... Boo

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Easy game not doing what i want it to... Boo

    This is my second attempt at running a program through the terminal. I finally found out how to change it to plain text so that i can add the .java extension and find it. It compiles and runs but doesn't quite do what i want. It detects if the number is too high or too low just fine. But when the correct number is guessed the command line just prints a "new" empty line when it should be saying "ding ding ding".

    I have tried getting rid of the random number and assigning a set field, 77 for example. It still does not ding for me once the two are equal. I have also tried rearranging my if/else statements to try to get it to work. This simple game is making me != happy. Please point me in the right direction!!!

    import java.util.Random;
    import java.util.Scanner;
    class UserGuess {
    	public static void main(String[] args) {
     
    	Random num = new Random();
    	Scanner input = new Scanner(System.in);
     
    	int winningNum = num.nextInt(100);
    	int userGuess;
     
    	System.out.println("Enter a number between 1 and 100");
    	userGuess = input.nextInt();
     
    	while (userGuess != winningNum){
    	if (userGuess == winningNum){
    	System.out.println("ding ding ding");
    	} else if (userGuess > winningNum) {
    	System.out.println("too high");
    	userGuess = input.nextInt();
    	} else {
    	System.out.println("too low");
    	userGuess = input.nextInt();
    	}
     
     
    	}
     
     
         }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Easy game not doing what i want it to... Boo

    Hello.
    It might help you if you can trace your program using a paper and pencil. Run your program and simultaneously trace it on the paper.
    You will end up knowing where exactly you are making mistake.
    You can even add some System.out.println() statements to monitor your execution.
    Try first to your level best. If you still fail then we shall help you more.
    Hint: Know the flow of control for loops and conditional statements.

    Syed.

Similar Threads

  1. Replies: 2
    Last Post: September 27th, 2012, 08:06 PM
  2. Easy.
    By Totel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 26th, 2012, 04:19 PM
  3. Easy Tic Tac Toe game and Applet Assignment
    By Jmastersam in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2012, 11:06 AM
  4. Easy but over my head
    By Dejay79 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2012, 11:54 AM
  5. This will be an easy one.
    By Pedroski in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2011, 01:53 AM