Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
darth_bubbles

How can I get the total number of cases in a switch statement as an integer?

Recommended Posts

Why do you think you need this? If it sounds dumb (it does) you are probably doing it wrong.

 

What language?

Post your existing code using the code tags.

 

The number will never change anyway

 

just count them?

 

 

I have a switch statement and I want to get the total number of cases in it and assign it as a integer... How would I do this?

 

Thanks!

Link to post
Share on other sites

I have a switch statement and I want to get the total number of cases in it and assign it as a integer... How would I do this?

 

Thanks!

you just count them, that's really it

Link to post
Share on other sites
Posted · Original PosterOP

I don't know which programming language you are using, but is it really necessary to do that, can't you just count them yourself?

 

you just count them, that's really it

 

Why do you think you need this? If it sounds dumb (it does) you are probably doing it wrong.

 

What language?

Post your existing code using the code tags.

 

The number will never change anyway

 

just count them?

 

It's java. I want to randomly choose a case in the switch statement and I want to be able to add as many cases as I want later on so it's easy to expand.

switch(Math.floor(Math.random() * switchLength))		case 0: //something happens			break;		case 1:  //something happens			break;		case 2:  //somthing happens			break;		case 3: //something happens			break;		case 4:  //something happens			break;		case 5:  //something happens			break;
Link to post
Share on other sites

 

It's java. I want to randomly choose a case in the switch statement and I want to be able to add as many cases as I want later on so it's easy to expand.

switch(Math.floor(Math.random() * switchLength))		case 0: //something happens			break;		case 1:  //something happens			break;		case 2:  //somthing happens			break;		case 3: //something happens			break;		case 4:  //something happens			break;		case 5:  //something happens			break;

Generate a random number outside the switch statement then do

switch(put whatever your integer name that is accepting the random number here)
Link to post
Share on other sites
Posted · Original PosterOP

 

Generate a random number outside the switch statement then do

switch(put whatever your integer name that is accepting the random number here)

Ok but I can't get how many cases there are?

Link to post
Share on other sites
Posted · Original PosterOP

What? YOU are the one that are making the cases for them... so, count them yourself and you'll get the total? Even though you already know what it is anyway? 

 

Counting them is the only way.

Right.. Just wanted to know if there was a way because I will make more cases regularly.

Link to post
Share on other sites

This is extremely bad practice:

/* * Executes one randomly chosen case statement */public static void executeSwitch() {	executeSwitch(-1);}/* * When executeSwitch is called with a value of -1, every case, except -1, * will increment numberOfCases by 1. Then executeSwitch will be called  * using a random number between 0 and numberOfCases. *  * When executeSwitch is called with a value other than -1, the switch case * that matches that number will execute. */private static void executeSwitch(int caseToExecute) {	int numberOfCases = 0;	switch (caseToExecute) {	case -1:		// Do nothing		// A negative case value is used when counting the number of cases		// because Math.floor(Math.random() * numberOfCases) can never equal -1	case 0:		if (caseToExecute == -1) {			numberOfCases++;		} else {			// Do stuff			break;		}	case 1:		if (caseToExecute == -1) {			numberOfCases++;		} else {			// Do stuff			break;		}	case 2:		if (caseToExecute == -1) {			numberOfCases++;		} else {			// Do stuff			break;		}	case 3:		if (caseToExecute == -1) {			numberOfCases++;		} else {			// Do stuff			break;		}	}	if (caseToExecute == -1) {		executeSwitch((int) Math.floor(Math.random() * numberOfCases));	}}
Link to post
Share on other sites

Why not just using an array whose elements contain a method that carries out the desired operation?

That way you can always add more and can get the number of elements in the array.

 

As an added bonus you no longer need to change the original method.

Link to post
Share on other sites

Why not just using an array whose elements contain a method that carries out the desired operation?

That way you can always add more and can get the number of elements in the array.

 

As an added bonus you no longer need to change the original method.

 

This is precisely the solution I was about to suggest. Indexing an array is also a quicker operation.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×