0

問題は、段落を5回書くことです。各段落には、異なるケージ番号と対応する動物があります。つまり、ケージ1にはライオンがいて、ケージ2にはトラがいます。問題は、同じ段落でケージ番号と対応する動物の両方を組み合わせる方法がわからないことです。

段落の2行目にswitchステートメントを入力する方法がわかりません。println("このケージは"+ i);を書いてみました。しかし、Eclipseは私にエラーを出しました。変数nとiの両方を同じ段落に同時に組み込むにはどうすればよいですか?

import acm.program.*;

public class ZooAnimals extends ConsoleProgram {
    private static final int START = 1; 

    public void run(){
        for (int n = START; n <=5; n++ ) {
             println("This animal is in cage" + n);
             println("This cage holds a " );  <---- type of animal goes in here. 
             println("Wild animals are very dangerous.");
        }       

        for(int i = START; i<=5; i++) {
              switch(i) { 
                   case 1: println("lion");
                   case 2: println("tiger");
                   case 3: println("elephant");
                   case 4: println("snakes");
                   case 5: println("hippo");
              }                   
        } 

    }
}
4

8 に答える 8

3

私は次のような小さなメソッドを書きます:

public String getAnimal(int cage)
{
     switch(cage) { 
           case 1: return "lion";
           case 2: return "tiger";
           case 3: return "elephant";
           case 4: return "snakes";
           case 5: return "hippo";
           default: return "Animal Not Found!";
           }    

}

次に、このコードを置き換えます。

for (int n = START; n <=5; n++ ) {
  println("This animal is in cage" + n);
  println("This cage holds a " );     <-----------type of animal goes in here. 
  println("Wild animals are very dangerous.");
             }

これとともに:

for (int n = START; n <=5; n++ ) {
  println("This animal is in cage" + n);
  println("This cage holds a " + getAnimal(n));     <-----------type of animal goes in here. 
  println("Wild animals are very dangerous.");
             }
于 2012-10-22T12:56:14.670 に答える
1

配列を使用します

public class ZooAnimals extends ConsoleProgram {
    String[] animals = "none,lion,tiger,elephant,snake,hippo".split(",");

    public void run() {
        for (int n = START; n < animals.length; n++) {
            println("This animal is in cage" + n);
            println("This cage holds a " + animals[n]);
            println("Wild animals are very dangerous.");
        }

        for (int i = START; i < animals.length; i++) {
             println(animals[i]);
        }
于 2012-10-22T12:56:43.283 に答える
0

サイクルの秒を削除します。スイッチでは、iの代わりにnを使用し、スイッチの各ブランチにブレークを追加します。

正しい動物の名前を取得するためのインデックスとして「n」を使用するよりも、正しい順序で動物の名前を含む配列を作成するのが最善です。

于 2012-10-22T12:59:50.193 に答える
0

printの代わりにメソッドを使用する必要がありprintlnます。前の行を参照してくださいswitch

そして、breakスイッチを使用するときに使用します。

for (int n = START; n <=5; n++ ) 
{   
    println("This animal is in cage" + n);   
    print("This cage holds a " );

    switch(n) 
    {             
        case 1: println("lion"); break;        
        case 2: println("tiger"); break;            
        case 3: println("elephant");  break;           
        case 4: println("snakes");  break;           
        case 5: println("hippo");  break;
        default: println("unknown"); break; // good practice to add default case.
    }
}

println("Wild animals are very dangerous.");              
于 2012-10-22T13:00:01.900 に答える
0

それ以外の場合は、代わりに1つのことを実行して、その中で関数を呼び出し、そこから値を返すことができます。

 for(int n = START;n<=5;n++){                     
        String animal=type();
        println("This animal is in cage" + n);
        println("This cage holds a "+animal);     <-----------type of animal goes in here. 
        println("Wild animals are very dangerous.");
 }

 for(int i = START; i<=5; i++) {
       switch(i) { 
           if(i==1){
                String type()
                    return "tiger";
           }else if(i==2){
                  String type()
                    return "lion"; 
           }

       }                   
  } 

これがうまくいくことを願っています...nはい、このソリューションよりもはるかに優れている可能性がありますが、今のところ、これは機能するはずです。幸運を

于 2012-10-22T13:00:17.623 に答える
0
for (int n = START; n <=5; n++ ) {
  println("This animal is in cage" + n);
  println("This cage holds a " );     <-----------type of animal goes in here. 

       switch(n) { 
       case 1: println("lion");
       case 2: println("tiger");
       case 3: println("elephant");
       case 4: println("snakes");
       case 5: println("hippo");
       }                  

  println("Wild animals are very dangerous.");

}

あなたの質問を正しく理解しているかどうかわかりません。上記のコードは役に立ちますか?

于 2012-10-22T13:03:44.663 に答える
0

あなたが尋ねたように i を印刷すると、「このケージは 1 を保持しています」という文が得られます。これはあなたが求めているものではないと思います。代わりに、動物の名前を保持する変数を作成し、switch ステートメントでその値を割り当てます。その後、println ステートメントを実行できます。

これは私には宿題のように見えるので、それを正確に実装する方法を理解するためにあなたを残します:)

于 2012-10-22T12:56:12.880 に答える
0

私はあなたがこれをやろうとしていると思います

 import acm.program.*;

public class ZooAnimals extends ConsoleProgram {

public void run(){

for (int n = START; n <=5; n++ ) {
  println("This animal is in cage" + n);
  println("This cage holds a " );     <-----------type of animal goes in here. 

           switch(n) { 
           case 1: println("lion");break;
           case 2: println("tiger");break;
           case 3: println("elephant");break;
           case 4: println("snakes");break;
           case 5: println("hippo");break;
               }                   

println("Wild animals are very dangerous.");
              }

private static final int START = 1; 

}

于 2012-10-22T12:58:26.977 に答える