Results 1 to 13 of 13

Thread: sentinel help

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default sentinel help

    i have a program that allows a user to enter 2 floating point values, and returns some mathematical statements about them (average, range). I say to enter -1 to quit the program, but it does not exit-it only keeps on running. i used the while statement (number1 != =1) any ideas?

    thanks!
    Attached Files Attached Files

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    I suggest you provide an SSCCE that demonstrates the problem, keeping in mind that the program should be as short as possible, probably only a few lines.

    People aren't going to want to click on an attached file, and they aren't going to want to wade through code that doesn't directly have to do with the problem at hand.
    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
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by droidus View Post
    i used the while statement (number1 != =1) any ideas?
    Unless it's just a spelling mistake in the post, I'd suggest replacing =1 with -1.

  4. #4
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Looks like you're missing the closing bracket of your do while loop. The while statement is in the do section so it will run forever.
    Java Code:
    float number1;
    float number2;
    
    do {
    //Prompts user for their first set of floating-point values
    Scanner in = new Scanner(System.in);
    System.out.print("What is your first number? (enter -1 to terminate this program: ");
    number1 = in.nextFloat();
    
    //Prompts user for their second set of floating-point values
    System.out.print("What is your second number?: ");
    number2 = in.nextFloat();
    
    float average = ((number1+number2)/2);
    float range = (number1-number2);
    
    System.out.println("The average of these two numbers is: " + average + ".");
    System.out.println("The range of these two numbers is: " + range + ".");
    
    if (number1<number2){
    		System.out.println("The smallest value is: " + number1 + " and the largest, " + number2 + ".");}
    	else if (number1>number2){
    		System.out.println("The smallest value is: " + number2 + " and the largest, " + number1 + ".\n");}
    	else if (number1==number2){
    		System.out.println("The numbers are equal.");}
    }
    while (number1 != -1);

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Skiller View Post
    Unless it's just a spelling mistake in the post, I'd suggest replacing =1 with -1.
    I opened that link and the original code reads:

    Java Code:
    do 
    {
       // all code ...
    }
    while (number1 != -1);
    But another remark: the indentation stinks and is misleading (at least to me it is). Don't put those right curly brackets at those funny locations.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    yes, it is a problem with brackets. and should i be lining up my brackets like in the attached document?

    and yes, it is a -1... sorry.

    so i got it fixed, but i am surprised, because it won't check if number1=-1 until that loop has run through... I was hoping if it was entered for number1, it would quit right then and there...
    Attached Files Attached Files

  7. #7
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by droidus View Post
    I was hoping if it was entered for number1, it would quit right then and there...

    To do that you could replace the do with the while statement from the bottom of the loop (removing it from the bottom), then move
    Java Code:
    //Prompts user for their first set of floating-point values
    Scanner in = new Scanner(System.in);
    System.out.print("What is your first number? (enter -1 to terminate this program: ");
    number1 = in.nextFloat();
    to outside of the loop just above what would now be the while statement, then also copy that code just and paste it inside the loop above the closing brace of the loop.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by droidus View Post
    so i got it fixed, but i am surprised, because it won't check if number1=-1 until that loop has run through... I was hoping if it was entered for number1, it would quit right then and there...
    No need to be surprised because your program first does all the calculations with those numbers (even if they equal -1) and only at the end of your do-while loop you test whether or not one of them equals -1. Better do something like this:

    Java Code:
    do {
       // input the numbers
       if (number1 != -1) {
          // do the calculations
       }
    }
    while (number1 != -1);
    There are many variations to this theme; play with the control flow a bit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Skiller View Post
    then also copy that code just and paste it inside the loop above the closing brace of the loop.
    Eeeww! He said "copy and paste the code"! Don't ever say that again when I'm reading! Yuck!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Eeeww! He said "copy and paste the code"! Don't ever say that again when I'm reading! Yuck!
    Haha, yer I guess I'm too used to writing code for real time scenarios where if statements in loops should be avoided if possible (especially on PS3 which doesn't have proper branch prediction), but in this case your solution would certainly be much neater and more readable and without the need for real time performance therefor better :).

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Skiller View Post
    Haha, yer I guess I'm too used to writing code for real time scenarios where if statements in loops should be avoided if possible (especially on PS3 which doesn't have proper branch prediction), but in this case your solution would certainly be much neater and more readable and without the need for real time performance therefor better :).
    Well, this problem does need a condition (number1 == -1) and an action, so simply moving that test down from the while( ... ) test doesn't add any other test; something like this:

    Java Code:
    for (;;) { // loop forever
       // read two numbers
       if (number1 == -1) break; // get out of here
       // do the work ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    No need to be surprised because your program first does all the calculations with those numbers (even if they equal -1) and only at the end of your do-while loop you test whether or not one of them equals -1. Better do something like this:

    Java Code:
    do {
       // input the numbers
       if (number1 != -1) {
          // do the calculations
       }
    }
    while (number1 != -1);
    There are many variations to this theme; play with the control flow a bit.

    kind regards,

    Jos
    awesome, thanks! exactly what I needed :)

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by droidus View Post
    awesome, thanks! exactly what I needed :)
    Better use my suggestion in reply #11, it avoids testing the condition twice.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. using a "char" sentinel value for "int" input
    By jh7468 in forum New To Java
    Replies: 8
    Last Post: 03-02-2011, 11:42 PM
  2. Help with Sentinel Loops
    By hedwards09 in forum New To Java
    Replies: 11
    Last Post: 11-07-2009, 07:39 PM

Posting Permissions

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