Hi,
Try this code, i've modified it.
import javax.swing.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
// declaration of variables
String numStr, qStr;
double num, squareNum, cubeNum, metres = 0;
int q;
qStr = JOptionPane.showInputDialog("How many numbers do you want to convert?");
q = Integer.parseInt(qStr);
double[ ] number = new double[q];
for (int index = 0; index < number.length; index++)
{
numStr = JOptionPane.showInputDialog("Enter number "+index);
number[index] = Double.parseDouble(numStr);
}
for (int index = 0; index < number.length; index++)
{
int answer = 0;
do{
String answerStr = JOptionPane.showInputDialog("Metres to convert "+number[index]+
"\n1. Convert to Kilometres"
+"\n2. Convert to Inches"
+"\n3. Convert to Feet"
+"\n4. Exit Program");
answer = Integer.parseInt(answerStr);
if (answer == 1){
showKilo(number[index]);
}else if (answer == 2){
showInch(number[index]);
}else if (answer == 3){
showFeet(number[index]);
}
}while(answer != 4);
}
}
public static void showKilo(double k)
{
double kilometres = k * 0.001;
JOptionPane.showMessageDialog(null, k+" metres = "+kilometres+" kilometres");
}
public static void showInch(double i)
{
double inches = i * 39.37;
JOptionPane.showMessageDialog(null, i+" metres = "+i+" inches");
}
public static void showFeet(double f)
{
double feet = f * 3.281;
JOptionPane.showMessageDialog(null, f+" metres = "+f+" feet");
}
}
Regards,
Rakesh
|