Java温度转换器错误

标签 java calculator

我编写了这个程序,但不知道出了什么问题,基本上它是 C->F、F->C、C->K、K->C、F->K、K 的转换器->F。顺便说一句,这是我在这个网站上的第一个问题,我使用 Bluej。当我尝试转换它时,我得到一个错误的答案,但我的公式都是正确的(在谷歌上检查)...

/**
 * Program to 
 * 
 * Anirudh Gupta
 * th August 2014
 */
import java.io.*;
public class Temperature
{
   public static void main () throws IOException
   {
       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(isr);
       System.out.println("Enter choice of scale in which current temperature in:");
       System.out.println(" 1)Celsius");
       System.out.println(" 2)Fahrenheit");
       System.out.println(" 3)Kelvin");
       int no=Integer.parseInt(br.readLine());
       double ans;
       switch(no)
       {
           case 1:
                System.out.println("Enter temperature to convert(in °C)");
                double C=Double.parseDouble(br.readLine());
                System.out.println("Enter choice of scale to which current temperature should be converted");
                System.out.println("1)Fahrenheit");
                System.out.println("2)Kelvin");
                int no1=Integer.parseInt(br.readLine());
                switch(no1)
                {
                    case 1:
                           ans=((9*C)/5)+32;
                           System.out.println(ans+"°F");
                           break;
                    case 2:
                           ans=C+273;
                           System.out.println(ans+"K");
                           break;
                }
                break;
           case 2:
                System.out.println("Enter temperature to convert(in °F)");
                double F=Double.parseDouble(br.readLine());
                System.out.println("Enter choice of scale to which current temperature should be converted");
                System.out.println("1)Celsius");
                System.out.println("2)Kelvin");
                int no2=Integer.parseInt(br.readLine());
                switch(no2)
                {
                    case 1:
                           ans=((5*(F-32))/9);
                           System.out.println(ans+"°C");
                           break;
                    case 2:
                           ans=((5/9)*(F-32))+273;
                           System.out.println(ans+"K");
                           break;
                }
                break;
           case 3:
                System.out.println("Enter temperature to convert(in K)");
                double K=Double.parseDouble(br.readLine());
                System.out.println("Enter choice of scale to which current temperature should be converted");
                System.out.println("1)Celsius");
                System.out.println("2)Fahrenheit");
                int no3=Integer.parseInt(br.readLine());
                switch(no3)
                {
                    case 1:
                           ans=K-273;
                           System.out.println(ans+"°C");
                           break;
                    case 2:
                           ans=((9/5)*(K-273))+32;
                           System.out.println(ans+"°F");
                           break;
                }
                break;
       }

   }
}

最佳答案

当您在整数算术中中执行该计算时,

(5/9) 的计算结果将为零。

同样,(9/5) 的计算结果为 1。

重写为5.0/9等,以便计算以浮点形式进行。

关于Java温度转换器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274138/

相关文章:

c++ - 在 C++ 的计算器程序中重载 '+' 运算符时无法获得正确的输出

objective-c - 为什么这个 sin 方法会返回错误的答案?

Java 资源关闭

java - Android 中的 TextView 在右侧留下间隙。这个要怎么调整

java - 阅读和理解 AspectJ 切入点?

javascript - 使用 Javascript 的预订计算器

c++ - 具有未指定操作数的C++计算器

带有用户定义变量的 Java 控制台计算器

java - 如何操作和打印二维 ArrayList?

java - AsynchronousSocketChannel#read 永远不会完成。