java 特价菜品打折
时间: 2024-05-25 11:09:22 浏览: 10
Java是一种面向对象编程语言,主要应用于开发各种应用程序,包括Web应用程序、移动应用程序、桌面应用程序等。在Java中,特价菜品打折可以通过定义折扣类来实现。我们可以创建一个Discount类,其中包含一个calculateDiscount方法,该方法接受商品价格和折扣百分比作为参数,并返回打折后的价格。
下面是一个示例Discount类的代码:
```
public class Discount {
public double calculateDiscount(double price, double discountPercentage) {
double discountAmount = price * discountPercentage / 100;
double discountedPrice = price - discountAmount;
return discountedPrice;
}
}
```
使用上述Discount类,我们可以通过以下代码计算特价菜品的打折价格:
```
Discount discount = new Discount();
double originalPrice = 100.0;
double discountPercentage = 20.0;
double discountedPrice = discount.calculateDiscount(originalPrice, discountPercentage);
System.out.println("Original Price: " + originalPrice);
System.out.println("Discount Percentage: " + discountPercentage);
System.out.println("Discounted Price: " + discountedPrice);
```
这将输出以下内容:
```
Original Price: 100.0
Discount Percentage: 20.0
Discounted Price: 80.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)