blake
blake

Reputation: 59

Multi-Variable Switch Statement with "and"/"or" JAVA

I am new to coding an am working on coding a game, where each player has four stats: str, con, dex, and int. With a specific race of player, the user chooses two stats that increase by two, and in turn the other two stats get decreased by two. Currently I am determining which stats should by decreased based on what was chosen to increase. stat1 is the name of the first stat the user increases, and stat2 is the name of the second stat, and these two stats are taken from user input. This how I currently have it as an if/else if/else. I was wondering if it could be written easily as a switch statement.

if((stat1.startsWith("con") && stat2.startsWith("dex"))||(stat1.startsWith("dex") && stat2.startsWith("con")))
{
    addStat("int",-2);
    addStat("str",-2);
}
else if((stat1.startsWith("con") && stat2.startsWith("int"))||(stat1.startsWith("int") && stat2.startsWith("con")))
{
    addStat("dex",-2);
    addStat("str",-2);
}
else if((stat1.startsWith("con") && stat2.startsWith("str"))||(stat1.startsWith("str") && stat2.startsWith("con")))
{
    addStat("dex",-2);
    addStat("int",-2);
}
else if((stat1.startsWith("dex") && stat2.startsWith("int"))||(stat1.startsWith("int") && stat2.startsWith("dex")))
{
    addStat("con",-2);
    addStat("str",-2);
}
else if((stat1.startsWith("dex") && stat2.startsWith("str"))||(stat1.startsWith("str") && stat2.startsWith("dex")))
{
    addStat("int",-2);
    addStat("con",-2);
}
else if((stat1.startsWith("str") && stat2.startsWith("int"))||(stat1.startsWith("int") && stat2.startsWith("str")))
{
     addStat("dex",-2);
     addStat("con",-2);
}

Upvotes: 2

Views: 107

Answers (3)

SpaceTrucker
SpaceTrucker

Reputation: 13556

If you would define an enum and have stat1 and stat2 of this enum type than your code could be as simple as:

public enum Stat {
    DEX,
    INT,
    STR,
    CON;
}

for(Stat stat : EnumSet.complementOf(EnumSet.of(stat1,stat2))) {
    addStat(stat,-2);
}

If you find yourself writing such long if-else-trains you should always make a step backwards and reconsider your design.

And maybe it would be better not to have two separate variables stat1 and stat2 and just represent it as a collection right from the beginning.

Upvotes: 4

JB Nizet
JB Nizet

Reputation: 691953

No, not as a switch statement. But it can be much more concise by just using a collection and a loop:

Set<String> allStats = new HashSet<>(Arrays.asList("dex", "str", "con", "int"));
Set<String> increasedStats = new HashSet<>();
// TODO: let the user choose the stats to increase, 
// and store them in increadedStats

// then decrease the two other ones:
for (String stat : allStats) {
    if (!increasedStats.contains(stat)) {
        addStat(stat, -2);
    }
}

Upvotes: 4

W. Goeman
W. Goeman

Reputation: 1714

It is not possible to switch more than a single variable. This is documented as follows in the Java documentation here:

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

Sorry you will have to stick to 'else if' for this one.

Upvotes: 0

Related Questions