我花了整整一夜为我的课程编写这段代码,它已经完成了,但我的输出值(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));
}
}
答案 0 :(得分:0)
佣金2:152.18美元是正确的。 检查代码:
com2 = (((totalSales - 10500) * 0.03) + 125);
<(>(((11406.0-10500)* 0.03)+ 125)= 152.18 //目标佣金2:$ 152.28不正确。
答案 1 :(得分:0)
对于double pCost = 950.50;
的12次溢价销售,您得到的totalSum
为11406.这意味着您对com2
使用以下计算:
if (totalSales >= 10500 && totalSales < 13500)
com2 = (((totalSales - 10500) * 0.03) + 125);
评估结果为152.18。
现在totalSales
必须得到什么才能产生152.28?
(152.28 - 125)/0.03 + 10500 = 27.28/0.03 + 10500
= 909,3333333333 + 10500
= 11.409,33333333333
因此,一台笔记本电脑的价格为11.409,33333333333/12
,而且永远无法评估为正确的货币价值。因此,如果您正确设置了变量,则152.28可能无法获得正确的结果。
因此,我会得出结论:输入和输出的组合是错误的,或者某些参数设置不正确。我的猜测是它在GOAL部分是一个拼写错误,而com2应该是你152.18的结果。