Sum函数在Java中不起作用。

Sum函数在Java中不起作用。,java,Java,我是Java编程新手。我有一个项目,它应该对一系列输入求和,并计算这些数字的平均值。现在,无论我输入什么值,总数都是零。我卡住了。请帮忙。多谢各位 private class InputButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0; i<7; i++

我是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();
          }
私有类InputButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{  

对于(int i=0;i它应该是
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;
}

for(inti=0;i您的问题似乎就在这里

numInput +=total; 
应该是

total += numInput;

+=运算符以相反的方式工作

你的问题在这里:

numInput += total;
应该是

total += numInput;

你不是应该把总数和numInput切换吗

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);
                   total +=numInput;                        
             }

用于(inti=0;我不知道
total
是在哪里定义的,但我几乎可以肯定的是,for循环的最后一行是向后的。您可能想要
total+=numInput
。这可能是您的问题。
numInput+=total;
您正在将
total
的值添加到
numInput
中。您打算这样做吗站点:
total+=numInput;