我有一个创建多个Integer
对象并将它们放入LinkedList
如下所示的类:
public class Shares<E> implements Queue<E> {
protected LinkedList<E> L;
public Shares() {
L = new LinkedList<E>();
}
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;
}
}
我有一个应用程序从控制台扫描输入“add”,如果找到,调用add
如下所示的方法:
public class Application {
private static Scanner scan;
public static <E> void main(String[] args) {
Queue<Integer> S = new Shares<Integer>();
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”,但在add
调用该方法后会出现错误“no line found”。
我猜这是因为Scanner
方法中的 没有关闭,然后在需要时重新打开。这是程序有什么问题吗?如果是,我将如何修复它?
请注意,此程序尚未完成,因为我将添加出售这些股票的出售方法。这就是我使用while循环的原因。