Я новичок в программировании и только что начал на прошлой неделе, так что все java mumbojumbo довольно запутывают меня. Мне удалось создать панель параметров для моей программы BMI, которая спрашивает, какая система единиц (метрическая/имперская) с помощью радиолюбителей, и это определяет, какой расчет выполнять при поиске ИМТ. все это прекрасно работает, за исключением того, что первая опция не закрывается при выборе опции, как мне закрыть ее, когда опция выбрана. Я хочу, чтобы jpane с радиоблоками закрывалась в инструкции do.Как вы закрываете JOptionPane после того, как выбран радиобъектив?
package javaapplication21;
import java.text.DecimalFormat;
import javax.swing.*;
import java.*;
public class JavaApplication21 {
public static void main(String[] args) {
JPanel jPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton metricButton = new JRadioButton("Metric");
metricButton.setActionCommand("Metric");
JRadioButton imperialButton = new JRadioButton("Imperial");
imperialButton.setActionCommand("Imperial");
group.add(metricButton);
group.add(imperialButton);
jPanel.add(metricButton);
jPanel.add(imperialButton);
JOptionPane.showOptionDialog(null, "Please select prefered units", "BMI Calculator", JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null, new Object[] { metricButton, imperialButton}, null);
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
double bodyMassIndex, weight, height;
String unitsWeight = "-1", unitsHeight = "-1";
do{
if (metricButton.isSelected()){
unitsWeight = " in kg.";
unitsHeight = " in meters";
}
else if (imperialButton.isSelected()){
unitsWeight = " in lbs";
unitsHeight = " in inches";
}
}
while ("-1".equals(unitsWeight));
String weightInput = JOptionPane.showInputDialog("Please enter your weight" + unitsWeight);
String heightInput = JOptionPane.showInputDialog("Please enter your height" + unitsHeight);
if (metricButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = weight/(height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("You are at high risk of many health concerns");
}
else if (imperialButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = (weight * 703)/(height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("No more big macs!");
}
}
}
+1 хорошее исследование – mKorbel
Большое вам спасибо за помощь. Я очень к этому не очень знаком. – user3491364