Results 1 to 3 of 3
Thread: if and then while?
- 01-19-2009, 08:55 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 39
- Rep Power
- 0
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
- 01-20-2009, 03:00 AM #2
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()); } }
- 01-20-2009, 03:07 AM #3
Do you mean
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.Java Code:if (foo) { while (bar) { // do something } }
When you can read through the steps and they all make sense, then turn what you have into Java.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks