/**
* Write a description of class GUI here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class GUI
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner r = new Scanner(System.in);
int x;
int y;
int z;
System.out.println("x");
x = r.nextInt();
System.out.println("y");
y = r.nextInt();
System.out.println("z");
z = r.nextInt();
double t = (x+y+z)/3;
System.out.println("result " + t);
}
}
您好,以上是我的代码。
我故意将它设置为int x,y,z来测试该程序。
例如,当我输入(运行程序时):$ x = 1、1、3 $时,它总是四舍五入!为什么是这样?
这是Java整数除法的一个示例,该整数除法必须始终返回另一个整数。它会截断任何十进制结果。即使将结果分配给,也会发生这种情况double
。
double
除法时使用文字,以强制进行浮点除法。
double t = (x+y+z)/3.0;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句