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 7 of 7

Thread: Basic Number guessing game.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Number guessing game.

    This is my first post in this forum and i have no idea how to post code in the format that other people post it.
    This is my first little program that i wrote after reading a couple of chapters in a book and looking at some tutorials but sadly it doesnt work.
    Every time one puts in a smaller number it still says that the number is smaller even though its supposed to be bigger
    I also tried using the Random number generator but it fails either way..
    Would be happy if anyone could help



    import java.util.*;

    public class Apple {
    public static void main(String[] args) {

    Scanner A = new Scanner(System.in);
    int realNumber = 55;
    System.out.println("Enter an integer between 0 and 100");
    int userNumber = A.nextInt();

    while(userNumber != realNumber){

    if( userNumber > realNumber){
    System.out.println("Its less then " +userNumber);
    System.out.println("Please enter another number");
    }
    if( userNumber < realNumber){
    System.out.println("Its less then " +userNumber);
    System.out.println("Please enter another number");
    }
    else{
    System.out.println("The number is invalid");
    System.out.println("Please enter another number");
    }
    userNumber = A.nextInt();
    }

    System.out.println("The number is correct");
    }


    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic Number guessing game.

    it fails either way..
    Please explain what happens.

    Could you copy the full content of the command prompt window when you execute the program and post it here so we can see what the program does?
    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Number guessing game.

    Thank you for the quick reply

    import java.util.*;
     
    public class Apple {
    	public static void main(String[] args) {
     
    	Scanner A = new Scanner(System.in);
    	int realNumber = 55;
    	System.out.println("Enter an integer between 0 and 100");
    	int userNumber = A.nextInt();  
     
    	while(userNumber != realNumber){
     
    		if( userNumber > realNumber){
    			System.out.println("Its less then  " +userNumber);
    			System.out.println("Please enter another number");
    		}
    		if( userNumber < realNumber){
    			System.out.println("Its less then  " +userNumber);
    			System.out.println("Please enter another number");
    		}
    		else{
    			System.out.println("The number is invalid");
    			System.out.println("Please enter another number");
    		}
    		userNumber = A.nextInt();
    	 }
     
    	System.out.println("The number is correct");
    	}
     
     
    }


    What happens is that if i execute it and a number that is less than 55 appears it still says that the number i entered is supposed to be less

    "Enter an integer between 0 and 100
    40
    Its less then 40
    Please enter another number
    30
    Its less then 30
    Please enter another number
    "

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic Number guessing game.

    it still says that the number i entered is supposed to be less
    What should it say?

    Where in the program is the message that you want to see?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Number guessing game.

    o.O I didnt realize that i said
    System.out.println("Its less then  " +userNumber);
    for both when its greater and when its less. I never had a statement saying that it is greater...problem resolved. Thank you

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Basic Number guessing game.

    Keep testing. There are still problems with the messages that are printed.
    What happens when you enter 60?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Number guessing game.

    import java.util.*;
     
    public class Apple {
    	public static void main(String[] args) {
     
    	Random randomGenerator = new Random();
    	Scanner A = new Scanner(System.in);
    	int realNumber = randomGenerator.nextInt(101);
    	System.out.println("Enter an integer between 0 and 100");
    	double userNumber =(int) A.nextDouble();  
     
    	while(userNumber != realNumber){
     
    		if( userNumber > realNumber){
    			System.out.println("Its less then  " +userNumber);
    			System.out.println("Please enter another number");
    		}
    		else if( userNumber < realNumber){
    			System.out.println("Its greater then  " +userNumber);
    			System.out.println("Please enter another number");
    		}
    		else {
    			System.out.println("The number is invalid");
    			System.out.println("Please enter another number");
    		}
    		userNumber = A.nextInt();
    	 }
     
    	System.out.println("The number is correct");
    	}
     
     
    }

    I also fixed the error that happens if you enter a double and i made it a random number

Similar Threads

  1. Basic Guessing Game
    By djl1990 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 06:14 PM
  2. JAVA HIGH LOW NUMBER GUESSING GAME
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 12th, 2011, 10:50 AM
  3. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM