似乎减法正在触发某种问题,结果值是错误的.
double tempCommission = targetPremium.doubleValue()*rate.doubleValue()/100d;
Run Code Online (Sandbox Code Playgroud)
78.75 = 787.5*10.0/100d
double netToCompany = targetPremium.doubleValue() - tempCommission;
Run Code Online (Sandbox Code Playgroud)
708.75 = 787.5 - 78.75
double dCommission = request.getPremium().doubleValue() - netToCompany;
Run Code Online (Sandbox Code Playgroud)
877.8499999999999 = 1586.6 - 708.75
由此产生的预期值为877.85.
应该怎样做才能确保正确计算?
import java.awt.Rectangle;
import java.util.Comparator;
public class RectangleComparator implements Comparator
{
public int compare(Object object1, Object object2)
{
Rectangle rec1 = (Rectangle) object1;
Rectangle rec2 = (Rectangle) object2;
return rec1.getWidth().compareTo(rec2.getWidth());
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我得到的错误是双倍无法解除引用.任何人都可以帮我找出原因吗?
public static void main(String[] args) {
double [] boxes;
boxes = new double[] {20, 10, 5, 40, 20, 41, 41, 2, 6, 7, 3, 4, 5, 6, 23, 34, 7, 8, 2, 2};
double heaviest = 0;
double normal = 0;
double heavy = 0;
double totalCost;
double a = 0;
double b = 0;
int repeatCount=0;
for (int i = 1; i < boxes.length; i++) {
if (boxes[i] > heaviest)
heaviest = boxes[i];
}
for(double element: boxes) {
if(element==heaviest) …Run Code Online (Sandbox Code Playgroud) 我有两个双重值.我想检查两个double值是否相同.我有两种比较这个双值的方法.
第一种方式:
double a = 0.1;
double b = 0.2;
if(a == b) {
System.out.println("Double values are same");
}
Run Code Online (Sandbox Code Playgroud)
另一种比较方式:
if(Double.compare(a, b) == 0) {
System.out.println("Double values are same");
}
Run Code Online (Sandbox Code Playgroud)
哪一种最好的方法又准确?比较双值是否两种方式相同?
所以我几乎完全理解方法,对象和OOP.然而,我正在为初学者准备一本德语Java编程书,我必须创建一个提款限额高达-1000欧元的银行账户.不知何故,即使金额为-1000欧元,代码也会执行else代码,这对我来说毫无意义.它不应该在我看来输出错误,但确实如此.
这是帐户代码:
public class Account {
private String accountnumber;
protected double accountbalance;
Account(String an, double ab) {
accountnumber = an;
accountbalance = ab;
}
double getAccountbalance() {
return accountbalance;
}
String getAccountnumber() {
return accountnumber;
}
void deposit(double ammount) {
accountbalance += ammount;
}
void withdraw(double ammount) {
accountbalance -= ammount;
}
}
Run Code Online (Sandbox Code Playgroud)
扩展帐户:
public class GiroAccount extends Account{
double limit;
GiroAccount(String an, double as, double l) {
super(an, as);
limit = l;
}
double getLimit() {
return limit;
} …Run Code Online (Sandbox Code Playgroud)