我有一个类创建多个Integer对象并将它们放入LinkedList,如下所示:
public class Shares implements Queue {
protected LinkedList L;
public Shares() {
L = new LinkedList();
}
public boolean add(E price) {
System.out.println("How many of these shares would you like?");
Scanner scanInt;
scanInt = new Scanner(System.in);
Integer noShares = scanInt.nextInt();
for (int i = 0; i < noShares; i++) {
L.addLast(price);
}
scanInt.close();
return true;
}
}
我有一个应用程序从控制台扫描输入“添加”,如果找到,调用方法添加如下所示:
public class Application {
private static Scanner scan;
public static void main(String[] args) {
Queue S = new Shares();
scan = new Scanner(System.in);
System.out.println("Please type add");
String sentence = scan.nextLine();
while (sentence.equals("quit") == false) {
if (sentence.equals("add")) {
System.out
.println("What price would you like to buy your shares at?");
S.add((Integer) scan.nextInt());
} else
System.exit(0);
sentence = scan.nextLine();
}
}
}
应用程序应允许用户根据需要多次输入“添加”,但在调用add方法后出现错误“no line found”.
我猜这是因为方法中的扫描仪尚未关闭,然后在需要时重新打开.这是程序出了什么问题,如果是这样,我该如何修复它?
请注意,此程序尚未完成,因为我将添加销售这些股票的销售方法.这就是我使用while循环的原因.