0

我已经发布了从我的 if-else 语句中提取代码。我需要从两个糖果的信息中打印订单总额。设置为静态显然不起作用,因为价格将始终为 0。我尝试在其他地方声明变量以使其工作。我可能做得不对。感谢您的帮助!

public class Program3Main {

    String candy1 = "";
    String candy1name = "";
    double candy1price;

    String candy2 = "";
    String candy2name = "";
    double candy2price;

    int numCandy1;
    int numCandy2;

    double totalCostCandy1;
    double totalCostCandy2;
    double totalOrder;

    double totalCostCandy1p1;
    double totalCostCandy1p2;

    double totalCostCandy2p1;
    double totalCostCandy2p2;

    public static void main(String args[]) 
    {

        System.out.println("          Coulter's Candy Shop\n");

        // This is the retailer section \\
        System.out.println("--------------- Retailer ---------------\n");

        // This sub-section asks the user to enter the first candies name and price.
        System.out.println("Please enter the name of the first candy: ");
        Scanner s= new Scanner(System.in); // This is the scanner that will collect information.
        String candy1name=s.next();

        System.out.println("Please enter how much the first candy costs: ");
        double candy1price=s.nextDouble();


        // This sub-section asks the user to enter the second candies name and price.
        System.out.println("Please enter the name of the second candy: ");
        String candy2name=s.next();

        System.out.println("Please enter how much the second candy costs: ");
        double candy2price=s.nextDouble();
        System.out.println("");


        // This is the consumer section \\
        System.out.println("--------------- Consumer ---------------\n");
        System.out.println("How many boxes of " + candy1name + " would you like to purchase?");
        int numCandy1=s.nextInt(); // Takes user input for # of candy 1.

        System.out.println("How many boxes of " + candy2name + " would you like to purchase?");
        int numCandy2=s.nextInt(); // Takes user input for # of candy 2.
        System.out.println("");

        int totalCandy = numCandy1 + numCandy2;


        System.out.println("---- Candy Units Price / Units Cost ----\n");
        // If and else statements to make sure candy 1 isn't above 12 (Determine what equation to exe) \\
        if (numCandy1 <= 12)
        {       
            double totalCostCandy1 = candy1price * numCandy1;

            System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);

        }
        else if (numCandy2 > 12)// Else to calculate 5% discount on any candy amount > 12
        {
            double totalCostCandy1p1 = 12 * candy1price;
            double totalCostCandy1p2 = (numCandy1 - 12) * (candy1price * .95 );
            double totalCostCandy1 = (totalCostCandy1p1 + totalCostCandy1p2);

            double moneySavedCandy1 = (numCandy1-12) * (candy1price * .5);
            System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);
            System.out.println("You saved $" + moneySavedCandy1 + "!");
            System.out.println("");
        }
        else 
        {
            totalCostCandy1 = 0;
        };


        // If and else statements to make sure candy 2 isn't above 12 (Determine what equation to exe) \\
        if (numCandy2 <= 12)
        {
            double totalCostCandy2 = candy2price * numCandy2;

            System.out.println("Your boxes of " + candy2name + " cost $" + totalCostCandy2);
            System.out.println("");

            double totalOrderp2 = totalCostCandy2;
        }
        else if (numCandy2 > 12) // Else to calculate 5% discount on any candy amount > 12
        {
            double totalCostCandy2p1 = 12 * candy1price;
            double totalCostCandy2p2 = (numCandy2 - 12) * (candy2price * .95 );
            double totalCostCandy2 = (totalCostCandy2p1 + totalCostCandy2p2);

            double moneySavedCandy2 = (numCandy2-12) * (candy2price * .5);
            System.out.println("Your boxes of " + candy2name + " cost $" + totalCostCandy2);
            System.out.println("You saved $" + moneySavedCandy2 + "!");
            System.out.println("");

        }
        else
        {
            totalCostCandy2 = 0;
        };

        totalOrder = totalCostCandy1 + totalCostCandy2;
        System.out.println("Total Cost: $" + totalOrder);

        //double totalOrder = totalCostCandy1 + totalCostCandy2;
        //System.out.println("The total cost of your order is $" + totalOrder); 
    }
}
4

1 回答 1

0

是一个静态函数,这main(String[] args)意味着它属于类而不是类的对象。

您在其中声明的成员字段

String candy1 = "";
String candy1name = "";
double candy1price;
.
.
.
double totalCostCandy2p1;
double totalCostCandy2p2;

属于类的实例化对象Program3Main。所以不能使用这些变量。在这种情况下,您可以将这些变量声明为静态变量,以便可以在静态函数的范围内访问它们。

static String candy1 = "";
static String candy1name = "";
...

您在 if-else 块中声明的变量只能在 if-else 块中访问。您可以在整个函数的范围内声明变量,并在 if-else 块中为它们赋值。

所以而不是

if (numCandy1 <= 12) {       
    double totalCostCandy1 = candy1price * numCandy1;
    System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);
}

double totalCostCandy1;
if (numCandy1 <= 12) { 
    totalCostCandy1 =  = candy1price * numCandy1;      
    System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);
}
于 2019-02-18T18:08:27.170 回答