short price = ...;//先计算出原价 short realPrice = price * 8 / 10;//再计算出打折之后 哪里不正确
时间: 2024-04-11 19:31:54 浏览: 19
在计算打折后的价格时,应使用浮点数进行计算,而不是整数。因此,正确的计算方法是将价格乘以0.8(或除以10再乘以8)来得到打折后的价格。修改后的代码如下:
float price = ...; // 先计算出原价
float realPrice = price * 0.8; // 再计算出打折之后的价格
相关问题
如何解析这个json数据{"code": 0,"data": {"platPrice": 836,"realPrice": 820},"msg": ""}
要解析这个 JSON 数据,你可以使用以下代码示例:
```csharp
[System.Serializable]
public class ResponseData
{
public int code;
public Data data;
public string msg;
}
[System.Serializable]
public class Data
{
public int platPrice;
public int realPrice;
}
void Start()
{
string json = "{\"code\": 0,\"data\": {\"platPrice\": 836,\"realPrice\": 820},\"msg\": \"\"}";
ResponseData responseData = JsonUtility.FromJson<ResponseData>(json);
Debug.Log(responseData.code);
Debug.Log(responseData.msg);
Debug.Log(responseData.data.platPrice);
Debug.Log(responseData.data.realPrice);
}
```
在这个示例中,我们定义了两个类 `ResponseData` 和 `Data` 来匹配 JSON 数据的结构。然后使用 `JsonUtility.FromJson()` 方法将 JSON 字符串解析为 `ResponseData` 对象,并通过对象的属性来访问解析后的数据。
上述代码中使用了 Unity 内置的 `JsonUtility` 类进行解析。如果你想使用其他的 JSON 解析库,例如 LitJson,你可以根据该库的文档和示例代码来进行解析。
c用switch语句输出实付金额
首先需要明确实付金额是指在购物时实际需要支付的金额,可能会受到商品价格、折扣等多种因素的影响。因此,在使用switch语句输出实付金额时,需要先获取这些因素的值,进而根据不同情况进行计算和比较。
假设购物时需要考虑商品价格、会员折扣和优惠券等三个因素,比较常见的算法如下:
1. 首先获取商品价格和购买数量等信息,计算出购买商品的总金额。
2. 根据会员身份和级别等因素,获取对应的折扣率。
3. 计算出折扣后的实际金额(即总金额乘以折扣率)。
4. 如果有优惠券,则根据优惠券类型和金额等信息进行判断,计算出实付金额。
5. 如果没有优惠券,则实付金额即为折扣后的实际金额。
在使用switch语句输出实付金额时,可以按照以上算法进行编写。具体实现方式如下:
1. 设置一个变量,用于存储实付金额。
2. 根据不同情况,使用switch语句进行判断和计算。
3. 输出实付金额。
以下是示例代码:
int price = 100; //商品单价
int num = 2; //购买数量
int vipLevel = 1; //会员等级
double discount = 1.0; //折扣率
double coupon = 0.0; //优惠券金额
double realPrice = 0.0; //实付金额
//计算总金额
double total = price * num;
//根据会员等级获取折扣率
switch(vipLevel){
case 1:
discount = 0.9;
break;
case 2:
discount = 0.8;
break;
case 3:
discount = 0.7;
break;
default:
break;
}
//计算折扣后的实际金额
realPrice = total * discount;
//使用优惠券
switch(couponType){
case 1:
coupon = 10;
break;
case 2:
coupon = 20;
break;
default:
break;
}
//计算实付金额
if(coupon > 0){
realPrice = realPrice - coupon;
}
//输出实付金额
cout<<"实付金额为:"<<realPrice<<endl;
综上所述,使用switch语句输出实付金额需要先获取商品价格、折扣和优惠券等信息,然后按照算法进行判断和计算,最后输出实付金额。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)