Java 程序中可能存在的舍入问题

Java 程序中可能存在的舍入问题,java,Java,我花了一整晚的时间为我的班级写这段代码,这几乎完成了,但是我的一个输出值(com2)是0.10美元,我完全不知道为什么我将粘贴整个程序,如果你们有任何建议,请告诉我!:) 目标成果: Salesperson: GARY Tier: B Base Salary: $ 800.89 Commission 1: $ 1200.00 Commission 2: $ 152.28 Total Commission: $ 1352.28 Monthly Salary: $ 2153.17 我目前的成果:

我花了一整晚的时间为我的班级写这段代码,这几乎完成了,但是我的一个输出值(com2)是0.10美元,我完全不知道为什么

我将粘贴整个程序,如果你们有任何建议,请告诉我!:)

目标成果:

Salesperson: GARY
Tier: B
Base Salary: $ 800.89
Commission 1: $ 1200.00
Commission 2: $ 152.28
Total Commission: $ 1352.28
Monthly Salary: $ 2153.17
我目前的成果:

Salesperson: GARY
Tier: B
Base Salary: $    800.89 
Commission 1: $   1200.00 
Commission 2: $    152.18 
Total Commision: $   1352.18 
Monthly Salary: $   2153.07
如果你们愿意运行此程序,请将这些数据用于笔记本电脑:

Basic: 0
Premium: 12
Deluxe: 0
代码:

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab2.gsmith;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;


