我一直在努力寻找解决方案,但还没有骰子。我一直试图找到一种方法来共同处理switch语句中的案例值。我将提供作业问题和我的进展。其中包括class和classTest。感谢您的任何建议。
家庭作业?
一家大公司以佣金为基础向销售人员付款。销售人员每周收到200美元,加上该周总销售额的9%。例如,一周销售价值5000美元的商品的销售人员将获得200美元加上5000美元的9%,或总计650美元。您已获得每个销售人员销售的物品清单。这些项目的价值如下: 物品价值 1 239.99 2 129.75 3 99.95 4 350.89
开发一个Java应用程序,输入上周销售的一个销售人员的项目,并计算并显示该销售人员的收入。可以出售的商品数量没有限制。
的 的 ** * ** * *** 我的问题: ** * ** * ** * ** < EM> * ** * ** * ** * **
当我尝试进入多个项目时,问题就出现了。当我只做一个单项时,程序工作。然而,当我做多个时,该计划从未将价值观和销售人员的收入完全关闭
THE CLASS * ** * ** * ** < / p>
public class SalesCommissionCalculator
{
private String item;
private double value;
public SalesCommissionCalculator()
{
setItem("");
setValue(0.0);
}
public SalesCommissionCalculator(String i, double v)
{
setItem(i);
setValue(v);
}
public void setItem(String i)
{
item = i;
}
public void setValue(double v)
{
value = v;
}
public String getItem()
{
return item;
}
public double getValue()
{
return value;
}
public String toString()
{
return ("Item"+item+"Value"+value);
}
}
的 的 ** * ** * ** * 的** * ClassTest:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class SalesCommissionCalculatorTest
{
public static void main(String args [])
{
int quantity;
int item;
double salary = 200.00;
double commission = 0.09;
double total= 0;
String msg="";
SalesCommissionCalculator item1 = new SalesCommissionCalculator("1", 239.99);
SalesCommissionCalculator item2 = new SalesCommissionCalculator("2", 129.75);
SalesCommissionCalculator item3 = new SalesCommissionCalculator("3", 99.95);
SalesCommissionCalculator item4 = new SalesCommissionCalculator("4", 350.89);
item=Integer.parseInt(JOptionPane.showInputDialog("Enter item number 1, 2, 3 or 4. Enter -1 to quit."));
while(item != -1)
{
switch(item)
{
case 1: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item1.getValue());
break;
case 2: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total= (quantity*item2.getValue());
break;
case 3: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item3.getValue());
break;
case 4: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item4.getValue());
break;
default:
System.out.println("Invalid Item Number");
}
item=Integer.parseInt(JOptionPane.showInputDialog("Enter item number 1, 2, 3 or 4. Enter -1 to quit."));
}
msg = msg + String.format("Your Commission is $%.2f", (total*commission)+salary);
JOptionPane.showMessageDialog(null, msg);
}
}
答案 0 :(得分:0)
在任何情况下,您都没有累计总数。你现在拥有的(以案例1为例)
total = (quantity*item1.getValue());
只需将总数设为quantity*item1.getValue()
即可。你可能想要的是
total += (quantity*item1.getValue());
代替。 (以及其他三种情况类似)
答案 1 :(得分:0)
import java.util。*; 公共课Sale {
public static void main(String[] args) {
double value;
int item;
double earn= 0.0;
double total=0.0;
Scanner console = new Scanner(System.in);
System.out.print("Enter 1, 2, 3, 4 or \"-1 to quit\": ");
item = console.nextInt();
while(item != -1)
{
switch(item)
{
case 1:
System.out.print("Enter quantity: ");
int quantity= console.nextInt();
total = quantity * 239.99;
break;
case 2:
System.out.print("Enter quantity:");
quantity= console.nextInt();
total = quantity * 129.75;
break;
case 3:
System.out.print("Enter quantity: ");
quantity= console.nextInt();
total= quantity * 99.95;
break;
case 4:
System.out.print("Enter quantity: ");
quantity= console.nextInt();
total= quantity * 350.89;
break;
default: System.out.print("Invalid Item!");
}
if (total>=5000)
{
earn= total * 0.09;
System.out.print("You have earned 9%, Your salary of the week : $"+earn);
}
else
System.out.println("Your salary of the week: $"+total);
System.out.println("");
System.out.print("Enter 1, 2, 3, 4 or \"-1 to quit\": ");
item = console.nextInt();
}
System.out.println("Thank you to visit our shop!");
}
}