从IF语句输出GUI中的字符串

时间:2018-03-24 15:20:54

标签: java

我是编程的新手,我一直在尝试将这段字符串从if语句输出到GUI字段中,当程序正在编译时,答案字段只输出最后一个语句,而另一个按钮工作很好......我附上了代码..

else如果附加到try语句,那些if语句没有运行或者什么,但它只输出final语句。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 implements ActionListener {
    JLabel ans, ans2;
    JTextField text1, text2;

    public void displayWindow(){
        JFrame frame= new JFrame ("BMI Calculator");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (5,2,7,10));

        text1= new JTextField();
        text2= new JTextField();

        JButton buttonBMI= new JButton ("BMI (Body Mass Index)");
        buttonBMI.setActionCommand ("bmi");
        buttonBMI.addActionListener (this);

        JButton buttonH= new JButton ("Am I Healthy?");
        buttonH.setActionCommand ("health");
        buttonH.addActionListener (this);

        ans= new JLabel();
        ans2= new JLabel();

        frame.add (new JLabel ("Height (m)"));
        frame.add (text1);
        frame.add (new JLabel ("Weight (kg)"));
        frame.add (text2);
        frame.add (buttonBMI);
        frame.add (buttonH);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans2);

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed (ActionEvent e){
            try {
                double a= Double.parseDouble (text1.getText());
                double b= Double.parseDouble (text2.getText());
                double answer=0;
                double height=0;
                String answer2= "";

                String command = e.getActionCommand();

                if (command.equals ("bmi")){
                    height = Math.pow (a,2);
                    answer = b / height;
                    ans.setText ("" +answer);
                }
                else if (command.equals ("health")){
                    if (answer > 30){
                        ans2.setText ("You are Obese");

                    }
                    else if (answer > 29.9 && answer < 24.9 ){
                        ans2.setText ("You are Overweight");

                    }
                    else if (answer > 25 && answer < 18.5){
                        ans2.setText ("You are of Normal Weight");

                    }
                    else if (answer < 18.5){
                        ans2.setText("You are Underweight");

                    }
                }
        }
        catch (Exception ex){
            ans.setText ("Please use Numbers");
        }
}
}

2 个答案:

答案 0 :(得分:0)

在代码中初始化answer的语句位于if语句中,该语句将命令与&#34; bmi&#34;进行比较。如果命令不是&#34; bmi&#34;,则不执行该语句。您需要将其移出if语句。

答案 1 :(得分:0)

这个if语句

else if (answer < 18.5){ ... }

始终为true,因为变量answer的值始终为0。我看到你正在为第一个if块中的answer变量赋值。您可以尝试将answer变量声明为全局范围,以便保留第一个if块中指定的值。

这两个陈述

answer > 29.9 && answer < 24.9

answer > 25 && answer < 18.5

总是假的。 answer值不能同时大于29.9且小于24.9。

试试这个

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 implements ActionListener {
    JLabel ans, ans2;
    JTextField text1, text2;
    double answer=0;
    double height=0;

    public void displayWindow(){
        JFrame frame= new JFrame ("BMI Calculator");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (5,2,7,10));

        text1= new JTextField();
        text2= new JTextField();

        JButton buttonBMI= new JButton ("BMI (Body Mass Index)");
        buttonBMI.setActionCommand ("bmi");
        buttonBMI.addActionListener (this);

        JButton buttonH= new JButton ("Am I Healthy?");
        buttonH.setActionCommand ("health");
        buttonH.addActionListener (this);

        ans= new JLabel();
        ans2= new JLabel();

        frame.add (new JLabel ("Height (m)"));
        frame.add (text1);
        frame.add (new JLabel ("Weight (kg)"));
        frame.add (text2);
        frame.add (buttonBMI);
        frame.add (buttonH);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans2);

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed (ActionEvent e){
            try {
                double a= Double.parseDouble (text1.getText());
                double b= Double.parseDouble (text2.getText());
                String answer2= "";

                String command = e.getActionCommand();

                if (command.equals ("bmi")){
                    height = Math.pow (a,2);
                    answer = b / height;
                    ans.setText ("" +answer);
                }
                else if (command.equals ("health")){
                    if (answer > 30){
                        ans2.setText ("You are Obese");

                    }
                    else if (answer > 24.9 && answer < 29.9 ){
                        ans2.setText ("You are Overweight");

                    }
                    else if (answer > 18.5&& answer < 25){
                        ans2.setText ("You are of Normal Weight");

                    }
                    else if (answer < 18.5){
                        ans2.setText("You are Underweight");

                    }
                }
        }
        catch (Exception ex){
            ans.setText ("Please use Numbers");
        }
}
}