Loops only on the first statement
A little help please..It does only loop on the first conversion which is in "miles".
Code Java:
package If_then_Else;
import javax.swing.*;
public class If_then_Else {
static final double km_per_miles = 1.6093;
static final double meter_per_miles = 1.6093;
static final double feet_per_miles = 5279.85565107;
static final double inches_per_miles = 63358.26781284;
static final double cm_per_miles = 160930.0002446136;
static final double miles_per_km = 0.6213882;
static final double meter_per_km = 1000;
static final double feet_per_km = 3280.8399;
static final double inches_per_km = 39370.07;
static final double miles_per_meter = 621.3882;
static final double km_per_meter = 1000;
static final double feet_per_meter = 3.280839;
static final double inches_per_meter = 39.3700788;
static final double cm_per_meter = 100;
static final double miles_per_feet = 0.0001893991176;
static final double km_per_feet = 0.0003048;
static final double meter_per_feet = 0.3048;
static final double inches_per_feet = 12;
static final double cm_per_feet = 30.48;
static final double miles_per_inches = 0.000015783254;
static final double km_per_inches = 0.000025349;
static final double meter_per_inches = 0.02534;
static final double feet_per_inches = 0.08333;
static final double cm_per_inches = 2.51;
static final double miles_per_cm = 0.000006218797;
static final double km_per_cm = 0.000004;
static final double meter_per_cm = 0.004;
static final double feet_per_cm = 0.032808386877;
static final double inches_per_cm = 0.3937008;
public static void main(String[] args) {
String opt = JOptionPane.showInputDialog(null, "1. Miles\n2. Kilometers\n3. Meters\n4. Feet\n5. Inches\n6. Centimters\n\nDesired Choice:");
double cm;
while (opt != null) {
String milesStr;
double km;
double meters;
double feet;
double inches;
double miles;
milesStr = JOptionPane.showInputDialog(null, "Number of Miles:");
miles = Double.parseDouble(milesStr);
km = miles * km_per_miles;
meters = miles * meter_per_miles;
feet = miles * feet_per_miles;
inches = miles * inches_per_miles;
cm = miles * cm_per_miles;
JOptionPane.showMessageDialog(null,km + " miles\n" + meters + " miles\n" + feet + " miles\n" + inches + " miles\n" + cm + " miles");
String kmStr;
kmStr = JOptionPane.showInputDialog(null, "Number of Kilometers:");
km = Double.parseDouble(kmStr);
miles = km * miles_per_km;
meters = km * meter_per_km;
feet = km * feet_per_km;
inches = km * inches_per_km;
JOptionPane.showMessageDialog(null,miles + " km\n" + meters + " km\n" + feet + " km\n" + inches + " km");
String metersStr;
metersStr = JOptionPane.showInputDialog(null, "Number of Meters:");
meters = Double.parseDouble(metersStr);
miles = meters * miles_per_meter;
km = meters * km_per_meter;
feet = meters * feet_per_meter;
inches = meters * inches_per_meter;
cm = meters * cm_per_meter;
JOptionPane.showMessageDialog(null,miles + " meters\n" + km + " meters\n" + feet + " meters\n" + inches + " meters\n" + cm + " meters");
String feetStr;
feetStr = JOptionPane.showInputDialog(null, "Number of Feet:");
feet = Double.parseDouble(feetStr);
miles = feet * miles_per_feet;
km = feet * km_per_feet;
meters = feet * meter_per_feet;
inches = feet * inches_per_feet;
cm = feet * cm_per_feet;
JOptionPane.showMessageDialog(null,miles + " feet\n" + km + " feet\n" + meters + " feet\n" + inches + " feet\n" + cm + " feet");
String inchesStr;
inchesStr = JOptionPane.showInputDialog(null, "Number of Inches:");
inches = Double.parseDouble(inchesStr);
miles = inches * miles_per_inches;
km = inches * km_per_inches;
feet = inches * feet_per_inches;
meters = inches * meter_per_inches;
cm = inches * cm_per_inches;
JOptionPane.showMessageDialog(null,miles + " inches\n" + meters + " inches\n" + feet + " inches\n" + km + " inches\n" + cm + " inches");
String cmStr;
cmStr = JOptionPane.showInputDialog(null, "Number of Centimeters:");
cm = Double.parseDouble(cmStr);
miles = cm * miles_per_cm;
km = cm * km_per_cm;
feet = cm * feet_per_cm;
inches = cm * inches_per_cm;
meters = cm * meter_per_cm;
JOptionPane.showMessageDialog(null,km + " cm\n" + feet + " cm\n" + feet + " cm\n" + inches + " cm\n");
}
}
}
Re: Loops only on the first statement
Is the aim of this application to pick a conversion type, input a number and then it displays the result?
Why are you using a while loop? Do you want it to do every single conversion in a row?
Please give me a better idea of the functionality of the application.
Re: Loops only on the first statement
Quote:
Originally Posted by
JavaPF
Is the aim of this application to pick a conversion type, input a number and then it displays the result?
Why are you using a while loop? Do you want it to do every single conversion in a row?
Please give me a better idea of the functionality of the application.
Yes that's what is supposed to do. If I pressed 1 ( for the computation of miles )then I will input the number of Miles then it'll show the results under miles. After it then it should go back to the main menu which is to choose again that is either option 1 to or 6, but if none is entered then the loop will exit.
Re: Loops only on the first statement
Quote:
Originally Posted by
JavaPF
Is the aim of this application to pick a conversion type, input a number and then it displays the result?
Yes that's what is supposed to do. If I pressed 1 ( for the computation of miles )then I will input the number of Miles then it'll show the results under miles. After I chose a single conversion then it should go back to the main menu which is to choose again that is either option 1 to or 6, but if none is entered then the loop will exit.
Re: Loops only on the first statement
Have you tried playing computer with your code by manually stepping thru it step by step and seeing why it does what it does?
You must go slow and carefully check what each statement does.
What needs to change for the code execution to exit the while loop?
Where is that change made?