这是对先前问题的跟进,得到了一些很好的反馈,我正在学习,寄存器的工作原理和计算结果很好。我想问问是否可以仅循环一个函数的一部分,我插入了一个if语句,该语句检查该人给的钱是否少于总数,以及如果给的钱要更多。我想再次要求现金,并在收到等于或大于总额的金额后继续进行找零。这是代码:
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace CSharp_Shell
{
public static class Program
{
public static void Main()
{
Console.WriteLine(" *****--------------------*****");
Console.WriteLine(" Wit Groceries 2020");
Console.WriteLine(" *****--------------------*****");
Console.WriteLine(" Welcome, would you like to do some shopping today?");
Console.WriteLine("");
var STOCK = new List<Items>
{
new Items{ id = 1, Name = "Bun", Price = 150},
new Items{ id = 2, Name = "Soda", Price = 100},
new Items{ id = 3, Name = "Cheese", Price =70},
new Items{ id = 4, Name = "Tissue", Price = 50},
new Items{ id = 5, Name = "Fabuloso", Price = 180},
new Items{ id = 6, Name = "Grace Mackerel", Price = 70},
new Items{ id = 7, Name = "Rice", Price = 50},
new Items{ id = 8, Name = "Flour", Price = 40},
new Items{ id = 9, Name = "Sugar", Price = 30},
};
var STOCKDict = STOCK.ToDictionary(i=>i.id);
var selecteditems = new List<Items>();
STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name.PadRight(20)} Price: {string.Format("{0:C2}", x.Price)}"));
Console.WriteLine();
Console.WriteLine(" Enter the number for the item you want, enter 'x' when finihshed");
do
{
var choice = Console.ReadLine();
if (choice.Trim() =="x")
{
break;
}
int id;
if (!int.TryParse(choice, out id))
{
Console.WriteLine("Enter number or 'x' ");
continue;
}
if (STOCKDict.ContainsKey(id))
{
var item = STOCKDict[id];
selecteditems.Add(item);
Console.WriteLine(item.Name + " is added to your list");
}
else
{
Console.WriteLine(id + " is not available");
}
}
while (true);
decimal subt = selecteditems.Sum(i=>i.Price);
decimal GCT = 16.50m;
decimal GCTamt = (subt*GCT/100);
decimal total = subt+GCTamt;
Console.WriteLine(" Sub-total = " + "$" + subt);
Console.WriteLine(" GCT = " + "$" + GCTamt);
Console.WriteLine(" You pay " + "$" +total);
Console.WriteLine();
Console.WriteLine(" Please tender cash");
decimal tend =decimal.Parse(Console.ReadLine());
if (tend>total)
{
decimal change = tend - total;
Console.WriteLine(" You gave " + " $"+tend + " Your change is " + "$"+change);
}
else
{
Console.WriteLine(" Please tender more cash");
}
}
}
public class Items
{
public int id {get; set;}
public string Name{get; set;}
public decimal Price {get;set;}
}
}
从您的需求看来,您只关心这部分代码:
Console.WriteLine(" Please tender cash");
decimal tend =decimal.Parse(Console.ReadLine());
if (tend>total)
{
decimal change = tend - total;
Console.WriteLine(" You gave " + " $"+tend + " Your change is " + "$"+change);
}
else
{
Console.WriteLine(" Please tender more cash");
}
应用现金找零逻辑的地方。如果我正确理解,可以执行以下操作:
decimal total = 15.75m;
while (true)
{
Console.WriteLine(" Please tender cash");
decimal tend = decimal.Parse(Console.ReadLine());
if (tend == total)
{
Console.WriteLine(" You gave " + " $" + tend + "the exact amount.");
Console.ReadLine();
break;
}
if (tend > total)
{
decimal change = tend - total;
Console.WriteLine(" You gave " + " $" + tend + " Your change is " + "$" + change);
Console.ReadLine();
break;
}
total -= tend;
Console.WriteLine(" You gave " + " $" + tend);
Console.WriteLine(" You still own " + " $" + total);
}
在while循环的每次迭代中-我们请求现金-如果现金大于或等于其拥有的金额-循环完成并提供反馈。否则,我们从总数中减去给定的数量并重复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句