gpt4 book ai didi

java - 我怎样才能在其他方法中使用方法?

转载 作者:行者123 更新时间:2023-12-01 18:16:02 25 4
gpt4 key购买 nike

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方法? **

最佳答案

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

关于java - 我怎样才能在其他方法中使用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com