Unity的C#编程教程_17_Variables 挑战 3 折扣计算器
- 2020 年 7 月 31 日
- AI
- C/C++/C#
- 使用变量设计一个 折扣计算器
- 比如我们需要知道原价,需要知道打几折,最后计算一个结算价,然后显示出来所有信息
- 怎么在程序里实现呢?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiscountCalculator : MonoBehaviour
{
public float priceOld;
public int discount;
public float priceNew;
void Start()
{
float discountChange = discount / 10;
priceNew = priceOld * discountChange;
Debug.Log("原价: " + priceOld + ";打" + discount + "折" + ";现价: " + priceNew);
}
void Update()
{
}
}
- 把脚本挂载到 Main Camera 下面
- 输入原价,比如987,打几折,比如2
- 在 console 里显示最后结果 197.4