2017-11-03 2 views
-2

私は本当にJavaには新しく、私は自分のコンソールアプリケーションのGUIを作成しようとしています。私はこれらのエラーを修正するのに役立つコードを書いています。プログラムは、私が情報を入力し、BMIが無限に読み込まれ、すべてのデータを受け取るまでうまく実行されるようです。もし誰かが私のコードをクリーンアップして働かせてくれたら、これに悩まされているのです。私のGUIプログラムに問題がありました

健康プロファイルクラス

public class HealthProfile 
{ 
    // attributes 

    private String name; 

    private short age; 
    private float weight; 
    private float height; //measured in total inches 

    //constructors 
    public HealthProfile() 
    { 
     name = "unknown"; 
     age = 0; 
     weight = 0.0f; 
     height = 0.0f; 
    } 

    public HealthProfile(String name, short age, float weight, float height) { 
     this.name = name; 
     this.age = age; 
     this.weight = weight; 
     this.height = height; 
    } 

    //behaviors 
    public float getBMI() 
    { 
     return (float) ((weight *703)/Math.pow(height, 2.0f)); 
    } 
    public String getCategory() 
    { 
     float bmi = getBMI(); 
     if(bmi < 18.5f) 
      return "Underweight"; 
     else if(bmi >= 18.5f && bmi < 25) 
      return "Normal"; 
     else if (bmi >= 25 && bmi < 30) 
      return "Overweight"; 
     else 
      return "Obese"; 
    } 
    public float getMaxHR() 
    { 
     return 220 - age; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public short getAge() { 
     return age; 
    } 

    public void setAge(int i) { 
     if (i > 0 && i < 150) 
      this.age = (short) i; 
     else 
      this.age = 0; 
    } 

    public float getWeight() { 
     return weight; 
    } 

    public void setWeight(double d) { 
     if (d > 0.0f && d <1000.0f) 
      this.weight = (float) d; 
     else 
      this.weight = 0.0f; 
    } 

    public float getHeight() { 
     return height; 
    } 

    public void setHeight(float height_feet, float height_inches) { 
     this.height = (height_feet *12) + height_inches; 
    } 
} 

GUIクラス::

import java.awt.GridLayout; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 

public class HealthProfileGUI extends JFrame 
{ 
    //Create GUI components 
    private JTextField txtName, txtAge, txtWeight, txtHeightFt, txtHeightIn, 
    txtBMI, txtCategory, txtMxHeartRate; 
    private JButton btnDisplay, btnClear; 
    private HealthProfile hthPro; 

    //Constructor for building the GUI 
    public HealthProfileGUI() 
    { 
     //Add a name to the GUI 
     super("Health Profile"); 
     hthPro = new HealthProfile(); 
     //instantiate components 
     txtName = new JTextField (20); 
     txtAge = new JTextField(10); 
     txtWeight = new JTextField(10); 
     txtHeightFt = new JTextField(10); 
     txtHeightIn = new JTextField(10); 
     btnDisplay = new JButton ("Display"); 
     btnClear = new JButton ("Clear"); 
     txtBMI = new JTextField(10); 
     txtCategory = new JTextField(10); 
     txtMxHeartRate = new JTextField(10); 

     //set layout 
     setLayout(new GridLayout(0,2)) ; 
     //Add components to frame 
     add(new JLabel("Name")); 
     add(txtName); 
     add(new JLabel("Age")); 
     add(txtAge); 
     add(new JLabel("Weight")); 
     add(txtWeight); 
     add(new JLabel("Height-Feet")); 
     add(txtHeightFt); 
     add(new JLabel("Height-Inches")); 
     add(txtHeightIn); 
     add(btnDisplay); 
     add(btnClear); 
     add(new JLabel("BMI")); 
     add(txtBMI); 
     add(new JLabel ("Category")); 
     add(txtCategory); 
     add(new JLabel ("Max Heart Rate")); 
     add(txtMxHeartRate); 

     //set up event handlers 
     //create an object 
     ButtonHandler handler = new ButtonHandler(); 
     //connect the buttons to the ActionListener using this object 
     btnDisplay.addActionListener(handler); 
     btnClear.addActionListener(handler);   
    }//end constructor 

    private class ButtonHandler implements ActionListener 
    { //Start class 
     public void actionPerformed(ActionEvent e) 
     { 
      //code to process the button events goes here 
      if (e.getSource() == btnDisplay) 
      { 
       //VERIFY that user has provided all input 
       if (txtName.getText().isEmpty() || 
        txtAge.getText().isEmpty() || 
        txtWeight.getText().isEmpty() || 
        txtHeightFt.getText().isEmpty()|| 
        txtHeightIn.getText().isEmpty() 
       )//missing input 
       { 
        JOptionPane.showMessageDialog(null, "Please provide all input"); 
        return; //exit this method so user can correct 
       } 
       { 
        hthPro.setName(txtName.getText()); 
        try 
        { 
         hthPro.setAge(Integer.parseInt(txtAge.getText())); 
         hthPro.setWeight(Double.parseDouble(txtWeight.getText())); 
         //hthPro.setHeight(Double.parseDouble(txtHeightFt.getText())); 

        } 
        catch (NumberFormatException ex) 
        { 
         JOptionPane.showMessageDialog(null, "Hours, rate must be numeric"); 
        } 
        txtBMI.setText(String.valueOf(hthPro.getBMI())); 
        txtCategory.setText(String.valueOf(hthPro.getCategory())); 
        txtMxHeartRate.setText(String.valueOf(hthPro.getMaxHR())); 
       } 
      } 
     } 
    } 
} 

メイン:ここ

は、各クラスのコードです

import javax.swing.JFrame; 

public class Lab2Main { 

    public static void main(String[] args) 
    { 
     HealthProfileGUI frame = new HealthProfileGUI(); 
     frame.setSize(400, 300); //these numbers are in pixels 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

誰のためにありがとうございました助けることができる私は問題がそれほど大きくないことを願っています。

hthPro.setHeight(Float.parseFloat(txtHeightFt.getText()), 
      Float.parseFloat(txtHeightIn.getText())); 

は、その後のは、あなたの次の問題を見てみましょう:Math.pow(0, 2.0f)による除算の結果は、あなたのsetHeight行のコメントを解除するので

+1

デバッガでコードを実行したときに何を見つけましたか? – bcsb1001

+0

問題は、 'setHeight'を呼び出す行をコメントアウトしたことですが、' getBMI'メソッドで 'height'の2乗で除算することになります。 0で除算すると無限大を返します。 – bcsb1001

+0

1)* "これらのエラーを修正するのに役立つだけです" *どのようなエラー?具体的にして、読者が彼らが何であるかを推測できると仮定しないでください。 (ここでは、コードはここでエラーを投げずに実行されているようです)2)すぐに役立つようにするには、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/) 。この場合、コードはおそらく十分短いですが、3つのコピー/ペーストではなく、単一の 'コピー/ペースト'コンパイルと実行でなければなりません。これは、 'main'メソッドを' HealthProfileGUI'クラスにコピーし、次に 'HealthProfile'をデフォルト(' public'から)に降格させ、それを@の末尾に貼り付けることによって行うことができます。 –

答えて

0

BMIは無限大です。

関連する問題