java - jvm错误的算术答案
问题描述
我有个问题:
A shopkeeper sells two calculators for the same price. He earns 20% profit on one and suffers a loss of 20% on the other. Write a program to find his total cost price of the calculators by taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
和一个解决方案:
import java.util.*;
public class Calculator {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
double sp,cp1,cp2;
System.out.println("Enter Sp");
sp=sc.nextDouble();
cp1=(sp / (1 + (20 / 100.0)));
cp2=sp/(1-(20/100.0));
System.out.println("CP of 1st calculator:"+cp1+"\n"+ "Cp of 2nd calculator="+cp2+"\n"+"TotalCP:"+(cp1+cp2)); sc.close();
}
}
和一个输出:
输入第一个计算器的 Sp 1000
CP:833.3333333333334
第二个计算器的 Cp=1250.0
TotalCP:2083.3333333333335
但是你可以看到 cp 的总和不正确,你能告诉我为什么会这样吗?
解决方案
推荐阅读
- r - 如何为动物园对象上的自定义聚合创建索引
- python - Pyvmomi 支持的类型
- django - 如何将情境字段添加到序列化程序?
- javascript - 使用一个 onChange 处理程序处理动态文本输入,并且在 React JS 中没有名称属性
- ansible - 调试http post请求ansible
- python-3.x - 如何从python文件访问数组值
- vbscript - 我无法使用 VBS 提取特定字符串
- python - 在 Python 中计算回报和波动率的更有效方法
- c++ - 有没有办法将数字作为字符串,并且不使用浮点数或双精度数,将其四舍五入到确切的位数?
- ignite - 引发 QueryCursor 中的序列化问题