-2

この例では、生徒の名前を出力し、ユーザーがキーボードからベクターに入力したクレジットを表示します。

しかし、クレジットが30を超えるベクターのみを印刷したい.

助けてくれてありがとう。

public class Main {


    public static void main(String[] args) {
       Teacher t = new Teacher("Prof. Smith", "F020");
       Student s = new Student("Gipsz Jakab", 34);


       Vector<Person> pv = new Vector<Person>(); 
       pv.add(t);
       pv.add(s);

       Scanner sc = new Scanner(System.in);
       String name;
       int credits;


       for (int i=0;i<5;i++){

         System.out.print("Name: ");
         name = sc.nextLine();
         System.out.print("Credits: ");
         credits = sc.nextInt(); 
         sc.skip("\n"); 

         pv.add(new Student(name, credits));
       }
       System.out.println(pv); 
       System.out.println("The size of the Vector is: " + pv.size()); 
    }
}
4

4 に答える 4

1

イテレータを使用する必要があります。簡単な方法は次のとおりです。

Iterator it = pv .iterator();
while(it.hasNext()){
    Student s= it.next();
    if(s.credits>30) System.out.println(s);
}
于 2012-12-12T11:32:30.330 に答える
0

if ステートメントを使用する必要があります。信用度が 30 を超えていることを確認します。

if (x > n ) {
 // this block of code will be executed when x is greated then n.
}
于 2012-12-12T11:29:59.423 に答える
0

ベクトルに追加する前に確認する必要があります。ArrayListではなくベクトルを使用している理由は興味深い

 for (int i=0;i<5;i++){
     System.out.print("Name: ");
     name = sc.nextLine();
     System.out.print("Credits: ");
     credits = sc.nextInt(); 
     sc.skip("\n"); 

     if (credits >= 30) { //this additional check is needed
          pv.add(new Student(name, credits));
      } 
 }
于 2012-12-12T11:30:44.090 に答える
0

これは機能しますか?

if (credits > 30){
    pv.add(new Student(name, credits));
}

それ以外の:

pv.add(new Student(name, credits));
于 2012-12-12T11:30:52.470 に答える