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

时间:2015-07-23 08:22:40

标签: java for-loop printing

对于我的班级,我们必须将股票信息放入数组的元素中,并显示每个股票及其符号的变化百分比。我有它工作的地方,它将打印第一个对象五次与五个对象一次就像它想象的那样。

这是我的代码(说明在评论中):

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());
     }

}     }

2 个答案:

答案 0 :(得分:2)

您正在使用错误的变量来初始化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);),因为在初始化之后您没有对它执行任何操作。

答案 1 :(得分:0)

我的代码可能会遇到一些问题:

  • 如下所示,直接在数组中创建和存储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;
    }

}