0

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

4

2 回答 2

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); 
于 2013-09-11T16:38:35.860 回答
-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;
于 2013-09-11T16:36:18.823 回答