【问题标题】:Cinema Queue Doesn't Work Java电影队列不工作 Java
【发布时间】:2015-12-08 20:30:12
【问题描述】:

我需要能够验证主类中的年龄,例如,如果将姓名和年龄添加到队列中,并且当该人试图离开时,如果年龄小于 18 岁,则会出现一条消息,显示“离开因为太年轻”,或者如果该人超过 18 岁,则会出现一条消息,说“人离开”

这是我的主要课程

  public static void main(String[] args)
  {
    Queue q = new Queue();


    Scanner k = new Scanner(System.in);
    System.out.print("Join (j), leave (l) or end (e)? ");
    String action = k.nextLine();
    while (!action.equalsIgnoreCase("e"))
    {
         if (action.equalsIgnoreCase("j"))
        {
            System.out.print("Enter Name ");
             String Name = k.nextLine();
             System.out.print("Enter Age : ");
             int Age = k.nextInt();
            Person p1 = new Person(Name,Age);
            q.add(p1);

            System.out.println(Name + " Age " + Age + " Joined");   
        } 

        else if (action.equalsIgnoreCase("l"))
        {
            if (!q.isEmpty())
            {
                System.out.println(q.remove() + " Person Left");
            } 
            else
            {
                System.out.println("Queue Empty");
            }
        } 
        else
        {
            System.out.println("Invalid operation");
        }
        System.out.print("Join (j), leave (l) or end (e)? ");
        action = k.nextLine();

我还有一个包含姓名和年龄的人员类,还有一个队列类。

【问题讨论】:

    标签: java arrays queue


    【解决方案1】:

    为什么不能只添加一个 if 语句?

    int Age = k.nextInt();
    Person p1 = new Person(Name, Age);
    q.add(p1);
    if (Age < 18) {
       q.remove(p1);
       System.out.println("left because too young");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2018-03-15
      • 2018-07-15
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多