What is a better way to write the code,

public void showFeelings(int howManyGoals)
{        
    switch (howManyGoals) 
    {
        case 0:  System.out.println("Oh dear, not very good"); break;
        case 1:  System.out.println("Oh dear, not very good"); break;
        case 2:  System.out.println("Oh dear, not very good"); break;
        case 3:  System.out.println("Ive seen donkeys shoot better"); break;
        case 4:  System.out.println("Ive seen donkeys shoot better"); break;
        case 5:  System.out.println("Ive seen donkeys shoot better"); break;

            //and so on
     }
}

Is there a possible way of writing cases 0-2 as one case or am i better writing it as follows

public void showFeelings(int howManyGoals)
{
  if(howManyGoals >= 0 && howManyGoals <= 2)
  {
    System.out.println("Oh dear, not very good");
  }
  else if(howManyGoals >= 0 && howManyGoals <= 2)
  {
    System.out.println("Ive seen donkeys shoot better");
  }
  // and so on
}

I think i've answered my own question as ive thought that at some stage i will need to end it so i will need something like howManyGoals being more than says your an "international hero" but i dont think i am able to do this with switch statements?


Thanks

For your first question, instead of repeating code, you could remove the code after the first 2 cases and only have it executed on the 3rd etc..
i.e.

public void showFeelings(int howManyGoals)
{        
    switch (howManyGoals) 
    {
        case 0:
        case 1:
        case 2:  System.out.println("Oh dear, not very good"); break;
        case 3: 
        case 5:  System.out.println("Ive seen donkeys shoot better"); break;


        default: System.out.println("International hero"); break;

     }
}

When howManyGoals == 0 for example, since case 0 has no code, it will run through the switch until it finds code, then executes that. The same applies for the other cases. They will find the next available code and run it.
There is not really any technical difference between switch and if..else blocks. Both can effectively do the same thing.

I think i've answered my own question as ive thought that at some stage i will need to end it so i will need something like howManyGoals being more than says your an "international hero" but i dont think i am able to do this with switch statements?


Thanks

If I understand you correctly, yes you can do that. You can use the switch's default statement, which is executed when any value that your cases do not deal with occurs.

e.g.

public void showFeelings(int howManyGoals)
{        
    switch (howManyGoals) 
    {
        case 0:
        case 1:
        case 2:  System.out.println("Oh dear, not very good"); break;
        case 3: 
        case 5:  System.out.println("Ive seen donkeys shoot better"); break;


        default: System.out.println("International hero"); break;

     }
}

Technically, the final break is not required because flow would fall out of the switch statement anyway. However, we recommend using a break so that modifying the code is easier and less error-prone. The default section handles all values that aren't explicitly handled by one of the case sections. http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Edited 5 Years Ago by Akill10: n/a

In reply to your question bangor boy,

public void showFeelings(int howManyGoals)
{        
    switch (howManyGoals) 
    {
        case 0: case 1: case 2: System.out.println("Oh dear, not very good"); break;
        case 3: case 4: case 5: System.out.println("Ive seen donkeys shoot better"); break;

            //and so on
     }
}

Also why is your name Bangor boy, are you at Bangor, Wales or perhaps Bangors, Ireland?

Chris

Is it better to use switch statements as they dont get as confusing if you are writing massive if else statements?

Thanks again AKill i seen the switch statements written like yours elsewhere but didnt understand how it worked.

Is there any meaningful difference between the way you or freaky chris wrote them?

Is it better to use switch statements as they dont get as confusing if you are writing massive if else statements?

Switch statements can be a bit easier to read. It's how I would do it.

But I just realised something, I told you there is not any technical difference between switch and if..else. ****I don't know why but I completely forgot about Strings. You can only switch on integer constants (static final int) or literals. Therefore, if you need to evalutate Strings, use an if...else.***** Well, unless you want to use Enums: http://www.xefer.com/2006/12/switchonstring

Thanks again AKill i seen the switch statements written like yours elsewhere but didnt understand how it worked.

Is there any meaningful difference between the way you or freaky chris wrote them?

No, all freaky_chris did was put them on the same line, it is the exact same code. AFAIK, they will not execute quicker if you put them inline. Spacing them out makes it more readable, for me anyway.

Edited 5 Years Ago by Akill10: n/a

Ok cheers i was thinking chris option would have been faster and thanks for the reminder regards evalutating strings :)

Ok cheers i was thinking chris option would have been faster and thanks for the reminder regards evalutating strings :)

No problem, and if you didn't see my last edit, there is a way of evaluating Strings using Enums. But it's easier using an if..else. Heres the link again if you want to give it a gander http://www.xefer.com/2006/12/switchonstring

Edited 5 Years Ago by Akill10: n/a


You can only switch on integer constants (static final int) or literals.

I dont get what you are trying to say. I think you can switch with any value as long as it is: char, byte, short or int. It doesnt have to be Constant..

Ex:

import java.util.Scanner;

public class Test
{



 public static void main(String[] args)
 {
	Scanner sc= new Scanner(System.in);
	System.out.print(" Enter int Value from 1 to 3 \n -------->: ");
	int switchValue = sc.nextInt();

		switch(switchValue)
		{
			case 1:
			case 2:
			case 3:
				System.out.println("Value passed into switch not need to be CONSTANT!");
				System.out.println();
				break;
		}

 }
 }

AKill, avoid editing your post after someone else has posted to make there post seem pointless. After all, your post was rather different until I had posted mine.


Regards.
Chris.

AKill, avoid editing your post after someone else has posted to make there post seem pointless. After all, your post was rather different until I had posted mine.


Regards.
Chris.

Nobody had posted when I posted mine, similarily, nobody had posted until after I edited my code. The only thing different in my post before the edit was I didn't read his question about the multiple cases, so I added it in. So please, avoid insinuating that people you do not know are somehow out to make you look bad.

Edited 5 Years Ago by Akill10: n/a

Well then, the only explanation is you were writing your reply as I was writing my edit. Hence, please avoid insinuating that people you do not know are somehow out to make you look bad.

I dont get what you are trying to say. I think you can switch with any value as long as it is: char, byte, short or int. It doesnt have to be Constant..

Ex:

import java.util.Scanner;

public class Test
{



 public static void main(String[] args)
 {
	Scanner sc= new Scanner(System.in);
	System.out.print(" Enter int Value from 1 to 3 \n -------->: ");
	int switchValue = sc.nextInt();

		switch(switchValue)
		{
			case 1:
			case 2:
			case 3:
				System.out.println("Value passed into switch not need to be CONSTANT!");
				System.out.println();
				break;
		}

 }
 }

I said Integer constants or literals

Besides, the new JDK 7 will allow the use of String in switches so nearly everything I said about switch will be out of date :D

Edited 5 Years Ago by Akill10: n/a

I said Integer constants or literals

Besides, the new JDK 7 will allow the use of String in switches so nearly everything I said about switch will be out of date :D

Thats nice. The code are getting smaller and smaller and more easier to write. 100 years from now, one code sign can creat billions and billions of command.(FLASH FORWARD) Flying cars and massive energy harvested from the sun, are then used to provide power into our brain implanted communication devices. We'll never have to talk using our mouth again. We'll talk to someone just by thinking about a number to reach that person and directly talk to them if that person verifies your identity inside his ugraded brain.

Just a thought. It'll be nice to have that kind of technology. Mobile phone providers wont be making Mobile phones anymore,.. They'll be making Brain Phones from then on. Thats possible , I think.

This question has already been answered. Start a new discussion instead.