2

为了在 10 个 ProjectilesDestroyed 后增加等级,我设置了 switch case,其中 10 个 projectiles Destroyed 后的第 1 级显示第 2 级,但从第 2 级开始,每 2 个 projectilesDestroyed 增加,并且级别增加到超过 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 个 projectilesDestroyed 更新,但我希望它达到 10 时。

4

5 回答 5

0

您在案例块之后缺少中断语句。

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

如果没有在 case 后面加上 break 语句,那么下一个 case 语句将按顺序执行,以此类推。

于 2013-07-06T10:16:23.853 回答
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 标签之后的所有语句(如果有的话)都按顺序执行。

于 2013-07-06T10:16:33.027 回答
0
   case 1:
           _projectilesDestroyed = 10;
            System.out.println("case 1");
   break; // missing

您在每种情况下都缺少break陈述。

每个 break 语句都会终止封闭的 switch 语句。控制流继续 switch 块之后的第一条语句。break 语句是必要的,因为没有它们,switch 块中的语句就会失败:

查看文档以获取更多信息

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

于 2013-07-06T10:16:41.640 回答
0

跌倒…………

根据开关文档

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

break 语句是必要的,因为没有它们,switch 块中的语句就会失败:

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

 switch(level)
                   {
                   case 1:
                       _projectilesDestroyed = 10;
                   System.out.println("case 1");
                   break;
于 2013-07-06T10:19:53.540 回答
0

我猜你的问题出在第一if条语句上:

if (++_projectilesDestroyed > 5)

一旦你摧毁了超过 5 个射弹,你将始终进入方块并增加你的level. 你可能需要一些不同的变量,像这样

if(++_projectilesDestroyed > _projectilesRequired)

_projectilesRequired根据您switch-case上面的陈述或使用_projectilesRequired = level * 10

于 2013-07-06T10:53:38.153 回答