2

In order increase the levels after 10 ProjectilesDestroyed, i have set switch case, where for the 1st level after 10 projectiles Destroyed 2nd level is displayed but from 2nd level its increased for every 2 projectilesDestroyed and also the levels are increases beyond 10, how should i make it a difference of 10 to increases the levels. this is how i implemented

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() method.

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

Even though if i add break; on all the cases from level 2, its getting updated for every 2 projectilesDestroyed but i want when it reached 10.

samm
  • 482
  • 5
  • 14
  • In this case, if you are only assigning values based on your current level, you could use `_projectilesDestroyed = level * 10` or fill an array beforehand like this `int[] projectilesDestroyed = {10, 20, ... 100}` – Michael Lang Jul 06 '13 at 10:24
  • If I get you correctly, you are saying that your `level` is increased for every 2 projectiles destroyed. I think the problem now is not with your code posted but with the code which calls the `addLevel()` method. – Michael Lang Jul 06 '13 at 10:36
  • have you done with the code or still waiting for the answer ? – Akarsh M Jul 09 '13 at 05:07
  • Possible duplicate of [Why do we need break after case statements?](http://stackoverflow.com/questions/2710300/why-do-we-need-break-after-case-statements) – Raedwald Jan 24 '16 at 16:57

5 Answers5

0

YOu are missing break satements after case blocks.

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

If you don't put break statement after the case, then the next case statement will be executed sequentially and so on.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You have forgotten to add the break statements for each case. It will be a fall-through.

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

Look at JLS 14.11

If code is not to fall through case to case in this manner, then break statements should be used.

If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0
   case 1:
           _projectilesDestroyed = 10;
            System.out.println("case 1");
   break; // missing

You are missing break statement for every case.

Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through:

Check the docs for more info

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

fall through........

As per switch docs:

Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block.

The break statements are necessary because without them, statements in switch blocks fall through:

You have to add the break after the each case

 switch(level)
                   {
                   case 1:
                       _projectilesDestroyed = 10;
                   System.out.println("case 1");
                   break;
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • this works only for the first cases, from second levels are updated for every 2 ProjectilesDestroyed. @Baadshah – samm Jul 06 '13 at 10:28
0

I guess your problem is with the first if statement:

if (++_projectilesDestroyed > 5)

Once you have destroyed more than 5 projectiles, you will always enter the block and increase your level. You will probably need some different variables here, like this

if(++_projectilesDestroyed > _projectilesRequired)

and set your _projectilesRequired according to your switch-case statement above or using _projectilesRequired = level * 10

Michael Lang
  • 3,902
  • 1
  • 23
  • 37