2016-12-13 3 views
0

私はシネマプロジェクトをコーディングしようとしてきましたが、私はそれを理解できません。私がプログラムを走らせるとき、setAgeに最後に入力された値しか出力されないので、正しい年齢でなくても彼の前の誰もが映画館に入ることができます。私はそれが配列ではない人物のオブジェクトと関係があると信じていますが、私はどのようにこれを解決するかわかりません、任意のアイデア?シネマプロジェクトとオブジェクト配列

import java.util.Scanner; 


public class Cinema 
{ 
    public static void main(String[] args) 
    { 
     QueueWithArray q = new QueueWithArray(); 

     Person person = new Person(); 

     Scanner kybd = new Scanner(System.in); 
     System.out.print("Join (j), leave (l) or end (e)? "); 
     String action = kybd.nextLine(); 
     while (!action.equalsIgnoreCase("e")) 
     { 
      if (action.equalsIgnoreCase("j")) 
      { 
       System.out.print("Enter your name: "); 
       String name = kybd.nextLine(); 
       person.setName(name); 
       q.add(name); 
       System.out.println("What is your age? "); 
       int age = kybd.nextInt(); 
       person.setAge(age); 
       kybd.nextLine(); 

       System.out.println(name + " is going to the cinema and is " + age + " years old."); 
      } 
      else if (action.equalsIgnoreCase("l")) 
      { 
       if (!q.isEmpty()) 
       { 
        if (person.getAge() >= 15) 
        { 
         System.out.println(q.remove() + " has left the queue and entered the cinema"); 

        } 
        else 
        { 
         System.out.println(q.remove() + " has left the queue, but is not old enough to watch the film"); 
        } 
       } 
       else 
       { 
        System.out.println("Queue empty"); 
       } 
      } 
      else 
      { 
       System.out.println("Invalid operation"); 
      } 
      System.out.print("Join (j), leave (l) or end (e)? "); 
      action = kybd.nextLine(); 
     } 
    } 

} 

答えて

0

人の年齢を格納する2番目のQueueWithArrayが必要です。

年齢のある紙を入れることができるピジョンホールがあるとします。p.setAge(年齢)を行うたびに、ピジョンホールから紙を取り出して新しい指定された年齢のピースイン。

休暇になると、毎回、同じ人物を1人ずつ読みます。代わりに、あなたは年齢を格納するので、名前ごとに年齢を格納する必要があります。

System.out.print("Enter your name: "); 
String name = kybd.nextLine(); 
System.out.println("What is your age? "); 
int age = kybd.nextInt(); 
qName.add(name); 
qAge.add(age); 
kybd.nextLine(); 

またはそれを行うには良い方法のような

何か:

System.out.print("Enter your name: "); 
String name = kybd.nextLine(); 
System.out.println("What is your age? "); 
int age = kybd.nextInt(); 
Person person = new Person(name, age); 
qPerson.add(person); 
kybd.nextLine(); 
+0

こんにちは、これは今、私はラインのPerson人=新しい人を(持っていることは良い答えですが、名前年齢);パラメータがあるので受け付けません – Luke

+0

パラメータを受け入れるpersonオブジェクトのコンストラクタを作成する必要があります。だから、パブリックPerson(String name、int age){this.name = name; this.age = age;} –

+0

私はこれをしましたが、現在は、qPersonを試して宣言するときに人が文字列に変更できないというエラーを投げています – Luke

関連する問題