从另一种方法编辑case语句?

时间:2014-03-20 04:24:24

标签: java methods case-statement

对于我正在为学校写的程序,其中一个扩展说我需要想办法编辑一个case语句如果我作为管理员输入,我创建了两个方法,因为它似乎是最好的方法来做到这一点

方法一(我需要编辑的那个):

public static double costSqM(Console c) {
    double price = 0;
    int choice;
    c.println("#. Type               Cost per sq.m.");
    c.println("1. Low pile carpet           $ ");
    c.print(pileCost, 5, 2);
    c.println("2. Shag rug                  $ ");
    c.print(rugCost, 5, 2);
    c.println("3. Parquet                   $ ");
    c.print(parquetCost, 5, 2);
    c.println("4. Linleum                   $ ");
    c.print(leumCost, 5, 2);
    c.println("5. Hardwood                  $ ");
    c.print(hardwoodCost, 5, 2);
    c.println("Please enter your choice: ");
    choice = c.readInt();
    while (choice < 1 && choice > 5) {
        c.println("That is not a valid choice. Please try again!");
        choice = c.readInt();
    }
    switch (choice) {
    case 0:
        admin(c);
    case 1:
        price = 18.75;
    case 2:
        price = 11.05;
    case 3:
        price = 14.35;
    case 4:
        price = 10.40;
    case 5:
        price = 28.15;
    }
    return price;
}

方法二(我正在编辑的方法):

public static void admin(Console c) {
    int choice = 0, priceChange = 0;
    cls(c);
    c.println("Hello admin. What changes would you like to make?");
    c.println("1. Change a price.");
    c.println("2. Add an item.");
    c.println("3. Remove an item.");
    switch(choice){
    //Changing the price of an item
    case 1: 
        c.print("What price would you like to change? ");
        c.println("1. Low Pile Carpet.");
        c.println("2. Shag Rug.");
        c.println("3. Parquet.");
        c.println("4. Linleum.");
        c.println("5. Hardwood.");
        switch(priceChange){
        case 1:
            c.println("Please enter the new price for the Low Pile Carpet: ");
            pileCost = c.readInt();
        case 2:
            c.println("Please enter the new price for the Shag Rug: ");
            rugCost = c.readInt();
        case 3:
            c.println("Please enter the new price for the Parquet: ");
            parquetCost = c.readInt();
        case 4:
            c.println("Please enter the new price for the Linluem: ");
            leumCost = c.readInt();
        case 5:
            c.println("Please enter the new price for the Hardwood: ");
            hardwoodCost = c.readInt();
        }
    //Adding an item
    case 2:
    //Removing an item
    case 3:

    }

如果不能用其他方法做,但它来自同一个方法,请解释一下,我可以做一个大方法。感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用管理模式的标志。将其设置为true并且您处于管理员模式:
boolean admin = true

然后您只需检查admin是否为true,如果是这种情况,请打印管理员资料,否则...打印正常输出。

用户键入1:

的示例
case 1:
    if(admin)
    {  //admin, change price
        c.println("Please enter the new price for the Low Pile Carpet: ");
        pileCost = c.readInt();
    }
    else
    { //non-admin, set price
        price = 18.75;
    }
break; //important! don't forget the break-keyword

如果您忘记在案例陈述结尾处添加break,则将运行以下所有陈述(例如2:3,4,5 ..)。

这应该是一种灵感,你应该自己弄清楚其余部分:)

哦,当然,你最终只会采用这种方法,因为逻辑不重复,所以这比前面的两个方法要小。