首页 > 解决方案 > 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 的总和不正确,你能告诉我为什么会这样吗?

标签: javajvm

解决方案


推荐阅读