“可能的精度损失”在我的Java程序中 [英] "Possible loss of precision" in my Java program
本文介绍了“可能的精度损失”在我的Java程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java的新手。我写了以下代码:
I'm new to Java. I wrote the following code:
import java.io.*;
import java.lang.*;
public class distravel
{
public static void main(String args[])
{
String a1,a2,a3;
int x=2;
float d,u,a,t;
//d=distance travelled,u=initial velocity,a=acceleration,t=timeinterval
try
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader buff=new BufferedReader(read);
System.out.print("Enter the INTIAL VELOCITY:");
a1=buff.readLine();
u=Float.parseFloat(a1);
System.out.print("Enter the ACCELERATION:");
a2=buff.readLine();
a=Float.parseFloat(a2);
System.out.print("Enter the TIME:");
a3=buff.readLine();
t=Float.parseFloat(a3);
d=((u*t)+a*Math.pow(t,x))/2F;
System.out.print("The total DISTANCE TRAVELLED:"+d);
}
catch(Exception e)
{}
}
}
我收到此错误:
distravel.java28:possible loss of precision
found :double
required :float
d=((u*t)+a*Math.pow(t,x))/2F;
^
如何解决此问题?
推荐答案
d=((u*t)+a*Math.pow(t,x))/2F;
应该是
d=(float)((u*t)+a*Math.pow(t,x))/2F;
或声明 d
为双
为 GrahamS 建议。
这篇关于“可能的精度损失”在我的Java程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文