“运行总计返回错误值”

时间:2019-03-23 15:14:07

标签: java loops

因此,此代码应该每天添加总计并显示总销售额

但是由于某种原因,答案始终是天数的平方

我认为这是由于其他Java文件打开导致的,因此我删除了它们,但没有帮助

package com.test;

import javax.swing.*;

public class test_3
{
    public static void main(String[] args)
    {
        int days;                       //number of days
        double sales;                  // sales per day
        double TotalSales = 0.0;       // Total sales

        String input = JOptionPane.showInputDialog("For how many days is the sales ?");

        days = Integer.parseInt(input);         //converting days into number

        for(int count = 1; count <= days; count++)
        {
            JOptionPane.showInputDialog("Enter sales amount of day " + count);
            sales = Double.parseDouble(input);

            TotalSales += sales;
        }

        JOptionPane.showMessageDialog(null,"Total sales is "+ TotalSales);


    }
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

此行:

JOptionPane.showInputDialog("Enter sales amount of day " + count);

应为:

input = JOptionPane.showInputDialog("Enter sales amount of day " + count);

否则,input仍然是您从第一个提示(多少天)中获得的值,因此您要将该金额与该总次数相加,从而得出平方。