Results 1 to 7 of 7
- 12-20-2012, 07:07 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Need help with my while loops please do not know what I'm doing wrong
I am having a couple issue with my while loops...I cannot exit my first while loop even when I enter the correct information and if i cancel out the entire first while loop I escape the second loop no matter what after my second entry...here is just the test portion of my code:
Results:Java Code:import java.util.*; public class TestSailBoat { public static void main(String[] args) { //Attributs int _numSails; int _lengthBoat; String _colour; //allows user to enter information about the boat Scanner scanner = new Scanner(System.in); System.out.print("Colour of boat: "); _colour = scanner.nextLine(); while((!_colour.equals("red"))||(!_colour.equals("blue"))||(!_colour.equals("white"))||(!_colour.equals("yellow"))) { System.out.println("ERROR: color must be red, white, blue or yellow"); System.out.print("Colour of boat: "); _colour = scanner.nextLine(); } System.out.print("Lenght of boat: "); _lengthBoat = Integer.parseInt(scanner.nextLine()); while ((_lengthBoat < 0) && (_lengthBoat > 50)); { System.out.println("ERROR: length of boat must be between 0 and 50 inclusively"); System.out.print("Lenght of boat: "); _lengthBoat = Integer.parseInt(scanner.nextLine()); } System.out.print("Number of sails: "); _numSails = Integer.parseInt(scanner.nextLine()); while (_numSails < 1 && _numSails > 4) { System.out.println("ERROR: number of sails must be between 1 and 4 inclusively"); System.out.print("Number of sails: "); _numSails = Integer.parseInt(scanner.nextLine()); } SailBoat sails = new SailBoat(_colour, _lengthBoat, _numSails); sails.calcPrice(); System.out.println(sails.toString()); } }
First Loop:
Colour of boat: red
ERROR: color must be red, white, blue or yellow
Colour of boat: white
ERROR: color must be red, white, blue or yellow
Colour of boat: blue
ERROR: color must be red, white, blue or yellow
Colour of boat:
Second Loop:
Colour of boat: white
Lenght of boat: 98
ERROR: length of boat must be between 0 and 50 inclusively
Lenght of boat: 98
Number of sails:
I have not even tried testing the third loop yet
- 12-20-2012, 07:26 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: Need help with my while loops please do not know what I'm doing wrong
You are using the wrong operator there. When you are using the || operator when one expression is true then the result will be true.
Your other while loop also have a wrong expression, where the condition will never be true. No number is less and greater than something at the same time.Java Code:colors ------------------------- !r !w !b !y inputs red F T T T = T white T F F F = T blue T T F T = T yellow T T T F = TLast edited by wsaryada; 12-20-2012 at 07:35 AM.
Website: Learn Java by Examples
- 12-20-2012, 07:42 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Re: Need help with my while loops please do not know what I'm doing wrong
Thank you wsaryada. I updated the second and third while loops in my code to be || instead of &&. That issue seems to be resolved now. I do not understand what type of operator I should use for the colors. I though it would be or (||) but your reasoning definitely makes sense (taking discrete math currently). What would you recommend?
- 12-20-2012, 07:54 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help with my while loops please do not know what I'm doing wrong
De Morgan already solved this small problem in the 19th century: if you want the colour to be one of red, white, blue or yellow, we can write it down an red || white || blue || yellow. If the colour isn't one of those colours, the expression !(red || white || blue || yellow) is true; De Morgan figured out that the last expression is equivalent to !red && !white && !blue && !yellow.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2012, 08:13 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Re: Need help with my while loops please do not know what I'm doing wrong
thank you so much JosAH that worked for the colors and now for some reason the lengthBoat loop is not working:
my input:Java Code:System.out.print("Lenght of boat: "); _lengthBoat = Integer.parseInt(scanner.nextLine()); while (_lengthBoat < 0 || _lengthBoat > 50); { System.out.println("ERROR: length of boat must be between 0 and 50 inclusively"); System.out.print("Lenght of boat: "); _lengthBoat = Integer.parseInt(scanner.nextLine()); } System.out.print("Size of engine: "); _sizeOfEngine = Integer.parseInt(scanner.nextLine()); while (_sizeOfEngine < 1 || _sizeOfEngine > 350) { System.out.println("ERROR: size of engine must be between 1 and 350 inclusively"); System.out.print("Size of engine: "); _sizeOfEngine = Integer.parseInt(scanner.nextLine()); }
Colour of boat: red
Lenght of boat: 34
ERROR: length of boat must be between 0 and 50 inclusively
Lenght of boat: 65
Number of sails:
- 12-20-2012, 08:27 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 12-20-2012, 08:56 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
Help with while loops
By enjoy2225 in forum New To JavaReplies: 9Last Post: 10-26-2012, 12:27 AM -
I can't find anything wrong with this but somehow it's wrong.
By Biscuit Tickler in forum New To JavaReplies: 2Last Post: 09-12-2012, 09:28 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
While Loops, need a bit of help.
By Keno777 in forum New To JavaReplies: 7Last Post: 10-30-2009, 08:24 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks