【问题标题】:How can I method in the other method?我怎样才能用另一种方法?
【发布时间】: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


【解决方案1】:

由于 originalPrice 是一个公共静态方法,您可以像这样使用它:MyCoffeeOutlet.originalPrice(paramHere)

【讨论】:

    【解决方案2】:

    我不确定我是否正确理解了您的问题。据我了解,您希望能够在您的方法discountPrice() 中使用您的方法originalPrice()。您遇到的问题是 orignalPrice() 接受了许多袋子,但您在 discountPrice() 中没有这些信息。

    如果这是您的问题,最简单的解决方法是让discountPrice() 接受折扣金额行李数量。如:

    public static double discountPrice(double discount, int bagsAmount){ }

    然后,当您调用discountPrice() 方法时,请确保发送两条信息。

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2014-08-13
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      相关资源
      最近更新 更多