切换声明

时间:2012-10-22 12:50:42

标签: java switch-statement

问题是要写第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");
              }                   
        } 

    }
}

8 个答案:

答案 0 :(得分: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.");
             }

答案 1 :(得分: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]);
        }

答案 2 :(得分:0)

如果你按照你的要求打印i,你会得到一句话:'这个笼子拿着1'。我怀疑这不是你想要的。而是创建一个变量来保存动物的名称并在switch语句中赋值。之后,您可以执行println语句。

这对我来说就像是一项家庭作业,所以我会让你弄清楚如何实现这一点:)

答案 3 :(得分: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; 

}

答案 4 :(得分:0)

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

最好的方法是以正确的顺序创建包含动物名称的数组,而不仅仅使用“n”作为索引来获取正确的动物名称

答案 5 :(得分: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.");              

答案 6 :(得分:0)

你可以做一件事而不是swicth使用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"; 
           }

       }                   
  } 

我希望这会...但是它可以比这个解决方案更好,但是现在这应该工作休息你可以修改。 祝你好运

答案 7 :(得分: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.");

}

我不确定我是否正确理解了你的问题。上面的代码对你有帮助吗?