1

我一直在努力寻找解决方案,但还没有骰子。我被困在试图找到一种方法来将 switch 语句中的案例值加在一起。我将提供作业问题和我的进度。其中将包括 class 和 classTest。感谢您的任何建议。

作业?

一家大公司向其销售人员支付佣金。销售人员每周收到 200 美元,外加该周总销售额的 9%。例如,一名销售人员在一周内销售了价值 5000 美元的商品,将获得 200 美元加上 5000 美元的 9%,即总共 650 美元。您已获得每个销售人员所售商品的清单。这些项目的值如下: 项目值 1 239.99 2 129.75 3 99.95 4 350.89

开发一个 Java 应用程序,输入一个销售人员上周售出的物品,然后计算并显示该销售人员的收入。可以出售的物品数量没有限制。

** * ** * ***我的问题:** * ** * ** * ** * ** * ** * ** * **

当我尝试输入多个项目时,问题就出现了。当我只做一个项目时,这个程序就可以工作。但是,当我做多个时,该程序永远不会将价值加在一起,销售人员的收入也会减少

班级* ** * ** * **

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);
    }

}

** * ** * ** * ** *类测试:

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);

        }
    }
4

2 回答 2

0

在任何情况下,您都没有累积总数。您现在拥有的(以案例 1 为例)

total = (quantity*item1.getValue());

只是将总数设置为等于quantity*item1.getValue()。你可能想要的是

total += (quantity*item1.getValue());

反而。(对于其他三种情况也是如此)

于 2013-10-03T23:14:34.407 回答
0

导入 java.util.*;公开课销售{

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!");

}

}

于 2017-04-08T21:17:13.433 回答