Windows 窗体应用程序 C# - 更改数组 [英] Windows Form Application C# - Change Array

查看:45
本文介绍了Windows 窗体应用程序 C# - 更改数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个 Windows 窗体应用程序,它会告诉您输入的金额需要每种类型的硬币多少.首先,我有一个包含 5 个文本框和一个按钮的表单.第一个文本框供用户输入他们拥有的金额.其余的都是只读的,因此它们只会显示获得用户输入的金额所需的每种硬币的数量.按钮在那里,所以他们可以按下它,然后计算他们需要的每个硬币的数量.

I need to make a Windows Form Application that will tell you how much of each type of coin you will need for the amount entered.To start I have a form with 5 text boxes and a button. 1st text box is for the user to enter the amount they have. The rest are read only so they will only display how many of each coin you will need to get that amount the user enters. Button is there so they can push it and then calculate the number of each coin they need will be displayed.

示例:- 如果用户输入 0.15,则在按下文本框中的按钮后,一角硬币将为 1,而 nikel 文本框将为 1.

Example: - If user enters .15 then after pushing button in text box for dime will be 1 and text box for nikel will be 1.

我遇到了麻烦,因为我不确定在为每个硬币制作一个数组后我可以做什么.我知道下一步是让表格能够获取输入的金额并查看哪些硬币将达到输入的金额,但我不知道如何开始.这里我的数组位于我的计算按钮的点击事件中.有人有建议吗?谢谢(* 改是数组名,因为硬币是零钱,不想改数组)

I'm having trouble because I'm not sure what I can do next after making an array for each of the coins. I know the next step is for the form to be able to take the entered amount and see which coins will get to entered amount but I'm not sure how to start it. Here my array thats inside the click event for my calculate button. Does anyone have a suggestion? Thank you (* change is the name of array as in coins are change, don't want to change the array)

     int[] change= new int[3];
        change[0] = 1;
        change[1] = 5;
        change[2] = 10;
        change[3] = 25;

推荐答案

您可以使用 Math.DivRem 方法来做到这一点.

You can use the Math.DivRem method to do that.

示例:

var amount = new Random().Next(1, 101);

Console.WriteLine($"{amount} Change:");

var Quarters = Math.DivRem(amount, 25, out amount);
var Dimes = Math.DivRem(amount, 10, out amount); 
var Nickels = Math.DivRem(amount, 5, out int Pennies);

Console.WriteLine($"Quarters: {Quarters}, Dimes: {Dimes}, Nickels: {Nickels}, Pennies: {Pennies}");

以下是一些输出:

52 变化:
四分之一:2,角钱:0,镍:0,便士:2

52 Change:
Quarters: 2, Dimes: 0, Nickels: 0, Pennies: 2

34 变化:
季度:1,角钱:0,镍:1,便士:4

34 Change:
Quarters: 1, Dimes: 0, Nickels: 1, Pennies: 4

11 个变化:
四分之一:0,一角硬币:1,镍:0,便士:1

11 Change:
Quarters: 0, Dimes: 1, Nickels: 0, Pennies: 1

99 变化:
四分之一:3,角钱:2,镍:0,便士:4

99 Change:
Quarters: 3, Dimes: 2, Nickels: 0, Pennies: 4

这篇关于Windows 窗体应用程序 C# - 更改数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