简体   繁体   中英

How to cast a if-else switch statement

I think I need to cast this code. I need to return an int but I have to multiply by percents and dont want them to be cut off.
Should I cast it like this brains = (double)brains - (this.getBrains() * 0.01);
Also should I cast the return statement at the end? I'm weak at casting and i've been trying to find examples to help but they're not helping.

          if(attacker >= attackee)
            {
            switch (weapon)
                {
                    case 't':
                        brains = brains + (other.getBrains() * 0.01);
                        attack = other.getBrains() * -0.01;
                        other.addBrains(attack); 
                        break;

                    case 's':
                        brains = brains + (other.getBrains() * 0.05);
                        attack = other.getBrains() * -0.05;
                        other.addBrains(attack); 
                        break;  

                    case 'c':
                        brains = brains + (other.getBrains() * 0.10);
                        attack = other.getBrains() * -0.10;
                        other.addBrains(attack); 
                        break;  

                    case 'k':
                        brains = brains + (other.getBrains() * 0.20);
                        attack = other.getBrains() * -0.20;
                        other.addBrains(attack); 
                        break;
                }
         }
        else 
            {
                switch (weapon)
                {
                    case 't':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;

                    case 's':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;  

                    case 'c':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;

                    case 'k':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;      
                }
            }                   
    return attack;
}

Casting from double to int will always truncate, so might not be exactly what you're after. That is, (int)3.812 will give you 3, when you probably want 4.

You don't need to cast to double either, as "int + int * double" will return a double.

The easiest way forward is to use Math.round, which will round the bigger values up. Be aware that doubles round to longs, and floats round to ints though.

Floats will almost certainly give you the precision you're after, so the simplest solution for you is:

brains = Math.round(brains - this.getBrains() * 0.01f);

Edit : if you do want the higher precision afforded by doubles, then Math.round will give you a long, which you'll need to cast to an int:

brains = (int)Math.round(brains - this.getBrains() * 0.01d); 

Note that the constant has changed from 0.01f to 0.01d changing the precision of the entire calculation.

Suppose you want to subtract a 100th of the brains. You can do that in different ways:

  • Round up: brains = brains - (this.getBrains() / 100);
  • Round to the nearest brain: brains = brains - (int)((double)this.getBrains() * 0.01 + 0.5);

Of course, the first way can always be done with casting if you'd like, and I'm not sure whether casting twice is faster or slower than integer division.

Since attack is an integer and the return value is as well, you don't need to cast the return value.

Java automatically promotes lesser data types if there is a greater data type in the same expression. If you do this:

brains = brains + (other.getBrains() * 0.01);

You don't need to explicitly cast to a double at all because 0.01 is a double and that part of the expression is evaluated first (parenthesized/multiplication). If brains is an int you will need to cast the expression back to an int to make the assignment. Do it like this:

brains = (int)Math.rint(brains + (other.getBrains() * 0.01));

And of course make sure your integers are not small (< 100 for the operations you are doing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM