gpt4 book ai didi

Java将整数转换为字符串并计算

转载 作者:行者123 更新时间:2023-12-01 07:30:05 26 4
gpt4 key购买 nike

谁能向我解释一下为什么我的作业不断重新调整为 1.0,我问了我的老师,他告诉我“语言非常相似,我不记得这种语言 -.-..... 你不应该学习这个是因为你使用的是 java 1 而不是 java 2”……无论如何,我正在努力进步并自学

              /**
* Author Rich Talcik, Do not Edit or modify
* Homework assignment2
* 09/11/13
* Problem 2.7 page 109
*
*/
private static final long serialVersionUID = 1L;
JLabel jlabel;
JFrame frame = new JFrame("home work");

JTextField tfAnswer = new JTextField("Click to find travel Time");
JTextField tfSpeed = new JTextField("Remvoe Text & Enter Speed here");
JTextField tfDistance = new JTextField("Remove Text & Enter Distance Here");

float dSpeed;
float dDistance;
String sTotal;
String sSpeed;
String sDistance;

public shapeDrawing() {
super(new BorderLayout());
JButton button = new JButton("Calculate my Travel Time");

//Converting the speed to an Integer from a String



button.addActionListener(this);


tfSpeed.addMouseListener(new MouseListener() {




@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
tfSpeed.setText("");
}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
});

tfDistance.addMouseListener(new MouseListener() {


@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
tfDistance.setText("");
tfAnswer.setText("");
}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

});

add(tfSpeed, BorderLayout.AFTER_LINE_ENDS);
add(tfDistance, BorderLayout.LINE_START);

add(tfAnswer, BorderLayout.PAGE_END);
add(button, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

}


public void actionPerformed(ActionEvent e) {

sSpeed = tfSpeed.getText();
dSpeed = Integer.valueOf(sSpeed);

//Converting Distance to integer from string
sDistance = tfSpeed.getText();
dDistance = Integer.valueOf(sDistance);


final float fTotal = (dDistance / dSpeed);


tfAnswer.setText(String.valueOf(fTotal));
}

有人可以解释为什么 tfAnswer.setText 不断返回 1.0 的值

最佳答案

您应该阅读 tfDistance 而不是 tfSpeed

         sSpeed = tfSpeed.getText();
dSpeed = Integer.valueOf(sSpeed);

//Converting Distance to integer from string
sDistance = tfSpeed.getText(); //!!! copy-past error
dDistance = Integer.valueOf(sDistance);

应该是

         sSpeed = tfSpeed.getText();
dSpeed = Integer.valueOf(sSpeed);

//Converting Distance to integer from string
sDistance = tfDistance.getText();
dDistance = Integer.valueOf(sDistance);

关于Java将整数转换为字符串并计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18746603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com