【发布时间】:2020-02-23 20:44:33
【问题描述】:
import java.util.*;
public class MyCoffeeOutlet {
//Hold the value of original price
public static double originalPrice(int bag) {
final double COFFE_PRICE = 5.50; // The price of coffe per bag
double originalPrice = 0;
originalPrice = bag * COFFE_PRICE; // before discounted price
return originalPrice;
}
//Hold the value of discounted price
public static double discountPrice(double discount) {
double discountPrice = 0;
discountPrice = originalPrice(bagsAmount) * discount; // to find discount price
return discountPrice;
}
public static void main(String[] args) {
// To hold the Total bag amount
int bagsAmount = 0;
// to hold the value of total price
double totalPrice, discount = 0;
// To show how much discounted.
int percent1 = 5,
percent2 = 10,
percent3 = 15,
percent4 = 20,
percent5 = 25,
percent6 = 30;
// to put suitable percent in to this variable
int percent = 0;
Scanner scanner = new Scanner(System.in);
// Get the bag amount from the Customer
System.out.println("How many bags do you want to buy? : ");
bagsAmount = scanner.nextInt();
// If it under 0, it will not count.
while (bagsAmount < 0) {
System.out.println("Input positive number or zero");
System.out.println("How many bags do you want to buy? : ");
bagsAmount = scanner.nextInt();
}
scanner.close();
// How much the customer get the discount?
if( bagsAmount >= 25 && bagsAmount < 50) {
discount = 0.05;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent1;
} else if (bagsAmount >= 50 && bagsAmount < 100 ) {
discount = 0.1;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent2;
} else if (bagsAmount >= 100 && bagsAmount < 150) {
discount = 0.15;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent3;
} else if (bagsAmount >= 150 && bagsAmount < 200) {
discount = 0.2;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent4;
} else if (bagsAmount >= 200 && bagsAmount < 300) {
discount = 0.25;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent5;
} else if (bagsAmount >= 300) {
discount = 0.3;
originalPrice(bagsAmount);
discountPrice(discount);
totalPrice = originalPrice(bagsAmount) - discountPrice(discount);
percent = percent6;
} else {
totalPrice = originalPrice(bagsAmount);
}
System.out.println("Number of Bags Ordered : " + bagsAmount + " - " + " $ "
+ originalPrice(bagsAmount));
System.out.println("\t" + " Discount :" + "\n\t\t\t "
+ percent + "%" + " - $ " + discountPrice(discount));
System.out.println("Your total charge is : " + " $ " + totalPrice);
}
}
这是我的代码。我想在 discountPrice 方法中使用 originalPrice 方法。但我不知道如何将参数放入 originalPrice 以便在 discountPrice 方法中使用。我想使用 if 语句向控制台输出。如何直接使用 originalPrice 方法? **
【问题讨论】:
-
你能举个例子吗?
标签: java methods parameters