gpt4 book ai didi

java - 从文本文件中将元素单独添加到队列中

转载 作者:行者123 更新时间:2023-12-01 10:59:09 25 4
gpt4 key购买 nike

我想向队列(链接)添加一个名称,一次从文本文件添加一个名称。如果用户选择选项 1,那么它应该从列表中获取下一个名称。

如果我想添加另一个名称,情况 1 不允许我输入另一个选择。

   int choice = console.nextInt();

FileReader names = new FileReader("Customer.txt");

Scanner lookup = new Scanner(names);

Queue a = new Queue();


String customerName;
// This loop was just to verify that
while(lookup.hasNextLine() ){ // It was actually reading

customerName = lookup.next();
System.out.println(customerName);

}


while(true){

switch(choice){

case 1:
customerName = lookup.next();//For some reason its not giving me
a.enqueue(customerName); // The choice to enter another number
break; //even though I made the case while(true)

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:
System.out.println(" Exiting......");
break;


default:

continue;


}

break;
}

最佳答案

这里的问题是 switch 语句后面有一个break。这会导致您的代码在执行一次 switch 语句后跳出 while 循环。

解决方案是删除中断,如下所示:

while(true){

switch(choice){

case 1:
customerName = lookup.next();//For some reason its not giving me
a.enqueue(customerName); // The choice to enter another number
break; //even though I made the case while(true)

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:
System.out.println(" Exiting......");
break;


default:

continue;


}

}

关于java - 从文本文件中将元素单独添加到队列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com