我是一名编程新手,所以请耐心等待。我搜索并找不到可以回答这个问题的现有主题。我写了下面的代码,它应该根据用户是将安全对象识别为股票还是债券来吐出stock.toString()或bond.toString()罐头短语。但是,我得到“安全无法解决”的编译错误。我想这是一个问题,因为安全对象的类没有在编译时定义。真的吗?如果是这样,有没有办法解决这个问题而不采用反思方法?谢谢!
public static void main(String[] args) {
double thePrice;
double theShares;
double theEarnings;
double theRate;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
Stock security = new Stock();
security.setEarnings(theEarnings);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
Bond security = new Bond();
security.setRate(theRate);
}
System.out.println("What is the price");
thePrice = in.nextDouble();
System.out.println("How many shares are there?");
theShares = in.nextDouble();
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
感谢@Jigur Joshi,@ penartur和其他人。这是我们提出的解决方案,但如果有更好的选择,请告诉我。我正在添加一个else语句来清理,以防securityType既不是“stock”也不是“bond”:)
public static void main(String[] args) {
...
Security security = null;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
System.out.println("What is the price");
thePrice = in.nextDouble();
System.out.println("How many shares are there?");
theShares = in.nextDouble();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully registered STOCK");
security = new Stock();
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
((Stock) security).setEarnings(theEarnings);
}
if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully registered BOND");
security = new Bond();
System.out.println("What is the rate?");
theRate = in.nextDouble();
((Bond) security).setRate(theRate);
}
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
答案 0 :(得分:6)
在if else声明它的一面,以便在if else之后它可用
假设Stock
是Bond
的超类,如果没有声明security
Object security = null;
成功
Stock security = null;
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
security = new Stock();
security.setEarnings(theEarnings);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
security = new Bond();
security.setRate(theRate);
}
查看强>
答案 1 :(得分:4)
问题不在于类型。
你在内部范围内定义了security
,它在外部(你在security.setPrice
处不存在。更糟糕的是,代码无法轻易修复,如{{当security
既不是“债券”也不是“股票”时,根本不会被定义(无论是否是内部范围)。
但是我想你想要做的是:
securityType
当然这是一个可怕的解决方案,但你必须首先澄清自己的任务。
答案 2 :(得分:1)
问题出在你的变量范围内。
security
仅在if / else块中定义。
您可以将代码更改为以下内容:
Object security; // you can use common super class of Bond and Stock (maybe Securiy?)
if (securityType.compareToIgnoreCase("stock") == 0) {
/* ... */
Stock stock = new Stock();
stock.setEarnings(theEarnings);
security = stock;
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
/* ... */
Bond bond = new Bond();
bond.setRate(theRate);
security = bond;
}
/* ... */
System.out.println(security);