joe
joe

Reputation: 29

how to change to case statement

My Question is how to change this code to case statement? this is my if-statement

if (jarak < 80){
    merah = 255;
    hijau = 255;
    biru = 255;
    alpha = 255;  
}
else if (jarak == 0)
{
    merah = 0;
    hijau = 0;
    biru = 0;
    alpha = 255;
}

I've done some work about it and it contain error. This is my case statement

if(jarak < 80)
{
    switch(jarak[0]){
    case merah = 255;
        break;    
    }  
}

Is that true?

This is my full code. I can't change if statement to case statement because case statement just work with the byte, short, char, and int primitive data types only. And I'm using double to declare jarak.

 lebar = 256;
 tinggi = 256;
 double jarak = 0;      
 datapixel = new int [lebar * tinggi];

 int nilais=0;

 for (int a = 0; a < tinggi; a++) { 
    for (int j = 0; j < lebar; j++) { 
       int merah = 0; 
       int hijau = 0; 
       int biru = 0; 
       int alpha = 255; 
       jarak = Math.pow(((Math.pow((j - 128), 2)) + (Math.pow((a - 128), 2))), 0.5);

       if (jarak < 80){
           merah = 255;
           hijau = 255;
           biru = 255;
           alpha = 255;
         }
         else if (jarak == 0)
         {
            merah = 0;
            hijau = 0;
            biru = 0;
            alpha = 255;
         }
   }
}

Upvotes: 0

Views: 82

Answers (3)

antoniodvr
antoniodvr

Reputation: 1259

Just to keep the code a little bit cleaner you could create an utility class that will return usable value in order to make it works with the switch statement.

public class JarakUtils {

    public static final enum JarakOutcome { ZERO, LESSER_THAN_80} ;

    public static JarakOutcome getOutcome(int jarak) {
        JarakOutcome outcome;

        if(jarak < 80) {
            outcome = JarakOutcome.LESSER_THAN_80 ;
        } 

       // ... 

        return outcome;
    } 

} 

And then use the switch in this way:

switch(getOutcome(jarak)) {
    case ZERO:
        //... 
    break;
    case LESSER_THAN_80:
        //... 
    break;
} 

You will add a default value for the enum type.

Probably I would never use this approach due to the fact this force you to write a lot of code to reach the same result (usually the same come you would have wrote in the ifelse statement) .

Upvotes: 0

scadge
scadge

Reputation: 9723

In Java, switch statements demands concrete values in case, not ranges. In your case:

  • jarak == 0 - this one fits into switch
  • jarak < 80 - this one doesn't

So the best you could do is:

if (jarak < 80) {
    switch (jarak) {
        case 0:  merah = 255;
                 hijau = 255;
                 biru = 255;
                 alpha = 255; 
                 break;
        default: merah = 0;
                 hijau = 0;
                 biru = 0;
                 alpha = 255; 
                 break;  
     }
}

See the docs about switch case in order to understand better: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Upvotes: 0

Kruti Patel
Kruti Patel

Reputation: 1472

if(jarak < 80)
        {
          switch(jarak){
              case 0: 
                  //do whatever when `jarak == 0`
              break;
          }  
        }

This is how cases are written.

Upvotes: 2

Related Questions