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

Thread: begginer wondering why his guessing game won't work

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default begginer wondering why his guessing game won't work

    heres the code:

            Scanner sc = new Scanner(System.in);
            Random rand = new Random();
            boolean running = true;
            String yesno;
            String y = "y";
            String n = "n";
            boolean guessrunning = true;
            int i = 0;
            int low = 0;
            int high = 0;
            boolean highlow = true;
            int guess = 0;
            boolean guesscheck = true;
     
            System.out.println("Would you like play? Y/N");
            while (running == true) {
                yesno = sc.next();
                if (yesno.compareToIgnoreCase(y) == 0) {
                    while (highlow == true) {
                        System.out.println("please enter the highest number");
                        high = sc.nextInt();
                        System.out.println("please enter the lowest number");
                        low = sc.nextInt();
     
                        if (high - low <= 0) {
                            System.out.println("Please enter a high number which is higher than the low number");
                        } else {
                            highlow = false;
                        }
                    }
                    highlow = true;
                    System.out.println("Please enter your Guesses");
                    int randomnum = rand.nextInt(high - low) + low;
     
                    System.out.println((int) Math.sqrt(high - low));
                    for (i = 0; i < (int) Math.sqrt(high - low); i++) {
                        while (guesscheck = true) {
                            guess = sc.nextInt();
                            if (guess > high || guess < low) {
                                System.out.println("Please enter a number that is between your low and high numbers");
                            } else {
                                guesscheck = false;
                                if (guess == randomnum) {
                                    System.out.println("congratulations!! you got it right!");
                                    i = (int) Math.sqrt(high - low);
                                } else {
                                    System.out.println("Wrong, try again");
                                }
                                guesscheck = false;
                            }
                        }
     
                    }
                    System.out.println("Play again? Y/N");
     
     
                } else if (yesno.compareToIgnoreCase(n) == 0) {
                    running = false;
                } else {
                    System.out.println("Please enter Y for yes or N for no");
                }
     
     
            }




    it does have stuff before and after it, thats the core of it though

    the problem is after you have entered high and low, you then have infinite guessses, despite my best attempts at making you only have the square root of high - low guesses

    this is a problem that i am doing for school, thats why its crazy, but its no assessment task, so no holding back

    thanks in advance
    Last edited by Ligawulf; March 7th, 2010 at 04:47 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: begginer wondering why his guessing game won't work

    These ones can drive you crazy, it took me a couple scans over your code to spot it.

    while (guesscheck = true) {

    You wanted a "==" in there
    What this does is first assign guesscheck the value true, then check to see if that evaluates to true (which it does), so your loop never exits.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: begginer wondering why his guessing game won't work

    god damn it....i was puzzling over that for ages...i though i had checked that....thanks for the response though, my brother had a look and he removed the while loop basically, and did a couplke of other things to get it to work...but thanks heaps, i would be wondering why it didn't work forever otherwise

Similar Threads

  1. [SOLVED] Can't get JFreeChart to work
    By igniteflow in forum Java SE APIs
    Replies: 2
    Last Post: February 15th, 2011, 02:19 AM
  2. mouse Released no work!!
    By frenkelor in forum AWT / Java Swing
    Replies: 1
    Last Post: January 23rd, 2010, 06:41 AM
  3. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM
  4. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM