1

私はJavaプログラミングが初めてです。一連の入力を合計し、それらの数値の平均も計算するプロジェクトがあります。現在、値に何を入力しても、合計はゼロになります。私は立ち往生しています。助けてください。ありがとうございました。

   private class InputButtonListener implements ActionListener
   {
          public void actionPerformed(ActionEvent e)
          {  
                 for(int i=0; i<7; i++)
                 {
                       numInput = 0.0;
                       strInput = JOptionPane.showInputDialog(null, "How many hours did you sleep on day " + (i+1));
                       numInput = Double.parseDouble(strInput);
                       numInput +=total;                        
                 }
          }
   }
   private class CalcButtonListener implements ActionListener
   {
          public void actionPerformed(ActionEvent e)
          {
                 JOptionPane.showMessageDialog(null,"The total amount of sleep for the week is " + total + " hours");

                 JOptionPane.showMessageDialog(null,"The average amount of sleep for 7 days is " + avg + " hours");
          }
   }
   public static void main(String[] args)
          {
                 HoursSlept HS = new HoursSlept();
          }

}

4

4 に答える 4

2

あなたの問題はここにあるようです

numInput +=total; 

そのはず

total += numInput;
于 2013-04-19T19:48:04.963 に答える
1

total += numInputの代わりにする必要がありnumInput += totalます。

for(int i=0; i<7; i++)
{
    strInput = JOptionPane.showInputDialog(null, "How many hours did you sleep on day " + (i+1));
    numInput = Double.parseDouble(strInput);
    total += numInput;
}
于 2013-04-19T19:47:35.423 に答える
0

+= 演算子は逆に機能します。

あなたの問題はここにあります:

numInput += total;

する必要があります

total += numInput;
于 2013-04-19T19:49:56.047 に答える