0

I got a switch case having 15+ cases. The switch case trigger on an integer variable which increments by 1 on each execution and changes back to value 1 after all cases are executed and starting again. What will I do to make it so that my switch case trigger random cases and I don't want to start it from beginning again.. just execute cases randomly on each prompt.

code:

if(guns)
            {
                if(mygun <9){
                    mygun += 1;
                }else 
                {
                    mygun = 1;
                }
                switch(mygun){
                    case 1:
                        thegun = "︻デ═一";
                        break;
                    case 2:
                        thegun = "*-* ︻┳デ═—";
                        break;
                    case 3:
                        thegun = "▄︻̷̿┻̿═━一";
                        break;
                    case 4:
                        thegun = "(⌐■_■)--︻╦╤─ - - -";
                        break;
                    case 5: 
                        thegun = "︻╦̵̵͇══╤─";
                        break;
                    case 6:
                        thegun = "✯╾━╤デ╦︻✯";
                        break;
                    case 7:
                        thegun = " ̿̿ ( ▀ ͜͞ʖ▀)=€̿̿▄︻̷̿┻̿═━一";
                        break;
                    case 8:
                        thegun = "(⌐■_■)–︻╦╤─";
                        break;
                    case 9:
                        thegun = "╾━╤デ╦︻༼ಠ益ಠ༽︻╦̵̵͇══╤─";
                        break;
                }

}

knoxgon
  • 1,070
  • 2
  • 15
  • 31
  • 2
    You ask this like you want us to write your code for you. Provide some context around the problem you are experiencing by sharing the code you've already written. Random from 1 to 15 is `int rand = (int)(1 + Math.random() * 15);` or from 0 to 14 is `int rand = (int)(Math.random() * 15);` – ThisClark Mar 03 '18 at 07:51
  • added code in question –  Mar 03 '18 at 07:56
  • Your code have some awesome guns. – Ubercool Mar 03 '18 at 08:00
  • heheh.. I know, xD –  Mar 03 '18 at 08:01

5 Answers5

4

Assuming that you want to generate a random number between 1 and 15.

You could try these 2 methods:

Method 1 : Use java.util.Random class

Random rand = new Random();
switch(rand.nextInt(15)+1)// default range is from(0 to 14) +1 at the end makes the range from(1 to 15)
{
 // your cases here
}

This saves you from resetting the values for mygun variable.

Reference: java.util.Random class

Method 2:

Use java.lang.Math.random()

Reference: java.lang.Math

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
StabCode
  • 106
  • 1
  • 6
2

Just create a wrapper method for your switch case. This method will take an int as input and pass that input to the switch. Then pass in any random value to this method and watch it execute.

Prateek Paranjpe
  • 513
  • 3
  • 13
1

Use Math.random() to generate a random number. But Math.random() generates a random number from 0 to 1. So to generate a random number in a range, for example from 1 to 16, you can use: Math.floor((Math.random() * 16) + 1); and give this output as the input of the switch case.

Rohan Pillai
  • 917
  • 3
  • 17
  • 26
0

Simpleat way is, generate a random number and use this to identify which case to execute. There are multiple ways to generate random number in Java which can be used to do the job. Refer this question for more information.

Random r = new Random();
int Result = r.nextInt(20);
switch(result){
  case 1:
   ...
}
Ubercool
  • 1,029
  • 2
  • 14
  • 29
0

Just use Random class

Random random=new Random();

int random no=++random.nextInt(15);

This will generate random number. Use it in switch case.

OR

Use randomNo=++(Math.random*15)

Replace 15 by 1 less than the number of cases.