【问题标题】:Is there a way to keep track of the elements and to loop the menu?有没有办法跟踪元素并循环菜单?
【发布时间】:2019-03-28 05:15:32
【问题描述】:

程序需要能够将人员输入队列并跟踪他们。用户有 3 个选项“A”将新人(int)输入队列,“N”让队列被处理,“Q”退出队列,然后显示队列中有多少人.我不太清楚如何循环和跟踪。

package pkg3650queue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;  // Import the Scanner class

public class Main {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
         Queue<Integer> line = new LinkedList<Integer>();
    Scanner input = new Scanner(System.in);
    Scanner addperson = new Scanner(System.in);

    String option;
do {
    System.out.println("Type A to add a person to the line (# of requests)\n"
            + "Type N to do nothing and allow the line to be processed\n"
            + "Type Q to quit the application\n");
    option = input.nextLine();

    if(option.equalsIgnoreCase("A")) {
            System.out.println("Enter a number to add a person to the line: ");
            int addtoLine = addperson.nextInt();
            line.add(addtoLine);
            System.out.println(line);
            System.out.println("There are " + line.size() + " people in the queue");
        }  else if (option.equalsIgnoreCase("N")) {
        if(line.isEmpty()){
            System.out.println("There are no elements in the line to be processed");
            System.exit(0);  
        }
        else{
            int requestsProccessed = line.remove();
            System.out.println(requestsProccessed);
            System.out.println(line);
            System.out.println("There are " + line.size() + " people in the queue");
            }
        }

    } while (!option.equalsIgnoreCase("Q"));

System.out.println("Q was chosen. The number of ppl in this queue are " + line.size());
}
}

【问题讨论】:

  • 与您的问题无关,但为了简化您的代码,您可以使用option.equals("q") || option.equals("Q") 而不是option.equalsIgnoreCase("q")。见corresponding doc
  • 您可以将所有代码(从问题的显示到结尾)放入一个循环中。使用布尔标志作为循环的条件。当用户输入 q 时,将标志设置为 false。

标签: java loops queue


【解决方案1】:

你的意思是如何循环用户输入?你可以使用do-while:

    String option;
    do {
        System.out.println("Type A to add a person to the line (# of requests)\n"
                + "Type N to do nothing and allow the line to be processed\n"
                + "Type Q to quit the application\n");
        option = input.nextLine();

        if(option.equalsIgnoreCase("A")) {
            // do something
        } else if (option.equalsIgnoreCase("N")) {
            // do something
        }

        // notice we don't need an if for 'Q' here. This loop only determines how many
        // times we want to keep going. If it's 'Q', it'll exit the while loop, where
        // we then print the size of the list.

        } while (!option.equalsIgnoreCase("Q"));

    System.out.println("Q was chosen. The number of ppl in this queue are " + line.size());

注意我没有测试这段代码,但它应该能让你走上正确的轨道。

还要注意,在这种情况下我们不需要System.exit(0),因为程序会自然结束。虽然也有例外,但您通常不想使用System.exit(0),而是希望找到让代码“完成自身”的方法。

【讨论】:

  • 谢谢它有效。但是我在尝试运行它时注意到另一个问题,每当我尝试使用“n”并让队列自己处理时,它只会从第一个 int 中减去 1,即 [5,3,2,1] -> n -> 4(仅显示队列中的第一个 int ),如果我添加另一个 int (6) ,它将显示 [5,3,2,1,6] 。有什么建议吗?
  • @Osama 我不确定你的意思。当用户选择 n 时,你希望它做什么?
  • 好的,让我们像程序员一样思考!您有一个 LinkedList 并且您想删除队列中的第一个 int。所以如果我们去这里:docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html 并检查哪种方法允许我们这样做(提示,它是pop())。所以你会这样做:int requestsProccessed = line.pop();
  • 另外,如果您认为我的回答对您有帮助,我将不胜感激:)
  • 抱歉,pop() 将删除最后放入的元素。由于您正在处理队列,因此您需要使用 removeFirst() 方法。
猜你喜欢
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2021-01-17
  • 2015-12-28
  • 2010-12-24
  • 1970-01-01
相关资源
最近更新 更多