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

Thread: How to narrow down the range of input in java?

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to narrow down the range of input in java?

    i have a question about my program. i have it set up to give an interval if its to high or to low but its a very wide range i was wondering if there was a way to narrow the range down to within like ten of the random number here is the code i have now.

    import java.util.Scanner;
    import java.util.*;
     
    public class NumberGame
    {
     
        public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
     
            Random random = new Random();
            int rand = random.nextInt(100);
            int guesses = 0;
            int low = 1;
            int high = 100;
     
            System.out.println("I'm thinking of a number between 0 and 100. Whats your guess?");
     
            while (sc.hasNextInt())
            {
                int myGuess = sc.nextInt();
                if (myGuess == rand)
                {
                    guesses++;
                    System.out.println("Congratulations! I was thinking " + rand
                            + ". It took you " + guesses + " guesses.");
                    break;
                }
                else if (myGuess < rand)
                {
                    System.out.println("Try to guess higher.Try Between:"+":"+myGuess+"-"+high);
                    guesses++;
     
                }
                else if (myGuess > rand)
                {
                    System.out.println("Try to guess lower.Try Between"+":"+myGuess+"-"+low);
                    guesses++;
     
               }
            }
     
     
        }
     
    }
    Last edited by Deep_4; November 7th, 2012 at 01:17 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: question about my program

    Hello big_c.

    I have edited the code to show a range of 10 either side of the guess.

    import java.util.Scanner;
    import java.util.*;
     
    public class NumberGame
    {
     
        public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
     
            Random random = new Random();
            int rand = random.nextInt(100);
            int guesses = 0;
            int low = 1;
            int high = 100;
     
            System.out.println(rand);
     
            System.out.println("I'm thinking of a number between 0 and 100. Whats your guess?");
     
            while (sc.hasNextInt())
            {
                int myGuess = sc.nextInt();
                if (myGuess == rand)
                {
                    guesses++;
                    System.out.println("Congratulations! I was thinking " + rand
                            + ". It took you " + guesses + " guesses.");
                    break;
                }
                else if (myGuess < rand)
                {
                    int a = (rand + 10);
                    int b = (rand - 10);
     
                    if(b < myGuess){
                        b = myGuess;
                    }
                    System.out.println("Try to guess higher." + " Try Between: "  + b + "-" + a);
                    guesses++;
     
                }
                else if (myGuess > rand)
                {
                    int a = (rand + 10);
                    int b = (rand - 10);
     
                       if(b > myGuess){
                        b = myGuess;
                    }
                    System.out.println("Try to guess lower." + " Try Between:"  + b + "-" + a);
                    guesses++;
     
               }
            }
     
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: question about my program

    thanks but i actually figured it out yesterday morning. instead of makeing low = 1 i set low to low=rand-5 and high=rand+5 and that seems to work out just as well. thanks for the help anyways

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: question about my program

    Did you see the if statements I added?

    This fixes a few problems. Compile this code with the if statments commented out:

    import java.util.Scanner;
    import java.util.*;
     
    public class NumberGame
    {
     
        public static void main(String[] args)
        {
     
            Scanner sc = new Scanner(System.in);
     
            Random random = new Random();
            int rand = random.nextInt(100);
            int guesses = 0;
            int low = 1;
            int high = 100;
     
            System.out.println(rand);
     
            System.out.println("I'm thinking of a number between 0 and 100. Whats your guess?");
     
            while (sc.hasNextInt())
            {
                int myGuess = sc.nextInt();
                if (myGuess == rand)
                {
                    guesses++;
                    System.out.println("Congratulations! I was thinking " + rand
                            + ". It took you " + guesses + " guesses.");
                    break;
                }
                else if (myGuess < rand)
                {
                    int a = (rand + 10);
                    int b = (rand - 10);
     
                    //if(b < myGuess){
                    //    b = myGuess;
                    //}
                    System.out.println("Try to guess higher." + " Try Between: "  + b + "-" + a);
                    guesses++;
     
                }
                else if (myGuess > rand)
                {
                    int a = (rand + 10);
                    int b = (rand - 10);
     
                       //if(b > myGuess){
                    //    b = myGuess;
                    //}
                    System.out.println("Try to guess lower." + " Try Between:"  + b + "-" + a);
                    guesses++;
     
               }
            }
     
     
        }
     
    }

    You will see that without the if statements, if the rand number is 30, and I guess 29, the program says "Try between 20 and 40"

    Thats because it is doing 10 either side of the rand number.

    I've already guessed over 20 so it makes sense to say "Try between 29 and 40"

    Thats what these if statements do. Un-comment them to see the difference. I hope you understand what I mean.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: question about my program

    ahhh i see what your saying now. that makes sense. thats for the help and clarification

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Talking Re: question about my program

    Quote Originally Posted by big_c View Post
    ahhh i see what your saying now. that makes sense. thats for the help and clarification
    Thats no problem. I just thought this made the program more user friendly.

    If this thread is solved, please mark it as solved:

    http://www.javaprogrammingforums.com...-new-post.html

    Thanks big_c!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM
  2. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 07:01 AM