程序将无法正确显示输出

时间:2015-10-04 00:05:08

标签: java output

我正在编写一个程序,该程序应根据输入的某些数字显示输出。我最终得到一个输出,它完全是空白的,并且没有执行任何计算

/**
* 
* @NMATA003PA1.java
* @author Nicholas Mata
* @version 1.00 2015/09/25
* 
* Program Purpose: This program controls whether a customer can calculate the cost of his/her stock purchases.
* 
*/

import java.util.Scanner; //Scanner is imported to capture user input
import java.util.Calendar;//Calendare is imported to reflect the current date when necessary

public class MataN003PA1

{
    public static void main(String[] args)//Customer will be able to calculate costs of multiple trades
    {
        String customerName = "", date = "";//Customer name and date are declared as Strings
        int shares = 0, noOfStocks = 0;//shares and noOfShares are declared as integers, they are set to zero
        double sharePrice = 0, stockCost = 0, commission = 0, totalCost = 0, onlineFee = 0, totalStockCost = 0, totalCommissions = 0, totalOnlineFees = 0; 
        /**
        * sharePrice, stockCost, commission, totalCost, onlineFee, totalStockCost, totalCommissions, totalOnline Fees are declared
        * as double, they are also set to zero
        */
        char onlineTrade = ' ', brokerAssisted = ' ', another = ' '; // onlineTrade and brokerAssisted are declared as char variables, set to blank
        Calendar dateTime = Calendar.getInstance(); //dateTime is declared

        Scanner input = new Scanner(System.in);

        System.out.printf("%nYEE-TRADE, INC. - The Wild West of Electronic Trading"
        +"%n%nWelcome to Yee-Trade's stock purchase calculator.%n");

        System.out.printf("%n%nWhat is your name?  ");
        customerName = input.nextLine();

        System.out.printf("%nDo you want to calculate your stock purchases?  Enter 'Y' or 'N' to exit:  ");
        another = input.nextLine().charAt(0);

        while (Character.toUpperCase(another) == 'Y')
        {
            noOfStocks = noOfStocks + 1;

            System.out.printf("%nHow many shares did you purchase?  ");
            shares = input.nextInt();

            System.out.printf("%nWhat is the price per share?  ");
            sharePrice = input.nextDouble();
            input.nextLine();

            stockCost = shares * sharePrice;
            totalStockCost = totalStockCost + stockCost;
            totalCost = totalCost + stockCost;

            System.out.printf("%nIs this an online trade?  Enter 'Y' or 'N':  ");
            onlineTrade = input.nextLine().charAt(0);

            if (Character.toUpperCase(onlineTrade) == 'Y')
            {
                onlineFee = 5.95;
                totalOnlineFees = totalOnlineFees + onlineFee;
                totalCost = totalCost + onlineFee;
            }//END if_1
            else
            {
                System.out.printf("%nIs this a broker assisted trade?  Enter 'Y' or 'N':  ");
                brokerAssisted = input.nextLine().charAt(0);

                if (Character.toUpperCase(brokerAssisted) == 'Y')
                {
                    commission = stockCost * .02;
                    totalCommissions = totalCommissions + commission;
                    totalCost = totalCost + commission;

                }//END if_2
                else
                {
                    System.out.printf("%nINVALID TRADE TYPE!  %n");
                    noOfStocks = noOfStocks - 1;
                    totalStockCost = totalStockCost - stockCost;
                    totalCost = totalCost - stockCost;
                }//end else_2
            }//end else_1

            System.out.printf("%nEnter 'Y' to calculate the cost of another stock purchase or 'N' to exit:  ");
            another = input.nextLine().charAt(0);

        }//END WHILE LOOP

        if (noOfStocks > 0);
        { 
            System.out.printf("%nYEE-TRADE, INC."
            +"%nTOTAL COST OF STOCK PURCHASES"
            +"%nFOR %s", customerName);
            System.out.printf("%nAS OF ");
            System.out.printf("%1$TB %1$Td, %1$TY%n", dateTime);
            System.out.printf("%nTotal Stock Cost:  $", totalStockCost);
            System.out.printf("%nTotal Online Fees:  $", totalOnlineFees);
            System.out.printf("%nTotal Commissions:  $", totalCommissions);
            System.out.printf("%n%nTOTAL COST:  $", totalCost);

        }//END if_3

        System.out.printf("%nThank you for using Yee-Trade's stock purchase calculator!  ");

        noOfStocks = 0;



        System.exit(0);}//END MAIN
}//END CLASS

1 个答案:

答案 0 :(得分:1)

如果没有必要,你不应该总是尝试format输出。

这是你的问题:

        System.out.printf("%nAS OF ");
        System.out.printf("%1$TB %1$Td, %1$TY%n", dateTime);
        System.out.printf("%nTotal Stock Cost:  $" + totalStockCost);
        System.out.printf("%nTotal Online Fees:  $", totalOnlineFees);
        System.out.printf("%nTotal Commissions:  $", totalCommissions);
        System.out.printf("%n%nTOTAL COST:  $", totalCost);

您必须使用以下

替换其中大部分内容
        System.out.printf("%nAS OF ");
        System.out.printf("%1$TB %1$Td, %1$TY%n", dateTime);
        System.out.println("Total Stock Cost:  $" + totalStockCost);
        System.out.println("Total Online Fees:  $" + totalOnlineFees);
        System.out.println("Total Commissions:  $" + totalCommissions);
        System.out.println("TOTAL COST:  $" + totalCost);

以下是我得到的输出:

YEE-TRADE, INC. - The Wild West of Electronic Trading

    Welcome to Yee-Trade's stock purchase calculator.


    What is your name?  Yassin

    Do you want to calculate your stock purchases?  Enter 'Y' or 'N' to exit:  Y

    How many shares did you purchase?  50

    What is the price per share?  50

    Is this an online trade?  Enter 'Y' or 'N':  Y

    Enter 'Y' to calculate the cost of another stock purchase or 'N' to exit:  N

    YEE-TRADE, INC.
    TOTAL COST OF STOCK PURCHASES
    FOR Yassin
    AS OF OCTOBRE 04, 2015
    Total Stock Cost:  $2500.0
    Total Online Fees:  $5.95
    Total Commissions:  $0.0
    TOTAL COST:  $2505.95

    Thank you for suing Yee-Trade's stock purchase calculator!  
相关问题