-4

我在尝试检索已经在块外部建立并在内部调用的双变量时遇到了麻烦,但我想返回相同变量的值,以便可以将其应用于计算。

我想要返回的变量是:

double quarter = 0;

但是当我在我的第一个 else/if 语句中将它插入四分之一时,它会插入0而不是我的 switch 块中的值。我可以做些什么来检索值?

        double quarter = 0;

        //Date entry will be calculated by how much KW user enters
        switch (input)
        {
            case "2/15/13":
                quarter = kwUsed * 0.10;
                break;
            case "4/15/13":
                quarter = kwUsed * 0.12;
                break;
            case "8/15/13":
                quarter = kwUsed * 0.15;
                break;
            case "11/15/13":
                quarter = kwUsed * 0.15;
                break;
            default:
                System.out.println("Invalid date");
        }


        //Declaring variables for calculations
        double base = 0;
        double over = 0;
        double excess = 0;

        double math1 = 0;
        double math2 = 0;


        //KW Calculations
        if (kwUsed <= 350)
        {
            base = quarter;

        }else if (kwUsed <= 500)
        {
            math1 = ((kwUsed - 350) * quarter);
            base = ((kwUsed * quarter) - math1);
            over = ((math1 * 0.1) + math1);

        }else if (kwUsed > 500)
        {
            math2 = ((kwUsed - 350) * 0.1);
            base = ((kwUsed * 0.1) - math2);
            math2 = ((kwUsed -350) - 50); 
            over = ((math2 * 0.1) + (15 * 0.1));
            double math3 =((kwUsed - 500) * 0.1);
            excess = ((math3 * 0.25) + math3);

        }

编辑以澄清问题。

4

1 回答 1

0

一定是输入kwUsed = 0switch日期或日期错误。quarter如果这些值正确,则不能为零。

double price = 0;
switch (input)
    {
        case "2/15/13":
            price = 0.10;
            break;
        case "4/15/13":
            price = 0.12;
            break;
        case "8/15/13":
            price = 0.15;
            break;
        case "11/15/13":
            price = 0.15;
            break;
        default:
            System.out.println("Invalid date");
    }


    //Declaring variables for calculations
    double base = 0;
    double over = 0;
    double excess = 0;

    double math1 = 0;
    double math2 = 0;


    //KW Calculations
    if (kwUsed <= 350)
    {
        base = kwUsed * price;

    }else if (kwUsed <= 500)
    {
        math1 = ((kwUsed - 350) * price);
        base = ((kwUsed * price) - math1);
        over = ((math1 * 0.1) + math1);

    }...
于 2013-10-28T09:10:24.050 回答