私の授業でこのコードを書くのは一晩中過ごしましたが、終わりは近いですが、私の出力値(com2)は$ 10です。
私はプログラム全体を貼り付けるつもりです、もし皆さんに提案があれば教えてください! :)プログラムでの丸め問題の可能性
GOALの成果:
Salesperson: GARY
Tier: B
Base Salary: $ 800.89
Commission 1: $ 1200.00
Commission 2: $ 152.28
Total Commission: $ 1352.28
Monthly Salary: $ 2153.17
MY CURRENT結果:
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));
}
}
float/doubleは、分数を正確に2進で表すことができないため、通貨を表すのには適していません - BigDecimalを使用してください。 – BarrySW19
デバッガの使い方を学び、繰り返しを歩く時間です。 – user3207158
@ BarrySW19お返事ありがとうございます!私はそれを前に読んだことがありますが、BigDecimalの使い方はまだ分かりません。 –