在java中将两个整数分成两个

编程入门 行业动态 更新时间:2024-10-28 04:26:37
本文介绍了在java中将两个整数分成两个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以看到这是新程序员的常见问题,但是我没有成功地实现我的代码的任何解决方案。基本上我想划分w和v,它必须保存到一个double变量中。但是它打印[0.0,0.0,...,0.0]

I can see this is a common problem for new programmers, however I didn't succeed in implementing any of the solution to my code. Basically I want to divide w and v, which must be saved to a double variable. But it prints [0.0, 0.0, ... , 0.0]

public static double density(int[] w, int[] v){ double d = 0; for(L = 0; L < w.length; L++){ d = w[L] /v[L]; } return d; }

推荐答案

这行这里 d = w [L] / v [L]; 发生在几个步骤

This line here d = w[L] /v[L]; takes place over several steps

d = (int)w[L] / (int)v[L] d=(int)(w[L]/v[L]) //the integer result is calculated d=(double)(int)(w[L]/v[L]) //the integer result is cast to double

$ b $换句话说,精确度已经在你转换成双倍之前就已经消失了,所以你需要先转一次,所以

In other words the precision is already gone before you cast to double, you need to cast to double first, so

d = ((double)w[L]) / (int)v[L];

这将强制java使用双重数学,而不是使用整数数学,然后转换为双精度在结尾

This forces java to use double maths the whole way through rather than use integer maths and then cast to double at the end

更多推荐

在java中将两个整数分成两个

本文发布于:2023-10-08 09:55:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   整数   中将   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!