checkValidity方法将所有值显示为无效,而不是仅显示无效值

时间:2015-03-07 16:33:31

标签: java

我有一种感觉我的问题出现在checkValidity方法的if语句中,但是我无法弄清楚我到底出错的地方。

这里是文件中使用的数据值

很抱歉,如果值看起来很紧凑

戴尔:戴尔公司

125 25.567 0.025 28.735 0.025

MSFT:Microsoft

34.1 -15.75 0.012 15.90 0.013

GOOG:谷歌

56.5 58.125 0.032 67.975 0.030

IBM:IBM Corp

87.3 8.875 0.015 7.500 0.020

DTLK:DataLink

345 23.250 0.055 25.750 0.050

CSCO:思科

90 14.570 1.025 16.890 1.024

INTC:英特尔

89.1 78.120 0.042 99.355 0.042

BGUS:Bogus Corp

0 25.567 0.012 25.678 0.023

import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;

public class Profit
{

    public static void main(String[] args) throws IOException
    {
        String input;                                     //string for input from opened file
        //Boolean TFStatement;                            //true/ false statement for validity
        int shares = 0;                                       //number of stocks
        String company;                                   //name of the company
        double sellPrice = 0;                                 //sell price per stock
        double buyPrice = 0;                                  //purchase price per stock
        double sellTotal = 0;                                 //selling total w/o factoring in commission amount
        double buyTotal = 0;                                  //purchase total w/o factoring in buying commission amount
        double sellCommissionPercentage = 0;                  //commission percentage for selling the stock
        double buyCommissionPercentage = 0;                   //commission percentage for buying the stock
        double profit = 0;                                    //total profit after buying and selling stocks
        double buyCommission = 0;                             //total purchase commission amount
        double sellCommission = 0;                            //total selling commission amount
        DecimalFormat formatPercent = new DecimalFormat("$#,##0.00%");   //format of currency for the profit

        //Scanner for keyboard input
        Scanner keyboard = new Scanner(System.in);

        //Get file
        System.out.print("Sean Harris\n\n" + "File name: ");
        String filename = keyboard.nextLine();

        //Open file
        File stockFile = new File(filename);
        Scanner inputFile = new Scanner(stockFile);


        //loop until fils has no more lines to read
        while (inputFile.hasNext())
            {
                company = inputFile.nextLine();
                shares = inputFile.nextInt();
                buyPrice = inputFile.nextDouble();
                buyCommissionPercentage = inputFile.nextDouble();
                sellPrice = inputFile.nextDouble();
                sellCommissionPercentage = inputFile.nextDouble();

                if (checkValidity(company, shares, buyPrice, buyCommissionPercentage, sellPrice, sellCommissionPercentage) == (false))
                    calcProfit(shares, buyPrice, buyCommissionPercentage, sellPrice, sellCommissionPercentage, profit, buyCommission, sellCommission, sellTotal, buyTotal);
                    display(company, profit);
            }
    }

    /**
        Check if the info in the file is valid
    */
    public static boolean checkValidity(String theCompany,int theShares, double theBuyPrice, double theBuyCommissionPercentage, double theSellPrice, double theSellCommissionPercentage)
    {
        //checking all of the values to see if they are valid

        Boolean TFStatement;

        TFStatement = (theShares <= 0 || theBuyPrice < 0 || theBuyCommissionPercentage < 0 || theBuyCommissionPercentage > .20 || theSellCommissionPercentage < 0 || theSellCommissionPercentage > .20 || theSellPrice < 0 ? true : false);
        /*
        if (theShares <= 0 || theBuyPrice < 0 || theBuyCommissionPercentage < 0 || theBuyCommissionPercentage > .20 || theSellCommissionPercentage < 0 || theSellCommissionPercentage > .20 || theSellPrice < 0)
            TFStatement = true;
        else
            TFStatement = false;
        */

        //displaying the invalid message if a value is invalid
        if (theShares <= 0)
            System.out.println(theCompany);
            System.out.println("Number of shares invalid: " + theShares);

        if (theBuyPrice < 0)
            System.out.println(theCompany);
            System.out.println("Purchase price invalid: " + theBuyPrice);

        if (theBuyCommissionPercentage < 0 || theBuyCommissionPercentage > .20)
            System.out.println(theCompany);
            System.out.println("Purchase commission percent invalid: " + theBuyCommissionPercentage);

        if (theSellPrice < 0)
            System.out.println(theCompany);
            System.out.println("Sales price invalid: " + theSellPrice);

        if (theSellCommissionPercentage < 0 || theSellCommissionPercentage > .20)
            System.out.println(theCompany);
            System.out.println("Sales commission percent invalid: " +theSellCommissionPercentage);

        return TFStatement;
    }

    /**
        Calculate profit
    */
    public static double calcProfit(int theShares, double theBuyPrice, double theBuyCommissionPercentage, double theSellPrice, double theSellCommissionPercentage, double theProfit, double theBuyCommission, double theSellCommission, double theSellTotal, double theBuyTotal)
    {
        //getting the total price of shares sold and bought
        //then figure out commission
        //then use the four values to calculate profit
        theSellTotal = theSellPrice * theShares;
        theBuyTotal = theBuyPrice * theShares;
        theSellCommission = theSellTotal * theSellCommissionPercentage;
        theBuyCommission = theBuyTotal * theBuyCommissionPercentage;
        theProfit = (theSellTotal - theSellCommission) - (theBuyTotal + theBuyCommission);
        return theProfit;
    }

    /**
        Display company name and profit
    */
    public static void display(String theCompany, double theProfit)
    {
        System.out.println(theCompany);
        System.out.println(theProfit);
    }
}

1 个答案:

答案 0 :(得分:0)

以此声明为例:

    //displaying the invalid message if a value is invalid
    if (theShares <= 0)
        System.out.println(theCompany);
        System.out.println("Number of shares invalid: " + theShares);

也许你来自Python背景,但在Java中,缩进并不表示嵌套。它只是一个可读性辅助工具。 if中唯一的语句是紧随其后的语句。在这种情况下:System.out.println(theCompany);

如果您希望多个语句属于同一个if,则必须将它们放在花括号中,使它们成为“复合语句”:

    if (theShares <= 0) {
        System.out.println(theCompany);
        System.out.println("Number of shares invalid: " + theShares);
    }

这适用于代码中所有类似的if语句。


另一个,无关,请注意:

将布尔值与false进行比较没有意义。相当于

if ( checkValidity(...) == (false) ) ...

简单地说:

if ( ! checkValidity() ) ...

本身并不是错误,但它的可读性较差。布尔可以在条件下直接使用。

出于同样的原因,表达式如下:

condition1 || condition2 || condition3 ? true : false

多余。它只相当于

condition 1 || condition2 || condition3

再次 - 这没有错,只是不可读。布尔值是布尔值。