gpt4 book ai didi

Java温度转换器错误

转载 作者:行者123 更新时间:2023-12-01 17:01:43 24 4
gpt4 key购买 nike

我编写了这个程序,但不知道出了什么问题,基本上它是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com