Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Sierra

Thread: While loop repeat

  1. #1
    Nb4bi9 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default While loop repeat

    I hope this isn't me asking for a handout but I've been reviewing my program, and while it's relatively simply I can't seem to get one of my while loops to repeat correctly.

    Code:
    Java Code:
    import java.util.Scanner;
    public class BoatBuilder
    {
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    
    		int s = 537;
    		int m = 1243;
    		int l = 2422;
    		int mtr = 400;
    		int o = 50;
    		double ft = 0.12;
    		int h = 0;
    		int t = 0;
    		int oo = 0;
    		int ot = 0;
    		double tt = 0.00;
    		double tot = 0.00;
    		String input = "";
    		String repeat = "y";
    
    		while(repeat.equals("y"))
    		{
    
    		System.out.println("Hi! Welcome to Boat Builder. We're ready to build your boat.");
    		System.out.println("What size boat would you like?");
    		System.out.println("(1) Small");
    		System.out.println("(2) Medium");
    		System.out.println("(3) Large");
    		h = in.nextInt();
    		while(true)
    		{
    			if(h == 1)
    			{
    				h = 537;
    				System.out.println("Small it is");
    				break;
    			}
    			if(h == 2)
    			{
    				h = 1243;
    				System.out.println("Medium it is");
    				break;
    			}
    			if(h == 3)
    			{
    				h = 2422;
    				System.out.println("Large and in charge!");
    				break;
    			}
    			if(h >= 3)
    			{
    				System.out.println("Please enter a number from 1 to 3");
    				h = in.nextInt();
    			}
    		}
    
    
    
    
    
    			System.out.println("Would you like to add a motor for $" + mtr + "? (yes or no)");
    			input = in.next();
    			while(true)
    			{
    
    			if(input.equals("yes"))
    			{
    				t = mtr;
    				System.out.println("Ok, we'll install that right away and the price has been added to your bill");
    
    			}
    			else
    			{
    				if(input.equals("no"))
    				{
    
    					System.out.println("Well, guess you'll be wanting some oars, then");
    
    				}
    			}
    
    			System.out.println("How many oars would you like?");
    			oo = in.nextInt();
    			while(true)
    			{
    
    				if(oo > 0)
    				{
    					ot = oo * o;
    					System.out.println("Ok, we'll add " + oo + " oars to your boat");
    					break;
    
    				}
    				else
    				{
    					if(oo == 0)
    					{
    						System.out.println("Ok, we won't add any oars to your boat");
    						break;
    
    					}
    				}
    				if(oo < 0)
    				{
    					System.out.println("Please enter a positive integer");
    					oo = in.nextInt();
    					break;
    
    				}
    			}
    
    			tt = (t + h + ot) * ft;
    			tot = t + h + ot + tt;
    			System.out.println(tot);
    
    			System.out.println("Would you like to build another boat? (y or n)");
    			repeat = in.next();
    			if(repeat.equals("y"))
    			{
    				 repeat = repeat;
    			}
    
    
    
    
    
    
    			}
    		}
    	}
    }
    The program is supposed to repeat when the user inputs a "y". However, the program only repeats after a certain point, specifically, where the program outputs "Ok, we'll install that right away and the price has been added to your bill". I can't pinpoint where I'm going wrong with my coding. I've tried to change some of the variables but I'm wondering if I need to add another variable or not.

    Thanks for any advice!
    Last edited by Fubarable; 12-11-2012 at 01:05 AM. Reason: code tags added

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: While loop repeat

    Please get your code into the forum code brackets... it is unreadable like that.
    Also it would be helpful to describe the problem in more detail... what is not working and how do you notice?
    I like likes!

  3. #3
    Nb4bi9 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: While loop repeat

    Sorry about the lack of code tags >.< and thank you Fubarable for adding them.

    As far as what more I can say on the issue is that after I've gone through and run the program once, meaning I've selected the type of boat I want, if I want a motor, oars, etc... It brings up the total amount for the boat and then asks if I want to make another one. What's supposed to happen is the program repeats from the "What size boat would you like" while repeat.equals("y"). What happens is the program doesn't go all the way back to the top and instead repeats from line 71. It also assumes, without asking, that I've automatically input a 1 for boat size and selected "yes" for adding a motor as the final calculated total gives me a number equal to those 2 values (that was kind of confusing to write, hope it makes some sense).

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: While loop repeat

    In my opinion you have way enough variables... try to make the code clearer:
    - include operations into new methods and call them at the appropriate points
    - use less infinite while loops
    - use break only if there is no other good option.
    - use extended variable names... oo ot tot are nothing anyone can know what they should do. ;)

    As far as I can see you are struck in your infinite loop before "Ok, we'll install that right away and the price has been added to your bill"
    because you will never exit that one on your "repeat" variable.
    Last edited by Sierra; 12-11-2012 at 01:21 AM.
    Fubarable likes this.
    I like likes!

Similar Threads

  1. Replies: 18
    Last Post: 04-03-2012, 10:49 AM
  2. repeat if statement in loop
    By eng_hyzoom in forum New To Java
    Replies: 5
    Last Post: 03-05-2012, 01:34 PM
  3. Storing A Repeat Loop In An Array
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 07-22-2011, 06:57 AM
  4. Help with a repeat
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 01-01-2010, 10:42 PM
  5. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 07:19 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
  •