Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Looping a if and then statement 5 times

    This is a computer HW.
    Question:
    Consider the process used for developing the program for Mr. Sphereman. Modify the Washer program so that the input, process, and output identified taes place five times.

    Problem: Not sure which looping method I should use.

    Code:
    Code:
    import javax.swing.JOptionPane;
    public class  James {
        public static void main(String[] args) {
    
    		double Radius;
    		double Circumference;
            String werd;
            double pie;
            int two;
            String input;
            char repeat;
            String enter;
    
    			
    			
    		input = JOptionPane.showInputDialog("Enter the radius ");
    		Radius = Double.parseDouble(input);
    		pie = 3.14;
    		two = 2;
    		Circumference = two * Radius * pie;
           
            
            
            if ((3 < Circumference) && (Circumference <5.5)){
                JOptionPane.showMessageDialog(null,"This washer is optimally sized.");
            }
               else {
               	JOptionPane.showMessageDialog(null,"This washer is not adequate.");
             
               
            }       
            System.exit(0);
        }
    }
    The above is the "Washer" program but as of right now I'm stuck on choosing a looping method.
    Should I choose the "the While loop", "the do-while loop" or "the for loop"?

    EDIT:
    Code:
    /**
     * @(#)Washy.java
     *
     * Washy application
     *
     * @Emaj
     * @version 1.00 2008/3/7
     */
    import java.util.Scanner ;
    public class Washy {
    	
    public static void main(String[] args) {
    
    	double radius;
    	double circumference;
    	final double PI = 3.1415;
    	Scanner keyboard;
    	char repeat;
    	String input;
    	
    		keyboard = new Scanner(System.in);
    		System.out.println("Enter a radius");
    		radius = keyboard.nextDouble();
    		circumference = 2 * PI * radius ;
    		
    		do
    		{
    		
    		if (( circumference > 3.0 ) && (circumference < 5.5))
    			System.out.println("The washer is appropriately sized");
    			else
    				System.out.println("The washer is not adequate.");
    				
    			System.out.println("Would you like to repeat " +
                                "this program?");
             System.out.print("Enter Y for yes or N for no: ");
             input = keyboard.nextLine();  
             repeat = input.charAt(0);     
             
    		} while (repeat == 'Y' || repeat == 'y');
    	
    }
    
    }
    I decided to add a do-while loop but I encountered this problem when running it:
    Code:
    Enter Y for yes or N for no: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:687)
        at Washy.main(Washy.java:38)
    Also is there a way to make sure the program repeats itself 5 times?

    EDIT: Please delete this post thank you!
    Last edited by Emaj; 03-08-2008 at 03:17 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
  •