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

时间:2013-09-11 16:32:00

标签: java string integer jtextfield

任何人都可以向我解释为什么我的家庭作业会重新调整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的值

2 个答案:

答案 0 :(得分:8)

您应该阅读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); 

答案 1 :(得分:-1)

基本上,您仍然将整数除以整数(然后将其转换为浮点数)。试试这个:

// Multiplying by 1.0 casts that answer to a float, so the
//     division by dSpeed will also result in a float.
float fTotal = (1.0 * dDistance) / dSpeed;