/**
*
* @author gskil
*/
public class Lab2Gsmith {
private static final Set<String> 
        validOptions = new HashSet<>    (Arrays.asList("Yes","YES","No","no","Y","y","N","n"));
private static final Set<String> 
        yValues = new HashSet<>(Arrays.asList("Yes","YES","Y","y"));

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

// ***************** DECLARE VARAIBLES ********************
String input;
String addAnother = null;
String tier = null;
String name = null;
String pString = null;




double bCost = 650.90;
double pCost = 950.50;
double dCost = 1350.95;
int bCom = 50;
int pCom = 100;
int dCom = 150;
int bSales = 0;
int pSales = 0;
int dSales = 0;
double totalSales;
double baseSal = 0;
double com1;
double totalCom;
int bonus = 0;
double com2 = 0;
double monthlySal;






// ***************** GET INPUT ****************************
do{

do{

input = JOptionPane.showInputDialog("What is your name?");  
if (input.matches("[a-zA-Z]+")) 

    name = input.toUpperCase();
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case\n");

}
while (!input.matches("[a-zA-Z]+"));


do{

input = JOptionPane.showInputDialog("What tier are you?");  
if (input.matches("[a-cA-C]+"))  

    tier = input.toUpperCase();
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid tier: ‘a’-‘c’, lower or upper case\n");
}

while (!input.matches("[a-cA-C]+"));            


do{

input = JOptionPane.showInputDialog("What is your base salary?");
if (input.matches("[0-9$.]+"))

    baseSal = Double.parseDouble(input);
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid salary amount containing: ‘0’-‘9’, and/or beginning ‘$’, and/or ‘.’\n");
}
while (!input.matches("[0-9$.]+"));


do{

input = JOptionPane.showInputDialog("How many basic laptops did you sell?");
if (input.matches("[0-9]+"))  

    bSales = Integer.parseInt(input);
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid number: Only positive integers\n");
}
while (!input.matches("[0-9]+")); 


do{
input = JOptionPane.showInputDialog("How many premium laptops did you sell?");
if (input.matches("[0-9]+"))  

    pSales = Integer.parseInt(input);
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid number: Only positive integers\n");
}
while (!input.matches("[0-9]+"));        


do{
input = JOptionPane.showInputDialog("How many deluxe laptops did you sell?");
if (input.matches("[0-9]+"))  

    dSales = Integer.parseInt(input);
else

JOptionPane.showMessageDialog
    (null,"Please enter a valid number: Only positive integers\n");
}
while (!input.matches("[0-9]+"));        



    // ******************** PROCESSING ************************
totalSales = ((bSales * bCost) + (pSales * pCost) + (dSales * dCost));

if (totalSales > 2500){
    com2 = 0.00;

if (totalSales >= 2500 && totalSales < 5500)
    com2 = (totalSales * 0.01);

if (totalSales >= 5500 && totalSales < 10500)
    com2 = (((totalSales - 5500) * 0.02) + 75);

if (totalSales >= 10500 && totalSales < 13500)
    com2 = (((totalSales - 10500) * 0.03) + 125);

if (totalSales > 13500)
    com2 = 375;
}


switch (tier){

    case "A":
        if (com2 > (.75 * baseSal))
            pString = ("Congratulations, " + name + (", you have been promoted to tier B."));
        else
            pString = ("Sorry, " + name + (", you have not been promoted this month."));
        break;

    case "B":
        if (com2 > (.75 * baseSal))
            pString = ("Congratulations, " + name + (", you have been promoted to tier C."));
        else
            pString = ("Sorry, " + name + (", you have not been promoted this month."));
        break;

    case "C":
        if (com2 > (.75 * baseSal))
            pString = ("Congratulations, " + name + (", you have earned a $1000 bonus!"));
        else
            pString = ("Sorry, " + name + (", you have not earned a bonus this month."));
        break;
    }

if ("C".equals(tier) && com2 > (.75 * baseSal))
    bonus = 1000;
else 
    bonus = 0;




// ***************** DO CALCULATIONS **********************

com1 = (bCom * bSales) + (pCom * pSales) + (dCom * dSales);

totalCom = (com1 + com2);

monthlySal = (baseSal + totalCom + bonus);





// ***************** SHOW OUTPUT **************************
System.out.println("Salesperson: " + name);
System.out.println("Tier: " + tier);
System.out.printf("Base Salary: $%10.2f \n", baseSal);
System.out.printf("Commission 1: $%10.2f \n", com1);
System.out.printf("Commission 2: $%10.2f \n", com2);
System.out.printf("Total Commision: $%10.2f \n", totalCom);
System.out.printf("Monthly Salary: $%10.2f \n", monthlySal);
System.out.println("\n\n" + pString + "\n");

while (true) {
addAnother = JOptionPane.showInputDialog("Would you like to enter another salespersons' data?");

if (validOptions.contains(addAnother)) {
    break;
}

JOptionPane.showMessageDialog(null,"Please enter a valid answer: Yes, YES, No, no, Y, y, N, n\n");
}


    }while (yValues.contains(addAnother));
}

}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装lab2.gsmith;
导入java.util.array;
导入java.util.HashSet;
导入java.util.Set;
导入javax.swing.JOptionPane;
/**
*
*@author gskil
*/
公共类标签{
私有静态最终集
validOptions=新的哈希集(Arrays.asList(“Yes”、“Yes”、“No”、“No”、“Y”、“Y”、“N”、“N”));
私有静态最终集
yValues=新的哈希集(Arrays.asList(“Yes”、“Yes”、“Y”、“Y”));
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//*******************申报可变金额********************
字符串输入;
字符串addother=null;
字符串层=空;
字符串名称=null;
字符串pString=null;
双B成本=650.90;
双pCost=950.50;
双dCost=1350.95;
int bCom=50;
int-pCom=100;
int dCom=150;
int-bSales=0;
int pSales=0;
int dSales=0;
双倍销售额;
双基=0;
双com1;
双totalCom;
整数奖金=0;
双com2=0;
双月;
//*******************获取输入****************************
做{
做{
input=JOptionPane.showInputDialog(“您叫什么名字?”);
if(输入匹配(“[a-zA-Z]+”)
name=input.toUpperCase();
其他的
JOptionPane.showMessageDialog
(null,“请输入一个有效名称,其中包含:'a-z'或'a-z'小写或大写\n”);
}
而(!input.matches(“[a-zA-Z]+”);
做{
input=JOptionPane.showInputDialog(“您是哪一层的?”);
if(输入匹配(“[a-cA-C]+”)
tier=input.toUpperCase();
其他的
JOptionPane.showMessageDialog
(null,“请输入有效层:'a'-'c',小写或大写\n”);
}
而(!input.matches(“[a-cA-C]+”);
做{
input=JOptionPane.showInputDialog(“你的基本工资是多少?”);
if(输入.匹配(“[0-9$.]+”)
baseSal=Double.parseDouble(输入);
其他的
JOptionPane.showMessageDialog
(null,“请输入一个包含以下内容的有效工资金额:'0'-'9',和/或开头'$'和/或'.'\n');
}
而(!input.matches(“[0-9$.]+”);
做{
input=JOptionPane.showInputDialog(“你卖了多少台基本笔记本电脑?”);
if(input.matches(“[0-9]+”))
bSales=Integer.parseInt(输入);
其他的
JOptionPane.showMessageDialog
(null,“请输入有效数字:仅限正整数\n”);
}
而(!input.matches(“[0-9]+”);
做{
input=JOptionPane.showInputDialog(“你卖了多少台高级笔记本电脑?”);
if(input.matches(“[0-9]+”))
pSales=Integer.parseInt(输入);
其他的
JOptionPane.showMessageDialog
(null,“请输入有效数字:仅限正整数\n”);
}
而(!input.matches(“[0-9]+”);
做{
input=JOptionPane.showInputDialog(“你卖了多少台豪华笔记本电脑?”);
if(input.matches(“[0-9]+”))
dSales=Integer.parseInt(输入);
其他的
JOptionPane.showMessageDialog
(null,“请输入有效数字:仅限正整数\n”);
}
而(!input.matches(“[0-9]+”);
//*************************处理************************
总销售额=((销售*成本)+(诗篇*成本)+(销售*成本));
如果(总销售额>2500){
com2=0.00;
如果(总销售额>=2500&&totalSales<5500)
com2=(总销售额*0.01);
如果(totalSales>=5500&&totalSales<10500)
com2=((总销售额-5500)*0.02)+75);
如果(totalSales>=10500&&totalSales<13500)
com2=((总销售额-10500)*0.03)+125);
如果(总销售额>13500)
com2=375;
}
交换机(层){
案例“A”:
如果(com2>(.75*baseSal))
pString=(“恭喜,”+name+(“,您已晋升为B级”);
其他的
pString=(“对不起,”+name+(“,您本月没有升职。”);
打破
案例“B”:
如果(com2>(.75*baseSal))
pString=(“恭喜,”+name+(“,您已晋升为C级”);
其他的
pString=(“对不起,”+name+(“,您本月没有升职。”);
打破
案例“C”:
如果(com2>(.75*baseSal))
pString=(“恭喜,”+name+(“,您已获得1000美元奖金!”);
其他的
pString=(“对不起,”+name+(“,您本月没有获得奖金。”);
打破
}
如果(“C”。等于(第二层)和&com2>(.75*baseSal))
奖金=1000;
其他的
奖金=0;
//*******************进行计算**********************
com1=(bCom*bSales)+(pCom*pSales)+(dCom*dSales);
totalCom=(com1+com2);
月收入=(基本收入+总收入+奖金);
//****************显示输出**************************
System.out.println(“销售人员:+姓名”);
System.out.println(“层:+Tier”);
System.out.printf(“基本工资:$%10.2f\n”,baseSal);
System.out.printf(“佣金1:$%10.2f\n”,com1);
System.out.printf(“佣金2:$%10.2f\n”,com2);
System.out.printf(“总佣金:$%10.2f\n”,totalCom);
System.out.printf(“月薪:$%10.2f\n”,每月);
System.out.println(“\n\n”+pString+”\n”);
while(true){
addother=JOptionPane.showInputDialog(“是否要输入其他销售人员的数据?”);
if(validOptions.contains(addother)){
打破
}
showMessageDialog(null,“请输入有效答案:是、是、否、否、Y、Y、N、N\N”);
}
}while(yValues.contains(addother));
}
}

佣金2:$152.18是正确的。 检查代码:

com2 = (((totalSales - 10500) * 0.03) + 125);
(((11406.0-10500)*0.03)+125)=152.18//目标佣金2:$152.28不正确。

佣金
if (totalSales >= 10500 && totalSales < 13500)
    com2 = (((totalSales - 10500) * 0.03) + 125);
(152.28 - 125)/0.03 + 10500 = 27.28/0.03 + 10500 
                            = 909,3333333333 + 10500
                            = 11.409,33333333333