我似乎无法添加文本字段并在显示卡路里文本字段中显示其结果。
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
public class caloriesBurnerApplet extends Applet implements ActionListener {
/****
//
//Running 6 mph : 10 METS
//Basketball : 8 METS
//Sleeping: 1 METS
//formula for the number calories burned per minute
//calories/minutes = 0.0175 * MET * Weight in kilogram
//
// 1 kg = 2.2 lbs ==> 1 lb = (1/2.2) kg
****/
Label lbWeight, lbRun, lbBsktBall, lbSleep, lbRunningCalories, lbBsktBallCalorioes,
lbSleepingCalories ;
Button caloriesCalcBtn ;
TextField txtWeight, txtRun, txtBsktBall, txtSleep, txtDspyRunCalr,
txtDspyBsktBallCalr, txtDspySleepCalr ;
int weight, minRun, minBsktBall, hourSleep;
float caloriesRun, caloriesBsktBall, caloriesSleep, caloriesTotal;
String weightString, minRunString, minBsktBallString, hourSleepString;
private static final long serialVersionUID = 1L;
public caloriesBurnerApplet()
{
lbWeight = new Label("Please enter your weight", JLabel.LEFT);
txtWeight = new TextField(15);
lbRun = new Label("How long do you run for 6 mph (in minutes)", JLabel.LEFT);
txtRun = new TextField(15);
lbBsktBall = new Label("How long do you play BasketBall (in minutes)");
txtBsktBall = new TextField(15);
lbSleep = new Label("How many hours do you sleep");
txtSleep = new TextField(15);
caloriesCalcBtn = new Button("Calculate calories");
lbRunningCalories = new Label("By Running you burnt these calories: ");
txtDspyRunCalr = new TextField(15);
lbBsktBallCalorioes = new Label("By Playing BasketBall you burnt there calories: ");
txtDspyBsktBallCalr = new TextField(15);
lbSleepingCalories = new Label("By Sleeping you burnt these calories: ");
txtDspySleepCalr = new TextField(15);
}
public void init()
{
add(lbWeight);
add(txtWeight);
add(lbRun);
add(txtRun);
add(lbBsktBall);
add(txtBsktBall);
add(lbSleep);
add(txtSleep);
add(caloriesCalcBtn);
add(lbRunningCalories);
add(txtDspyRunCalr);
add(lbBsktBallCalorioes);
add(txtDspyBsktBallCalr);
add(lbSleepingCalories);
add(txtDspySleepCalr);
}
@Override
public void actionPerformed(ActionEvent e) {
//calories/minutes = 0.0175 * MET * Weight in kilogram
// , minRunString, minBsktBallString, hourSleepString;
//weight = Integer.parseInt(txtWeight.getText());
//minRun = Double.parseDouble(txtRun.getText());
//minBsktBall = Double.parseDouble(txtBsktBall.getText());
//hourSleep = Double.parseDouble(txtSleep.getText());
weight = Integer.parseInt(txtWeight.getText());
minRun = Integer.parseInt(txtRun.getText());
minBsktBall = Integer.parseInt(txtBsktBall.getText());
hourSleep = Integer.parseInt(txtSleep.getText());
caloriesRun = (float) (minRun * 0.0175 * 10 * ((weight) * (1/2)));
if (e.getSource() == caloriesCalcBtn)
{
// can't seems to display result in the caloriesRun textfield
txtDspyRunCalr.setText(String.valueOf(caloriesRun));
}
}
}
答案 0 :(得分:0)
这个小程序使用整数值,它应该使用float或double。解决这个问题。
变化:
int weight, minRun, minBsktBall, hourSleep;
要:
double weight, minRun, minBsktBall, hourSleep;
&安培; actionPerformed应该更符合以下几行:
weight = Double.parseDouble(txtWeight.getText());
minRun = Double.parseDouble(txtRun.getText());
minBsktBall = Double.parseDouble(txtBsktBall.getText());
hourSleep = Double.parseDouble(txtSleep.getText());
// That '1/2' is the real problem, it equates to '0' in integer logic!
// caloriesRun = (float) (minRun * 0.0175 * 10 * ((weight) * (1/2)));
caloriesRun = (float) (minRun * 0.0175 * 10d * ((weight) * (.5)));