Я новичок в программировании и только начал на прошлой неделе, поэтому вся эта мешанина с java меня сбивает с толку. Мне удалось создать панель параметров для моей программы ИМТ, которая спрашивает, какая система единиц (метрическая/имперская) с радиокнопками, и это определяет, какой расчет выполнять при нахождении ИМТ. все это работает нормально, за исключением того, что первая панель параметров не закрывается при выборе параметра, как мне сделать так, чтобы он закрывался при выборе параметра. Я хочу, чтобы панель jpane с радиокнопками закрывалась в инструкции do.
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!");
}
}
}