java - Why doesn't my temperature conversion calculate correctly? -


this question has answer here:

i trying take temperature in fahrenheit , convert celsius. when call tocelsius() method calculates temperature 0 degrees celsius.

here conversion method

    public temperature tocelsius()     {     temperature answer = new temperature();     switch(temptype)     {     case 'c':         answer =  new temperature(this.temp,'c');         return answer;     case 'f':         answer =  new temperature(((this.temp-32)*(5/9)),'c');         return answer;      case 'k':         answer =  new temperature((this.temp-241.15),'c');         return answer;      default:         system.out.println("error!");         break;      }     return answer;     } 

here demo

    public class tempdemo {          public static void main(string[] args) {              temperature temp1 = new temperature(122, 'f');             system.out.println(temp1.tocelsius());       }      } 

5 , 9 int literals, dividing them done using integer division - i.e., take whole part of division. since 5 smaller 9, result in 0, , entire multiplication.

instead, should use double literals:

answer =  new temperature(((this.temp-32)*(5.0/9.0)), 'c'); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -