C# 使用C语言更改计算器#
我正试图写一个程序,采取的价格和金额支付和输出的变化面额。我有以下问题C# 使用C语言更改计算器#,c#,while-loop,C#,While Loop,我正试图写一个程序,采取的价格和金额支付和输出的变化面额。我有以下问题 我还希望只打印适用的更改面额(即,如果更改为71,则只显示FithyBills=1、TwentyBills=1和Bills=1,而忽略其余部分)。目前,它显示所有,其他记录为10张票据=0,以此类推) 这是密码 using System; namespace ChangeCalulator { class Program { static void Main(string[] args)
- 我还希望只打印适用的更改面额(即,如果更改为71,则只显示FithyBills=1、TwentyBills=1和Bills=1,而忽略其余部分)。目前,它显示所有,其他记录为10张票据=0,以此类推)
using System;
namespace ChangeCalulator
{
class Program
{
static void Main(string[] args)
{
Console.Write("Item Price: ");
int price = Convert.ToInt32(Console.ReadLine());
Console.Write("Amount Paid: ");
int paid = Convert.ToInt32(Console.ReadLine());
while (paid < pice)
{
Console.WriteLine("You have paid less than the price");
Console.Write("Paid: ");
paid = Convert.ToInt32(Console.ReadLine());
}
decimal changeamount = paid - price;
Change change = new Change(changeamount);
//This displays the change based on each denominantion
Console.WriteLine($"Hundred SEK Bills: {change.HundredBills}");
Console.WriteLine($"Fifty SEK Bills: {change.FiveBills}");
Console.WriteLine($"Twenty SEK Bills: {change.TwentyBills}");
Console.WriteLine($"Ten SEK Bills: {change.TenBills}");
Console.WriteLine($"Five SEK Bills: {change.FiveBills}");
Console.WriteLine($"One SEK: {change.Bills}");
Console.ReadKey();
}
}
//The Change class will create a public integer variable for each denomination and store the values.
public class Change
{
private int changeamount;
public int HundredBills { get; }
public int FiftyBills { get; }
public int TwentyBills { get; }
public int TenBills { get; }
public int FiveBills { get; }
public int Bills { get; }
//This calculates the number of each denomination the amount equals
public Change(decimal price)
{
HundredBills = (int)(changeamount / 100);
price %= 100;
FiftyBills = (int)(changeamount / 50);
price %= 50;
TwentyBills = (int)(changeamount / 20);
price %= 20;
TenBills = (int)(changeamount / 10);
price %= 10;
FiveBills = (int)(changeamount / 5);
price %= 5;
Bills = (int)(changeamount / 1);
price %= 1;
}
}
}
使用系统;
名称空间更改计算器
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台。写入(“项目价格:”);
int price=Convert.ToInt32(Console.ReadLine());
控制台。写入(“支付金额:”;
int paid=Convert.ToInt32(Console.ReadLine());
同时(已支付
非常感谢您在这方面提供的任何帮助。在构造函数
公共更改(十进制价格)
中,您需要使用price
:
public Change(decimal price)
{
HundredBills = (int)(price / 100);
price %= 100;
FiftyBills = (int)(price / 50);
price %= 50;
TwentyBills = (int)(price / 20);
price %= 20;
TenBills = (int)(price / 10);
price %= 10;
FiveBills = (int)(price / 5);
price %= 5;
Bills = (int)(price / 1);
price %= 1;
}
然后,您可以删除不需要的变更金额
字段
工作演示:
至于不打印0金额,一个简单的
if(theValue>0)
检查每个WriteLine就可以做到这一点。那么问题到底是什么呢?不要让我们猜测,也不要花很多时间试图弄明白。请确切地解释一下你被困在哪里。让人们更容易帮助你!你能再解释一下你的第二点吗。还不清楚。假设账单是71,那么您想打印什么?Console.WriteLine($“50瑞典克朗账单:{change.FiveBills}”)代码>应该是改变吗。五十张钞票
?我已经做了改变并澄清了问题。谢谢。看起来您需要在设置每个*账单
金额后重新计算更改金额
。在调用Console.WriteLine
之前,检查每个*Bills
属性的0。我之前的评论仍然是…谢谢。这解决了第二个问题