当前位置: 代码迷 >> java >> 如何运行for循环以显示数组的不同元素?
  详细解决方案

如何运行for循环以显示数组的不同元素?

热度:39   发布时间:2023-08-02 11:05:14.0

对于我的班级,我们必须将股票信息放入数组的元素中,并用符号显示每种股票的变动百分比。 我让它工作到将第一个对象打印五次的位置,然后将五个对象打印一次,如其预期的那样。

这是我的代码(说明在注释中):

import java.util.Scanner;


public class StockTest{
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    /*         
    Make sureto complete the Stock class before you do the following items.
    */

    /* Step 1:
     * Instantiate two Stock objects with arguments of your choice(stock      symbol and name).
     * Using two set methods set the previousClosingPrice and curentPrice of two Stocks.
     * Display the percentage changed from previousClosingPrice to curentPrice of both stocks.
     */
    System.out.println("Input stock symbol");
    String symbol = input.next();
    System.out.println("Input stock name");
    String name = input.next();

    Stock stock = new Stock(symbol, name);

    System.out.println("Input previous price");
    Stock.setPreviousClosingPrice(input.nextDouble());

    System.out.println("Input current price");
    Stock.setCurrentPrice(input.nextDouble());

    System.out.println("the change percentage of the stock is " +   Stock.getChangePercent() + "%");
    /*
     * Step 2: Declare an array of 5 Stock objects. (Next three steps should be done in a loop)
     * Ask the user to input stock symbol, name, previousClosingPrice and curentPrice.
     * Initialize an array element with new Stock object using symbol and name that user input.
     * Using two set methods set the previousClosingPrice and curentPrice of each Stock element
     */
     Stock[] stockArray = new Stock[5];
     for(int i = 0; i < stockArray.length; i++){
        System.out.println("Please input stock symbol, name, previous price, and current price");
        String stockSymbol = input.next();
        String stockName = input.next();
        stockArray[i] = new Stock(symbol, name);
        Stock.setPreviousClosingPrice(input.nextDouble());
        Stock.setCurrentPrice(input.nextDouble());
     }


    /*
     * Step 4:  (this step should be done in a loop)
     * Display the percentage changed from previousClosingPrice to curentPrice of all stocks with their symbol.
     */
    for (int i = 0; i < stockArray.length; i++){
         System.out.println("Percentage changed of " + symbol + " " + Stock.getChangePercent());
     }

}}

您使用错误的变量来初始化Stock实例。

更改

    String stockSymbol = input.next();
    String stockName = input.next();
    stockArray[i] = new Stock(symbol,name);

    String stockSymbol = input.next();
    String stockName = input.next();
    stockArray[i] = new Stock(stockSymbol,stockName);

尚不清楚您创建的第一个Stock实例的用途是什么( Stock stock = new Stock(symbol, name); ),因为初始化后您什么都不做。

我看不到您的代码有什么问题:

  • 如下面一行所示,直接在数组中创建和存储Stock对象会产生开销,因为您还需要更新同一对象的剩余属性。

    stockArray[i] = new Stock(symbol, name);

  • 使用静态方法更新对象属性没有任何意义。 Stock.setPreviousClosingPrice(input.nextDouble()); Stock.setCurrentPrice(input.nextDouble());

  • 再次需要仔细使用扫描仪方法。

我尝试根据注释更正代码:

StockTest类:

import java.util.Scanner;

public class StockTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        /*
         * Make sureto complete the Stock class before you do the following
         * items.
         */

        /*
         * Step 1: Instantiate two Stock objects with arguments of your
         * choice(stock symbol and name). Using two set methods set the
         * previousClosingPrice and curentPrice of two Stocks. Display the
         * percentage changed from previousClosingPrice to curentPrice of both
         * stocks.
         */
        System.out.println("Input stock symbol");
        String symbol = input.nextLine();
        System.out.println("Input stock name");
        String name = input.nextLine();

        Stock stock1 = new Stock(symbol, name);

        System.out.println("Input previous price");
        stock1.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
        System.out.println("Input current price");
        stock1.setCurrentPrice(Double.parseDouble(input.nextLine()));

        System.out.println("Input stock symbol");
        symbol = input.nextLine();
        System.out.println("Input stock name");
        name = input.nextLine();

        Stock stock2 = new Stock(symbol, name);

        System.out.println("Input previous price");
        stock2.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
        System.out.println("Input current price");
        stock2.setCurrentPrice(Double.parseDouble(input.nextLine()));

        System.out.println("the change percentage of the stock is "
                + stock1.getChangePercent() + "%");
        System.out.println("the change percentage of the stock is "
                + stock2.getChangePercent() + "%");

        /*
         * Step 2: Declare an array of 5 Stock objects. (Next three steps should
         * be done in a loop) Ask the user to input stock symbol, name,
         * previousClosingPrice and curentPrice. Initialize an array element
         * with new Stock object using symbol and name that user input. Using
         * two set methods set the previousClosingPrice and curentPrice of each
         * Stock element
         */
        Stock[] stockArray = new Stock[2];

        for (int i = 0; i < stockArray.length; i++) {
            System.out
                    .println("Please input stock symbol, name, previous price, and current price");

            symbol = input.nextLine();
            name = input.nextLine();

            Stock stock = new Stock(symbol, name);
            stock.setPreviousClosingPrice(Double.parseDouble(input.nextLine()));
            stock.setCurrentPrice(Double.parseDouble(input.nextLine()));

            stockArray[i] = stock;
        }

        /*
         * Step 4: (this step should be done in a loop) Display the percentage
         * changed from previousClosingPrice to curentPrice of all stocks with
         * their symbol.
         */
        for (Stock stock : stockArray) {
            System.out.println("Percentage changed of " + stock.getSymbol()
                    + " " + stock.getChangePercent() + "%");
        }
    }
}

股票类别

public class Stock {

    private String name;
    private String symbol;
    private double previousClosingPrice;
    private double currentPrice;

    public Stock(String symbol, String name) {
        this.symbol = symbol;
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public String getSymbol() {
        return this.symbol;
    }

    public void setCurrentPrice(double currentPrice) {
        this.currentPrice = currentPrice;
    }

    public void setPreviousClosingPrice(double previousClosingPrice) {
        this.previousClosingPrice = previousClosingPrice;
    }

    public double getChangePercent() {
        return (this.currentPrice / this.previousClosingPrice - 1) * 100;
    }

}
  相关解决方案