【问题标题】:How to output each stock?如何输出每只股票?
【发布时间】:2017-04-23 02:18:33
【问题描述】:

我必须执行这个程序,我必须显示每只股票的利润计算,但我还必须显示股票总量的利润。我的代码只有它,所以它显示了所有股票的计算:

import java.util.Scanner;

public class KNW_MultipleStockSales
{

  //This method will perform the calculations
  public static double calculator(double numberShare, double purchasePrice, 
                                  double purchaseCommission, double salePrice,
                                  double salesCommission)
  {
    double profit = (((numberShare * salePrice)-salesCommission) - 
                     ((numberShare * purchasePrice) + purchaseCommission));
    return profit;
  }

  //This is where we ask the questions
  public static void main(String[] args)
  {
    //Declare variables
    Scanner scanner = new Scanner(System.in);
    int stock;
    double numberShare;
    double purchasePrice;
    double purchaseCommission;
    double salePrice;
    double saleCommission;
    double profit;
    double total = 0;

    //Ask the questions
    System.out.println("Enter the stocks you have: ");
    stock = scanner.nextInt();

    //For loop for the number stock they are in
    for(int numberStocks=1; numberStocks<=stock; numberStocks++)
    {
      System.out.println("Enter the number of shares for stock " + numberStocks + ": ");
      numberShare = scanner.nextDouble();

      System.out.println("Enter the purchase price" + numberStocks + ": ");
      purchasePrice = scanner.nextDouble();

      System.out.println("Enter the purchase commissioned:" + numberStocks + ": ");
      purchaseCommission = scanner.nextDouble();

      System.out.println("Enter the sale price:" + numberStocks + ": ");
      salePrice = scanner.nextDouble();

      System.out.println("Enter the sales commissioned:" + numberStocks + ": ");
      saleCommission = scanner.nextDouble();

      profit = calculator(numberShare, purchasePrice, purchaseCommission,
                          salePrice, saleCommission);
      total = total + profit;
    }


      //Return if the user made profit or loss
      if(total<0)
      {
        System.out.printf("You made a loss of:$%.2f", total);
      }
      else if(total>0)
      {
        System.out.printf("You made a profit of:$%.2f", total);
      }
      else
      {
        System.out.println("You made no profit or loss.");
      }
  }
}

我怎样才能得到它,以便显示每只股票的利润,以及所有股票的利润?

【问题讨论】:

    标签: java for-loop methods


    【解决方案1】:

    尝试维护单独的损益图。您可能希望接受股票名称作为有助于有效管理单个股票的输入。

     // Map of stock name and profit/loss
     Map<String,Double> profitMap = new HashMap<String,Double>();
    

    计算盈亏后,添加入口到地图

    profitMap.put("stockName", profit);
    total = total + profit;
    

    在程序结束时,迭代并显示 Map 中每只股票的损益。

    for (Entry<String, Integer> entry : profitMap.entrySet()) {
            System.out.println("Stock Name : " + entry.getKey() + " Profit/loss" + entry.getValue());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2018-02-04
      • 2018-09-12
      相关资源
      最近更新 更多