hibernati0n 3 Posted January 29, 2017 I'm very new to Java and so far I have been able to make the scripts for the things I want without a problem but I've noticed people saying that enums and switch statements make your scripts overall better if you implement them when appropriate. I've learnt a little bit about switch statements and enums and I thought I had a decent idea but I've never implemented them into any of my scripts because I don't know when I should or even what would be an efficient way to do so. I was hoping maybe someone could explain exactly why they are good in their opinion and maybe give an example as to how or why it would be good to implement one in a certain situation? My idea is they are more useful than if/elseif/else block statements because they allow greater flexibility and I think I'm starting to see that now as I try make more advanced scripts that if one chain of the block fails the whole script fails instead of repeating that chain so hopefully someone can explain it in a way that maybe I'll better understand it compared to how it has been explained to me before and I haven't really understood. Thanks
Pandemic 2853 Posted January 29, 2017 It doesn't really make a difference, it's cleaner to look at sure, but functionally it's no different than a bunch of if's.
hibernati0n 3 Author Posted January 29, 2017 It doesn't really make a difference, it's cleaner to look at sure, but functionally it's no different than a bunch of if's. Oh okay, I thought it had an impact thanks
Im A Baller 348 Posted January 29, 2017 its really just a good organizational tool. it's well worth learning
Xephy 237 Posted January 30, 2017 its really just a good organizational tool. it's well worth learning Well said.
Scrivus 172 Posted February 6, 2017 This is the basis of every one of my scripts. Makes the script look way cleaner and more readable for debugging later.
Cardozz 46 Posted February 6, 2017 Erm, to be honest there is a huge difference between Enums and Switch/Case statements if you'd ask me. Enums are constants and do not change when the script is in use. Switch/Case statements is just some sort of looping tool which is very useful when you want the script to pick a certain action with a certain input. You can't compare these two in my opinion. Overall, the above answers will provide you the information you were seeking for. Example: Use enums for constants, such as runes and level needed for a spell: public enum Spells{ AIR_STRIKE("Mind rune", "Air rune", 1); private String[] runeNames; private int levelRequired; private Spells(String... runeNames, levelRequired){ this.runeNames = runeNames; this.levelRequired = levelRequired; } //To grab the rune names for example, use a getter: public String[] getRunesForSpell(){ return runeNames; } } For a switch/case statement, you'd use it in a scenario like this: //For example you want to print something for when a car switches it's gear, but don't want to use a big nested if-statement //In this example a nested if-statement wouldn't be too bad, but imagine this when a car has 100 gears or something. int i = getCarGear(); //Imagine getCarGear() is a method that grabs the car's gear. public void executeSwitchCase(){ //You'll need to call this as a method if it's outside a loop. switch(int i){ case 1: System.Out.Println("The maximum speed in gear 1 is 20 km/h."); break; case 2: System.Out.Println("The maximum speed in gear 2 is 40 km/h."); break; case 3: System.Out.Println("The maximum speed in gear 3 is 60 km/h."); break; case 4: System.Out.Println("The maximum speed in gear 4 is 80 km/h."); break; case 5: System.Out.Println("The maximum speed in gear 5 is 100 km/h."); break; case 6: System.Out.Println("The maximum speed in gear 6 is 120 km/h."); break; default: break; } } I hope this clears things up!
rokaHakor 171 Posted February 6, 2017 For a switch/case statement, you'd use it in a scenario like this: //For example you want to print something for when a car switches it's gear, but don't want to use a big nested if-statement //In this example a nested if-statement wouldn't be too bad, but imagine this when a car has 100 gears or something. int i = getCarGear(); //Imagine getCarGear() is a method that grabs the car's gear. public void executeSwitchCase(){ //You'll need to call this as a method if it's outside a loop. switch(int i){ case 1: System.Out.Println("The maximum speed in gear 1 is 20 km/h."); break; case 2: System.Out.Println("The maximum speed in gear 2 is 40 km/h."); break; case 3: System.Out.Println("The maximum speed in gear 3 is 60 km/h."); break; case 4: System.Out.Println("The maximum speed in gear 4 is 80 km/h."); break; case 5: System.Out.Println("The maximum speed in gear 5 is 100 km/h."); break; case 6: System.Out.Println("The maximum speed in gear 6 is 120 km/h."); break; default: break; } } int i = getCarGear(); //Imagine getCarGear() is a method that grabs the car's gear. public void executeSwitchCase(){ System.Out.Println("The maximum speed in gear " + i + " is " + i*20 + " km/h."); } I optimized it for you. No need to thank me fam.
Cardozz 46 Posted February 7, 2017 int i = getCarGear(); //Imagine getCarGear() is a method that grabs the car's gear. public void executeSwitchCase(){ System.Out.Println("The maximum speed in gear " + i + " is " + i*20 + " km/h."); } I optimized it for you. No need to thank me fam. You're missing the point. I was explaining the usefulness of the switch case statement. I mean.. The speed indicator could be anything and not have a linear increasement, for example gear 1 max = 45, gear 2 max = 77, gear 3 max = 123 and whatnot. Don't you think i am using switch cases to print this stuff Also note the "//In this example a nested if-statement wouldn't be too bad, but imagine this when a car has 100 gears or something."
Recommended Posts
Archived
This topic is now archived and is closed to further replies.