程序中的数学不正确,不确定原因?

时间:2018-04-04 22:33:14

标签: java math

我正在为课堂写一个简单的程序。我不确定为什么数学表现不正确?当我要求税收并输入0.08时,它没有计算到正确的数字。例如,输入1234.55作为零售额,交易金额为1266.6483,而不是正确金额,应为1333.31。任何帮助将不胜感激......我将发布以下代码

package Project03Driver;

import java.io.*;

public class Project03Driver
{
    public static void main(String args[]) throws IOException
    {
        Project03 app;
        app = new Project03();
        app.appMain();
    }
}

class Project03
{
    BufferedReader stdin;
    int tNum, tCount, smallTnum;
    double tax, rA, tA, raTot, disTot, taTot, taAvg, smallTa;
    boolean disc;
    String store, date, dFlag, inString;

    public void appMain() throws IOException
    {
        rptInit();
        displayHeader();
        getStore();
        while(tNum != 0)
        {
            salesData();
        }
        calcAvg();
        rptOut();
    }

    void rptInit()
    {
        tNum = 1;
        tCount = smallTnum = 0;
        raTot = disTot = taTot = taAvg = 0;
        smallTa = 10000;
        stdin = new BufferedReader (new InputStreamReader(System.in));
    }

    void displayHeader()
    {
        System.out.println("Project #03 Solution");
        System.out.println("Written by Jordan Hawkes");
    }

    void getStore() throws IOException
    {
        System.out.println("Enter Store: ");
        store = stdin.readLine();
        System.out.println("Enter Date: ");
        date = stdin.readLine();
        System.out.println("Enter Tax as Decimal (ex. .05): ");
        tax = Double.parseDouble(stdin.readLine());
    }

    void salesData() throws IOException
    {
        getTransNum();
        if (tNum != 0)
        {
            inputRa();
            calcSalesData();
            outputSalesData();
        }
    }

    void getTransNum() throws IOException
    {
        System.out.println("Enter Transaction Number: ");
        tNum = Integer.parseInt(stdin.readLine());
    }

    void inputRa() throws IOException
    {
        System.out.println("Enter Retail Amount: ");
        rA = Double.parseDouble(stdin.readLine());
    }

    void calcSalesData()
    {
        tCount = tCount + 1;
        raTot = raTot + rA;
        if (tA > 1500)
        {
            disc = true;
            dFlag = "Discounted";
        }
        else
        {
            disc = false;
            dFlag = "No";
        }

        transAmt();
        discTot();

        taTot = taTot +tA;

        smallTrans();
    }

    void transAmt()
    {
        if (disc = true)
        {
            tA = (rA * 0.95) + ((rA * 0.95) * tax);
        }
        else
        {
            tA = (rA * tax) + rA;
        }
    }

    void discTot()
    {
        if (disc = true)
        {
            disTot = disTot + (tA - rA);
        }
        else
        {
            disTot = disTot;
        }
    }

    void smallTrans()
    {
        if (tA < smallTa)
        {
            smallTa = tA;
            smallTnum = tNum;
        }
    }  

    void outputSalesData()
    {
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Transaction Number: " + tNum);
        System.out.println("Retail Amount: " + rA);
        System.out.println("Discount Flag: " + dFlag);
        System.out.println("Transaction Amount: " + tA);
        System.out.println("--------------------------------------------");
        System.out.println("");
    }

    void calcAvg()
    {
        taAvg = taTot / tCount;
    }

    void rptOut()
    {
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Store: " + store);
        System.out.println("Date: " + date);
        System.out.println("Tax Rate: " + tax);
        System.out.println("--------------------------------------------");
        System.out.println("Retail Amount Total: " + raTot);
        System.out.println("Discount Total: " + disTot);
        System.out.println("Transaction Amount Total: " + taTot);
        System.out.println("Average Transaction Amount: " + taAvg);
        System.out.println("Smallest Transaction Amount: " + smallTa);
        System.out.println("Smallest Transaction Number: " + smallTnum);
        System.out.println("--------------------------------------------");
    }
}

1 个答案:

答案 0 :(得分:1)

if (disc = true)

应该是

if (disc == true)

我认为你应该删除这个问题,因为这是一个错字。根据{{​​3}}