C# 实现商品购买类系统
循环输入商品编号和购买数量,系统自动计算每种商品的价钱(单价X数量),并累加总金额。
当输入n时结账(假设享受8折优惠)
结账时,根据折扣计算应付金额,输入实付金额,并找零:
代码如下:
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
double totalprices = 0;
double totalprices2;
double totalprices3;
double shirt = 245.0d;
double shoes = 570d;
double racket = 1000d;
Console.WriteLine("***********************************");
Console.WriteLine("请选择购买的商品编号:");
Console.WriteLine("1.T恤 2.网球鞋 3.网球拍");
Console.WriteLine("***********************************");
for (int K = 1; K > 0; K++)
{
Console.WriteLine("请输入商品编号:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入购买数量:");
b = Convert.ToInt32(Console.ReadLine());
if (a == 1)
{
totalprices += shirt * b;
Console.WriteLine("T恤 ¥{0} 数量{1} 合计{2}", shirt, b,totalprices);
}
if (a == 2)
{
totalprices2 = shoes * b ;
totalprices += totalprices2;
Console.WriteLine("网球鞋 ¥{0} 数量{1} 合计{2}", shoes, b, totalprices2);
}
if (a == 3)
{
totalprices3 = racket * b;
totalprices += totalprices3;
Console.WriteLine("网球拍 ¥{0} 数量{1} 合计{2}", racket, b, totalprices3);
}
Console.WriteLine("是否继续(y/n)");
string s = Console.ReadLine();
if (s == "n")
{
double yingFu = totalprices * 0.8;
Console.WriteLine("折扣:0.8");
Console.WriteLine("应付款金额为:{0}", yingFu);
Console.WriteLine("实际付款金额:");
double p = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("找钱:{0}", p - yingFu);
break;
}
}
}
}
}