android中的switch case问题

时间:2013-07-06 10:15:20

标签: java android cocos2d-iphone switch-statement cocos2d-android

为了在10 ProjectilesDestroyed之后增加等级,我设置了开关盒,其中10级射弹后的第1级被显示第2级被显示,但是从第2级开始,每增加2个射弹被破坏,并且等级增加超过10,我应该如何使它相差10来增加水平。 这就是我实施的方式

if (++_projectilesDestroyed > 5)
            {
                _projectilesDestroyed = 0;

            //  for(level=1; level<12; level++)
                switch(level)
                   {
                   case 1:
                       _projectilesDestroyed = 10;
                   System.out.println("case 1");
                                      break;
                   case 2:
                       _projectilesDestroyed = 20; 
                       System.out.println("case 2");
                       break;
                   case 3:
                       _projectilesDestroyed = 30;
                       System.out.println("case 3");
                       break;
                   case 4:
                       _projectilesDestroyed = 40;
                       System.out.println("case 4");
                       break;
                   case 5:
                       _projectilesDestroyed = 50;
                                         break;
                   case 6:
                       _projectilesDestroyed = 60;
                                    break;
                   case 7:
                       _projectilesDestroyed = 70;
                                   break;
                   case 8:
                       _projectilesDestroyed = 80;
                                      break;
                   case 9:
                       _projectilesDestroyed = 90;
                                    break;
                   case 10:
                       _projectilesDestroyed = 100;
                       System.out.println("case 10");
                       break;
                   default: break;
                   }
                 addLevel();

addLevel()方法。

public void addLevel() {
    level = level + 1;
                showLevel(level);
          }

即使我加上休息;在第2级的所有案例中,它每2个射弹被更新一次,但是当它达到10时我想要它。

5 个答案:

答案 0 :(得分:0)

在案件阻止之后,你错过了休息时间。

      case 1:
                   _projectilesDestroyed = 10;
                   System.out.println("case 1");
                   //  missing break here and in the following case staments

如果你没有在case之后放置break语句,那么下一个case语句将按顺序执行,依此类推。

答案 1 :(得分:0)

您忘记为每个break添加case语句。这将是一个堕落。

case 1:
   _projectilesDestroyed = 10; 
    System.out.println("case 1");
    break; // to prevent falling through

查看JLS 14.11

  

如果代码不是以这种方式逐个案例,那么应该使用break语句。

     

如果其中一个case常量等于表达式的值,那么我们说case匹配,并且switch块中匹配case标签之后的所有语句(如果有的话)按顺序执行。

答案 2 :(得分:0)

   case 1:
           _projectilesDestroyed = 10;
            System.out.println("case 1");
   break; // missing

每个案例都缺少break语句。

每个break语句都会终止封闭的switch语句。控制流继续切换块后面的第一个语句。 break语句是必要的,因为如果没有它们,switch块中的语句就会落空:

查看文档以获取更多信息

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

答案 3 :(得分:0)

通过........

根据switch docs

  

每个break语句都会终止封闭的switch语句。控制流继续切换块后面的第一个语句。

     

break语句是必要的,因为如果没有它们,switch块中的语句将会通过:

您必须在每个案例之后添加break

 switch(level)
                   {
                   case 1:
                       _projectilesDestroyed = 10;
                   System.out.println("case 1");
                   break;

答案 4 :(得分:0)

我猜您的问题出在第一个if语句中:

if (++_projectilesDestroyed > 5)

一旦你摧毁了超过5枚射弹,你将始终进入该区块并增加level。你可能需要一些不同的变量,比如这个

if(++_projectilesDestroyed > _projectilesRequired)

并根据上面的_projectilesRequired声明或使用switch-case

设置您的_projectilesRequired = level * 10