我有如下所示创建多个整数对象到链表一类:Java的多台扫描仪
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;
}
我有扫描从控制台输入“添加”,如果发现了一个应用程序调用添加方法如下图所示:
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();
}
}
}
的appliation应该允许用户输入他们的愿望,但错误“没有行发现”,“添加”多次后出现add方法已被调用。
我猜这是因为该方法中的扫描仪尚未关闭,然后在需要时重新打开。这是什么错误的程序,如果是的话,我将如何去解决它?
请注意这个程序没有完成,因为我将添加销售这些股票的销售方法,这就是为什么我使用while循环的原因。
在S.add中,您可能会遇到与调用扫描器相关的问题? –