我已经发布了从我的 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);
}
}