0

问题是要在不同的时间写第 5 段。每个段落都有不同的笼号和对应的动物。所以笼子 1 有一只狮子,笼子 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 ,你会得到这样的句子:“这个笼子里有一个 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 回答
0

删除第二个循环,在 switch 中使用 n 而不是 i 并在 switch 的每个分支中添加 break。

最好的方法是以正确的顺序创建包含动物名称的数组,而不是仅仅使用“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

您可以做一件事,而不是使用 if else 并在其中调用函数并从那里返回值

 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 回答