有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的数组只打印出文件中的最后一个元素,我希望它打印出所有元素

我已经将要点链接到stockData txt文件以及我的程序运行时显示的内容。 还有,我不担心totalChangepercent返回0.0%,因为我还没有完成,但我没有进一步解决这个问题

是的,这是一个家庭作业,但我做了两个类似的作业,没有任何问题,所以我不知道还能做什么

编程运行:

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

Symbol: AAPL
Stock: Apple Inc
Previous Closing Price: $100.8
Current Price: $102.3
Total Change Percent: 0.0%

股票数据。txt:

GPRO  
GoPro, Inc.
89.93
89.8773
SBUX
Starbucks
75.26
75.76
JCP
JC Penney
8.18
7.72
AMZN
Amazon
323.71
319.94
AE
Adams Resources and Energy
44.71
44.69
CEP
Constellation Energy Partners
3.38
3.35
KO
Coca-Cola
43.66
44.44
MCD
McDonald's
92.81
93.53
TSLA
Tesla Motors
259.28
264.57
AAPL
Apple Inc
100.80
102.30

public class Stock
{
private static String symbol;
private static String stockName;
private static double previousClosingPrice, currentPrice;
private static double totalChangePercent;
public Stock ()
{
    symbol = " ";
    stockName = " ";
    previousClosingPrice = 0.0;
    currentPrice = 0.0;
} //end default

public Stock(String symbol, String stockName, double previousClosingPrice, double currentPrice)
{
    this.symbol = symbol;
    this.stockName = stockName;
    this.previousClosingPrice = previousClosingPrice;
    this.currentPrice = currentPrice;
} //end overloaded

//setters

public void setSymbol(String symbol)
{
    symbol = symbol;
}  //end setSymbol();

public void setStock(String stockName)
{
    stockName = stockName;
}  //end setStock();

public void setPreviousclosingPrice(double previousClosingPrice)
{
    previousClosingPrice = previousClosingPrice;
}  //end setPreviousclosingPrice();

public void setCurrentPrice(double currentPrice)
{
    currentPrice = currentPrice;
}  //end setCurrentPrice();

//getters

public String getSymbol()
{
    return symbol;
} //end getSymbol();

public String getStockName()
{
    return stockName;
} //getStock();

public double getPreviousClosingPrice()
{
    return previousClosingPrice;
} //end getPreviousClosingPrice()

public double getCurrentPrice()
{
    return currentPrice;
} //end getCurrentPrice()

public static double getChangePercent()
{
    totalChangePercent = ((currentPrice - previousClosingPrice) / previousClosingPrice);

    return totalChangePercent;
} //end getChangePercent();

public boolean equals(Stock bool)
{
    if(symbol.equals(bool.getSymbol()) && stockName.equals(bool.getStockName()) && previousClosingPrice == bool.getPreviousClosingPrice() && currentPrice == bool.getCurrentPrice())
      {
        return true;
      } else
      {
        return false;
      }
}

public String toString()
{
    String str = " ";
    str += "\nSymbol: " + symbol + "\nStock: " + stockName;
    str += "\nPrevious Closing Price: $" + previousClosingPrice + "\nCurrent Price: $" + currentPrice;
    str += "\nTotal Change Percent: " + totalChangePercent + "%";

    return str;
}
}




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

public class StockDriver
{
public static void main(String args[]) throws IOException
{
    Stock [] stockData = new Stock [10];

    setStockData(stockData);
    displayStockData(stockData);
    displayImprovedStocks(stockData);
} //end main

public static void setStockData(Stock [] stockData) throws IOException
{

    File fileIn = new File("stockdata.txt");
    Scanner scan = new Scanner(fileIn);

    if(!fileIn.exists())
    {
        System.out.println("No file found!");
        System.exit(0);
    } //end if -- ends check

    String symbol, stockName;
    double previousClosingPrice, currentPrice;
    int i = 0;

    while(scan.hasNext()&& i < stockData.length)
    {
        symbol = scan.nextLine();
        stockName = scan.nextLine();
        String postPriceStr = scan.nextLine();
        String currentPriceStr = scan.nextLine();
        previousClosingPrice = Double.parseDouble(postPriceStr);
        currentPrice = Double.parseDouble(currentPriceStr);
        stockData[i] = new Stock(symbol, stockName, previousClosingPrice, currentPrice);
        i++;
    } //end while
} //end setStockData()

public static void displayImprovedStocks(Stock [] stockData)
{
    DecimalFormat decFor = new DecimalFormat("#0.00");
    double totalImproved = 0.0;
    double postImproved = 0.0;
    double doMath = 0.0;

    for (int i = 0; i < stockData.length; i++)
    {
        postImproved += stockData[i].getPreviousClosingPrice();
        if (postImproved >= stockData[i].getCurrentPrice())
        {
            doMath = postImproved;
        } else
        {
            System.out.println(" ");
        } //end

        totalImproved = doMath;
        System.out.println("\nImproved Stocks: " + decFor.format(totalImproved));
    }


}
public static void displayStockData(Stock [] stockData)
{
    for(int i = 0; i <= stockData.length; i++)
    {
        System.out.println(stockData[i]);
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    所有类变量都是静态的,这意味着它们在类的所有实例中共享。相反,试着让他们像这样:

    public class Stock
    {
    private String symbol;
    private String stockName;
    private double previousClosingPrice, currentPrice;
    private double totalChangePercent;