1

I am in need of a code, using which in my Android App, I want to go through all the cases of switch condition , (Not randomly!!!).I am able to move it randomly as follows.

public void switchingLogic(){
         Random rand = new random();
          int i = rand.nextInt(4)+1;
    switch (i) {
        case 1:
            setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
            selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
            selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
            break;
        case 2:
            setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
            selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
            selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
            break;
        case 3:
            setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
            selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
            selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
            break;
        case 4:
            setImages(R.drawable.ic_launcher, R.drawable.ic_launcher);
            selectedCorrectImage(img1, R.raw.correct_img, R.raw.thats_correct);
            selectedWrongImage(img2, R.raw.wrong_img, R.raw.no_its_wrong);
            break;
}

But now I want to just go in order from 1 through 4...

If the condition(Selected image) is correct in first case, I am showing Image in fullscreen and calling back the method again.

I just came across CURSOR . Can I use it(if yes , How?) or is there any way I can solve this. Important: I Want to use this "Switching Logic method" in onCreate()

sai
  • 548
  • 10
  • 30
  • in a switch case u will got only one value at a time..u have to do in side a loop..then check the condition inside the loop – Sam May 14 '13 at 09:17
  • means like for(i=0;i<4;i++){} Also see the edited question. – sai May 14 '13 at 09:19
  • Hi take a look at this link http://stackoverflow.com/questions/8563970/switch-without-break. – Kalai.G May 14 '13 at 09:24
  • for(i=0;i – Sam May 14 '13 at 09:25
  • Why do you use a switch if you want to run all the cases? I really don't see what you are trying to do, you should explain more what you have in mind... – Yoann Hercouet May 14 '13 at 12:03
  • "If the condition(Selected image) is correct in first case, I am showing Image in fullscreen and calling back the method again." Like this I want to show all cases. – sai May 15 '13 at 06:57

2 Answers2

1

If you dont set the break command in a switch-case, switch wont terminate after executing one case and move to the next one. so this code:

int i = 2;
switch(i) {
   case 1:
      // do something
   case 2:
      // do something
   case 3:
      // do something

}

will execute case 2 and case 3. Maybe you can implement an ifclause to define if the cases should break or not.

danijoo
  • 2,823
  • 4
  • 23
  • 43
0

You can try creating 2 methods to do this:

  1. a method to loop through all cases by using a for loop
  2. a method that contains your cases

Then just call

LoopThroughAllCases();

The code would look something like this.

    void LoopThroughAllCases()
    {
    int minCaseValue = 0; // modify to suit your case
    int maxCaseValue = 4; // modify to suit your case
    for (int i = minCaseValue ; i  <=  maxCaseValue ; i++)
                {

                    Cases(i);
                }
    }

    void Cases(int i)
            {
                switch (i)
                {
                    case 0:
                        // do something
                        break;
                      case 1:
                        // do something
                        break;
                      case 2:
                        // do something
                        break;
                      case 3:
                        // do something
                        break;
                      case 4:
                        // do something
                        break;
                      default:
                       //handle unknown value
                       break;

                }
            }
Shane.A
  • 106
  • 9