Results 1 to 3 of 3
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default if and then while?

    Is there a way i can say while Variable is over 0 compute within a if statement, please see below:


    public class BicylceGears{

    public static void main (String[]args){

    double wheelRad;
    int fSprocket;
    int rSprocket;
    double eRad;
    double ratio;
    double eRadLst=0.0;

    System.out.println("Please enter the wheel radius");
    wheelRad =UserInput.readDouble();

    fSprocket =UserInput.readInt();

    rSprocket =UserInput.readInt();


    if( (wheelRad <= 0) || (fSprocket <= 0) || (rSprocket <= 0)){


    System.out.println("message Error, invalid value and do not let your program continue.");

    }else{


    eRad = wheelRad*fSprocket/rSprocket;

    ratio=eRadLst/eRad;

    System.out.println("The effective radius for raduis "+wheelRad+" and sprockets ");
    System.out.println("ratio to previous "+ratio);

    eRadLst= eRad+0;

    }
    while (eRad >0){


    eRad = wheelRad*fSprocket/rSprocket;

    ratio=eRadLst/eRad;

    System.out.println("The effective radius for raduis "+wheelRad+" and sprockets ");
    System.out.println("ratio to previous "+ratio);

    eRadLst= eRad+0;


    }
    }//end main

    }//end class

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class BG{
        public static void main (String[] args){
            double wheelRad;
            int fSprocket;
            int rSprocket;
            double eRad = 0;
            double ratio;
            double eRadLst=0.0;
    
            System.out.println("Please enter the wheel radius");
            wheelRad =UserInput.readDouble();
    
            fSprocket =UserInput.readInt();
    
            rSprocket =UserInput.readInt();
    
            if( (wheelRad <= 0) || (fSprocket <= 0) || (rSprocket <= 0)){
    
                System.out.println("message Error, invalid value " +
                                   "and do not let your program continue.");
                // Program execution will move to the [i]while[/i] loop from here.
    
            }else{
    
                eRad = wheelRad*fSprocket/rSprocket;
    
                ratio=eRadLst/eRad;
    
                System.out.println("The effective radius for raduis "+
                                    wheelRad+" and sprockets ");
                System.out.println("ratio to previous "+ratio);
    
                eRadLst= eRad+0;
            }
    
            // This loop will run until the value of eRad falls below zero.
            // What, inside the curley braces, will cause it to do this?
            // Maybe a different test would work better. What changes here?
            while (eRad > 0){
    
                // Changes to this variable affect/influence the
                // test condition for the while loop. How will the
                // value of this variable change inside this loop?
                eRad = wheelRad*fSprocket/rSprocket;
    
                ratio=eRadLst/eRad;
    
                System.out.println("The effective radius for raduis "+
                                    wheelRad+" and sprockets ");
                System.out.println("ratio to previous "+ratio);
    
                // What is this statement doing?
                eRadLst = eRad + 0;
            }
        }
    }
    
    class UserInput {
        static java.util.Scanner scanner = new java.util.Scanner(System.in);
    
        static double readDouble() {
            return Double.parseDouble(scanner.nextLine());
        }
    
        static int readInt() {
            return Integer.parseInt(scanner.nextLine());
        }
    }

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Do you mean
    Java Code:
    if (foo) {
      while (bar) {
        // do something
      }
    }
    Here's a suggestion. In a simple word processor, write out, in plain language, what you want your program to do. Avoid using any programming language, within reason. Put one step on each line. Use indentation to help you see structure.

    When you can read through the steps and they all make sense, then turn what you have into Java.

Posting Permissions

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