为了账号安全,请及时绑定邮箱和手机立即绑定

问:为什么我的运行结果中有很多位小数的情况?

类:

package com.imooc;

public class Telphone {
//属性
	float screen;
	float cpu;
	float mem;
	
//方法
	void call(){
		System.out.println("打电话功能!");
	}
	
	void sendMessage(){
		System.out.println("screen: "+screen+"	cpu: "+cpu+"	mem: "+mem+"	发短信功能!");
	}
}
main:

package com.imooc;

public class initialTelphone {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Telphone phone=new Telphone();
		
		
		for(int i=1; i<=100; i++){
			phone.screen=(float) 0.01*i;
			phone.cpu=(float) 0.02*i;
			phone.mem=(float)0.03*1;			
			phone.sendMessage();
		}
	}

}
运行结果:

screen: 0.01	cpu: 0.02	mem: 0.03	发短信功能!
screen: 0.02	cpu: 0.04	mem: 0.03	发短信功能!
screen: 0.03	cpu: 0.06	mem: 0.03	发短信功能!
screen: 0.04	cpu: 0.08	mem: 0.03	发短信功能!
screen: 0.049999997	cpu: 0.099999994	mem: 0.03	发短信功能!
screen: 0.06	cpu: 0.12	mem: 0.03	发短信功能!
screen: 0.07	cpu: 0.14	mem: 0.03	发短信功能!
screen: 0.08	cpu: 0.16	mem: 0.03	发短信功能!
screen: 0.089999996	cpu: 0.17999999	mem: 0.03	发短信功能!
screen: 0.099999994	cpu: 0.19999999	mem: 0.03	发短信功能!
screen: 0.11	cpu: 0.22	mem: 0.03	发短信功能!
screen: 0.12	cpu: 0.24	mem: 0.03	发短信功能!
screen: 0.13	cpu: 0.26	mem: 0.03	发短信功能!
screen: 0.14	cpu: 0.28	mem: 0.03	发短信功能!
screen: 0.14999999	cpu: 0.29999998	mem: 0.03	发短信功能!
screen: 0.16	cpu: 0.32	mem: 0.03	发短信功能!
screen: 0.17	cpu: 0.34	mem: 0.03	发短信功能!
screen: 0.17999999	cpu: 0.35999998	mem: 0.03	发短信功能!
screen: 0.19	cpu: 0.38	mem: 0.03	发短信功能!
screen: 0.19999999	cpu: 0.39999998	mem: 0.03	发短信功能!
screen: 0.21	cpu: 0.42	mem: 0.03	发短信功能!
screen: 0.22	cpu: 0.44	mem: 0.03	发短信功能!
screen: 0.22999999	cpu: 0.45999998	mem: 0.03	发短信功能!
screen: 0.24	cpu: 0.48	mem: 0.03	发短信功能!
screen: 0.25	cpu: 0.5	mem: 0.03	发短信功能!
screen: 0.26	cpu: 0.52	mem: 0.03	发短信功能!
screen: 0.26999998	cpu: 0.53999996	mem: 0.03	发短信功能!
screen: 0.28	cpu: 0.56	mem: 0.03	发短信功能!
screen: 0.29	cpu: 0.58	mem: 0.03	发短信功能!
screen: 0.29999998	cpu: 0.59999996	mem: 0.03	发短信功能!
screen: 0.31	cpu: 0.62	mem: 0.03	发短信功能!
screen: 0.32	cpu: 0.64	mem: 0.03	发短信功能!
screen: 0.32999998	cpu: 0.65999997	mem: 0.03	发短信功能!
screen: 0.34	cpu: 0.68	mem: 0.03	发短信功能!
screen: 0.35	cpu: 0.7	mem: 0.03	发短信功能!
screen: 0.35999998	cpu: 0.71999997	mem: 0.03	发短信功能!
screen: 0.37	cpu: 0.74	mem: 0.03	发短信功能!
screen: 0.38	cpu: 0.76	mem: 0.03	发短信功能!
screen: 0.39	cpu: 0.78	mem: 0.03	发短信功能!
screen: 0.39999998	cpu: 0.79999995	mem: 0.03	发短信功能!
screen: 0.41	cpu: 0.82	mem: 0.03	发短信功能!
screen: 0.42	cpu: 0.84	mem: 0.03	发短信功能!
screen: 0.42999998	cpu: 0.85999995	mem: 0.03	发短信功能!
screen: 0.44	cpu: 0.88	mem: 0.03	发短信功能!
screen: 0.45	cpu: 0.9	mem: 0.03	发短信功能!
screen: 0.45999998	cpu: 0.91999996	mem: 0.03	发短信功能!
screen: 0.47	cpu: 0.94	mem: 0.03	发短信功能!
screen: 0.48	cpu: 0.96	mem: 0.03	发短信功能!
screen: 0.48999998	cpu: 0.97999996	mem: 0.03	发短信功能!
screen: 0.5	cpu: 1.0	mem: 0.03	发短信功能!
screen: 0.51	cpu: 1.02	mem: 0.03	发短信功能!
screen: 0.52	cpu: 1.04	mem: 0.03	发短信功能!
screen: 0.53	cpu: 1.06	mem: 0.03	发短信功能!
screen: 0.53999996	cpu: 1.0799999	mem: 0.03	发短信功能!
screen: 0.55	cpu: 1.1	mem: 0.03	发短信功能!
screen: 0.56	cpu: 1.12	mem: 0.03	发短信功能!
screen: 0.57	cpu: 1.14	mem: 0.03	发短信功能!
screen: 0.58	cpu: 1.16	mem: 0.03	发短信功能!
screen: 0.59	cpu: 1.18	mem: 0.03	发短信功能!
screen: 0.59999996	cpu: 1.1999999	mem: 0.03	发短信功能!
screen: 0.61	cpu: 1.22	mem: 0.03	发短信功能!
screen: 0.62	cpu: 1.24	mem: 0.03	发短信功能!
screen: 0.63	cpu: 1.26	mem: 0.03	发短信功能!
screen: 0.64	cpu: 1.28	mem: 0.03	发短信功能!
screen: 0.65	cpu: 1.3	mem: 0.03	发短信功能!
screen: 0.65999997	cpu: 1.3199999	mem: 0.03	发短信功能!
screen: 0.66999996	cpu: 1.3399999	mem: 0.03	发短信功能!
screen: 0.68	cpu: 1.36	mem: 0.03	发短信功能!
screen: 0.69	cpu: 1.38	mem: 0.03	发短信功能!
screen: 0.7	cpu: 1.4	mem: 0.03	发短信功能!
screen: 0.71	cpu: 1.42	mem: 0.03	发短信功能!
screen: 0.71999997	cpu: 1.4399999	mem: 0.03	发短信功能!
screen: 0.72999996	cpu: 1.4599999	mem: 0.03	发短信功能!
screen: 0.74	cpu: 1.48	mem: 0.03	发短信功能!
screen: 0.75	cpu: 1.5	mem: 0.03	发短信功能!
screen: 0.76	cpu: 1.52	mem: 0.03	发短信功能!
screen: 0.77	cpu: 1.54	mem: 0.03	发短信功能!
screen: 0.78	cpu: 1.56	mem: 0.03	发短信功能!
screen: 0.78999996	cpu: 1.5799999	mem: 0.03	发短信功能!
screen: 0.79999995	cpu: 1.5999999	mem: 0.03	发短信功能!
screen: 0.81	cpu: 1.62	mem: 0.03	发短信功能!
screen: 0.82	cpu: 1.64	mem: 0.03	发短信功能!
screen: 0.83	cpu: 1.66	mem: 0.03	发短信功能!
screen: 0.84	cpu: 1.68	mem: 0.03	发短信功能!
screen: 0.84999996	cpu: 1.6999999	mem: 0.03	发短信功能!
screen: 0.85999995	cpu: 1.7199999	mem: 0.03	发短信功能!
screen: 0.87	cpu: 1.74	mem: 0.03	发短信功能!
screen: 0.88	cpu: 1.76	mem: 0.03	发短信功能!
screen: 0.89	cpu: 1.78	mem: 0.03	发短信功能!
screen: 0.9	cpu: 1.8	mem: 0.03	发短信功能!
screen: 0.90999997	cpu: 1.8199999	mem: 0.03	发短信功能!
screen: 0.91999996	cpu: 1.8399999	mem: 0.03	发短信功能!
screen: 0.93	cpu: 1.86	mem: 0.03	发短信功能!
screen: 0.94	cpu: 1.88	mem: 0.03	发短信功能!
screen: 0.95	cpu: 1.9	mem: 0.03	发短信功能!
screen: 0.96	cpu: 1.92	mem: 0.03	发短信功能!
screen: 0.96999997	cpu: 1.9399999	mem: 0.03	发短信功能!
screen: 0.97999996	cpu: 1.9599999	mem: 0.03	发短信功能!
screen: 0.98999995	cpu: 1.9799999	mem: 0.03	发短信功能!
screen: 1.0	cpu: 2.0	mem: 0.03	发短信功能!


正在回答

2 回答

你用的类型是float,float的小数有效位是7位。没任何问题啊。

0 回复 有任何疑惑可以回复我~
#1

阿May林不林 提问者

非常感谢!确实和数据类型有关系!
2015-08-15 回复 有任何疑惑可以回复我~

 phone.mem=(float)0.03*1; 这里是不是写错了?

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

问:为什么我的运行结果中有很多位小数的情况?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信