Results 1 to 13 of 13
Thread: sentinel help
- 03-03-2011, 04:21 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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!
- 03-03-2011, 04:26 PM #2
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!
- 03-03-2011, 05:30 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
- 03-03-2011, 05:43 PM #4
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);
- 03-03-2011, 05:44 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-03-2011, 07:54 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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...
- 03-03-2011, 08:18 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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
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.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();
- 03-03-2011, 08:22 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
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:
There are many variations to this theme; play with the control flow a bit.Java Code:do { // input the numbers if (number1 != -1) { // do the calculations } } while (number1 != -1);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-03-2011, 08:30 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 03-04-2011, 06:37 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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 :).
- 03-04-2011, 07:48 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:for (;;) { // loop forever // read two numbers if (number1 == -1) break; // get out of here // do the work ... }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-04-2011, 02:55 PM #12
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
- 03-04-2011, 02:58 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
using a "char" sentinel value for "int" input
By jh7468 in forum New To JavaReplies: 8Last Post: 03-02-2011, 11:42 PM -
Help with Sentinel Loops
By hedwards09 in forum New To JavaReplies: 11Last Post: 11-07-2009, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks