如何在Class Stock中创建用户定义的输入

时间:2016-10-24 18:05:49

标签: java oop

我在我的程序中遇到用户定义的方法时遇到问题,如果有人能帮助我会很好吗

package Ex_9_2;



public class TestStock {
public static String price;

//main
public static void main(String[] args) {

    Stock stock = new Stock("ORCL", "Oracle Corporation");

    System.out.println("Please enter previous closing price ");
    stock.setPreviousClosingPrice(34.5);

    System.out.println("Please enter current price ");
    stock.setCurrentPrice(34.35);
    System.out.println(stock);

    // Display stock info
    System.out.println("Previous Closing Price: " + stock.getPreviousClosingPrice());
    System.out.println("Current Price: " + stock.getCurrentPrice());
    System.out.println("Price Change: " + stock.changePercent());
 }
}

这是我的另一个班级

package Ex_9_2;

public class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;

public Stock() {
}

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

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

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

public double getPreviousClosingPrice() {
    return previousClosingPrice;
}

public double getCurrentPrice() {
    return currentPrice;
}

public void setSymbol(String symbol) {
    this.symbol = symbol;
}

public void setName(String name) {
    this.name = name;
}

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

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

public double changePercent() {
    return (currentPrice - previousClosingPrice) / previousClosingPrice;
}

@Override
public java.lang.String toString(){
    return "\nYour Company's name is " + this.name
            + "\nYour Company's Symbol is " + this.symbol;

}

我似乎无法在我的主要用户输入此问题:

System.out.println("Please enter previous closing price ");
stock.setPreviousClosingPrice(34.5);

和此:

System.out.println("Please enter current price ");
stock.setCurrentPrice(34.35);

请帮忙,

1 个答案:

答案 0 :(得分:1)

您可以使用 java.util.Scanner 类从用户处获取输入。 在主函数中声明一个Scanner变量:

Animals <- c("giraffes", "orangutans", "monkeys")
Count <- c(20, 14, 23)
data <- data.frame(Animals, Count)

data <- arrange(data,desc(Count))

plot_ly(data, x = ~Animals, y = ~Count, type = 'bar', name = 'SF Zoo')

然后使用scan.nexDouble()从用户那里获取输入。例如:

Scanner scan = new Scanner(System.in);