0

Output of small code

I don't understand why the output of this code is M. Somebody can help me. thank you so much!!!!int size = 31; char result = 'L'; size = (int) (0.5*size++); if (size <= 12) { result = 'X'; } else if (size >= 12 && size < 15) { result = 'S'; } else if (result >= 14){ result = 'M'; } else if (result <= 20){ result = 'L'; } else { result = '-'; }

9th Jun 2019, 9:47 AM
Ha Nguyen
Ha Nguyen - avatar
7 Antworten
+ 1
you can work with char like with int number, because chars has number value in table of chars char result = 'L'; int i=0; i = i + result; System.out.println(i); // 76 System.out.println(result == 76); //true System.out.println(result =='L'); //true i=1; i=i++; //++ is lost System.out.println(i); // 1 ++ here is lost because first it do new_i = old_i then old_i++ but old_i is lost then and continue only with new_i
10th Jun 2019, 4:05 AM
zemiak
+ 3
Because size is 16!
9th Jun 2019, 9:49 AM
CodeFu
CodeFu - avatar
+ 1
int size = 31; char result = 'L'; // (int) 'L' = 76 size = (int) (0.5*size++); //15 0.5*31 = 15.5 (int) 15.5 = 15 ++ is lost 15 <= 12 //false 15 >= 12 && 15 < 15 //true && false //false next compare result as integer not size :) 76 >= 14 // true, result= 'M' stop
9th Jun 2019, 9:33 PM
zemiak
+ 1
zemiak it's very useful for me thank you so much
10th Jun 2019, 4:11 AM
Ha Nguyen
Ha Nguyen - avatar
0
But it compare result >= 14 and result is 'L'
9th Jun 2019, 10:10 AM
Ha Nguyen
Ha Nguyen - avatar
9th Jun 2019, 4:08 PM
Ha Nguyen
Ha Nguyen - avatar
0
zemiak how it can compare result as integer ? And why ++ is lost ?
9th Jun 2019, 11:48 PM
Ha Nguyen
Ha Nguyen - avatar