停车场小程序锻炼

时间:2020-02-27 02:52:39

标签: java

我正在尝试制作一个停车小程序,以便您输入停车时间。它告诉您将向您收取多少费用,并保持累计收据总额。当我输入第一个数字时,总收据会无限增加。我正在尝试将WHILE循环用于哨兵控制的重复,但是它不起作用。请帮忙。

   import java.awt.event.*;

   import javax.swing.*;

   public class Garage2 extends JApplet implements ActionListener
   {
       JLabel promptLabel, chargeLabel;
       JTextField inputField, chargeField;

       public void init()
       {
           Container container = getContentPane();
           container.setLayout(new FlowLayout());

           promptLabel = new JLabel("Enter hours parked ");
           inputField = new JTextField(10);
           inputField.addActionListener(this);
           container.add(promptLabel);
           container.add(inputField);

           chargeLabel = new JLabel("Your current charge is ");
           chargeField = new JTextField(10);
           chargeField.setEditable(false);
           container.add(chargeLabel);
           container.add(chargeField);
       }   //end of init() method

       public void actionPerformed(ActionEvent actionEvent)
       {
           double input = Double.parseDouble(actionEvent.getActionCommand());
           double receipts = 0.0, amountCharged;

           showStatus("Total receipts equal: " + receipts);

           while (input != -1)
           {
               if (input > 0.0 && input <= 3.0)
               {
                   receipts += 2.0;
                   chargeField.setText("2.0");
                   showStatus("Total receipts equal: " + receipts);
               }   //end of if block

               else if (input <= 24.0)
               {
                   receipts += calculateCharges(input);
                   amountCharged = calculateCharges(input);
                   chargeField.setText(Double.toString(amountCharged));
                   showStatus("Total receipts equal: " + receipts);
               }   //end of else if block

               else 
               {
                   receipts += 10.0;
                   chargeField.setText("10.0");
                   showStatus("Total receipts equal: " + receipts);
               }   //end of else block
           }   //end while body

       }   //end actionPerformed() method

       public double calculateCharges(double hours)
       {
           return Math.ceil(hours) * 0.50;
       }   //end calculateCharges() method
   }   //end Garage2 class```

0 个答案:

没有答案
相关问题