请帮助..创建默认和最大数量 [英] Please Help.. Creating a default and maximum number
问题描述
我的程序现在正在运行并正确编译,但我仍然遇到设置最大和默认安装的问题。
我真的很感激一些指示。我是C#的新手,所以非常感谢任何帮助!
一些指导原则:'H'最多可以安装5个。默认值为1.
'E'默认包含10个安装。对于10以上的每个安装,都会添加额外的价格。
'H'和'E'都有默认的附加程序0.数字不能是负数。
这是我的到目前为止的代码:
I have my program working and compiling correctly now, but I am still having an issue getting the maximum and default installations set.
I would really appreciate some pointers. I'm new to C#, so any help is greatly appreciated!
A few guidelines: 'H' can have a maximum of 5 installations. The default is 1.
'E' has a default of 10 installations included. For each installation above 10, the additional price is added.
Both 'H' and 'E' have a default of additional programs of 0. Number must not be negative.
Here is my code thus far:
private void btnCalculate_Click(object sender, EventArgs e)
{
const decimal U_CUST = 999;
const decimal E_CUST = 249;
const decimal ADD_E_CUST = 99;
const decimal H_CUST = 99;
const decimal ADD_H_CUST = 49;
char CustType;
int AdditionalPrograms = 0;
int Installations = 10;
decimal Bill = 0;
if (txtCustType.Text.Length != 1)
{
MessageBox.Show("Customer type is required.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCustType.Focus();
return;
}
CustType = char.Parse(txtCustType.Text);
CustType = char.ToUpper(CustType);
txtCustType.Text = CustType.ToString();
if (CustType != 'U' && CustType != 'H' && CustType != 'E')
{
MessageBox.Show("Invalid customer type - must be H, E or U.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCustType.Focus();
return;
}
//calculate bill according to customer type
if (CustType == 'U')
{
Bill = U_CUST;
}
else
{
if (txtAddPrograms.Text != "") // AdditionalPrograms will remain at 0 if textbox is empty
{
bool isValid = int.TryParse(txtAddPrograms.Text, out AdditionalPrograms);
if (!isValid || AdditionalPrograms < 0)
{
MessageBox.Show("Invalid number of additional programs - can't be negative.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtAddPrograms.Focus();
return;
}
}
if (CustType == 'H')
{
Bill = H_CUST + ADD_H_CUST * AdditionalPrograms;
}
else // Customer Type must be 'E'
{
if (txtInstallations.Text != "") // Installations will remain at 10 if textbox is empty
{
bool isValid = int.TryParse(txtInstallations.Text, out Installations);
if (!isValid || Installations < 1)
{
MessageBox.Show("Invalid number of installations - must be positive.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtInstallations.Focus();
return;
}
}
Bill = E_CUST + ADD_E_CUST * AdditionalPrograms;
if (Installations > 10) Bill *= 1 + (Installations - 10) * 0.1m;
}
}
//Display the result.
lblBill.Text = Bill.ToString("C");
if (CustType == 'E' && Installations > 40)
{
MessageBox.Show("Note that the unlimited plan would be more economic.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
推荐答案
好的 - 让我们再看看这个...
那里有一些奇怪的代码 - 我认为你不知道字符串
可以被处理作为char
的数组?所以你可以替换它:
OK - let's have another look at this...
There is some rather odd code there - I take it you don't know that astring
can be treated as an array ofchar
s? So you could replace this:
CustType = char.Parse(txtCustType.Text);
CustType = char.ToUpper(CustType);
txtCustType.Text = CustType.ToString();
这个:
With this:
txtCustType.Text = txtCustType.Text.ToUpper();
CustType = txtCustType.Text[0];
哪个更明显一点。
但是......我个人也不会乱用角色 - 使用直接改为字符串! :笑:
其次,你似乎花了相当多的时间来转换数字并检查它们是否正面 - 你是否仅限于使用TextBoxes?因为如果你不是,那么有一个名为NumericUpDown的方便控件 - 它只能在一定范围内保存有效数字,并且不需要检查或转换,因为它提供十进制
直接值。这可以清理你的代码!
Which is a little more obvious.
But...personally I wouldn't mess about with characters anyway - use the strings directly instead! :laugh:
Secondly, you seem to spend a fair amount of time converting numbers and checking they are positive - are you restricted to using TextBoxes? Because if you aren't, then there is a handy control called a NumericUpDown - which can only ever hold valid numbers in a certain range, and needs no checking or conversion as it provides a decimal
value directly. This could clean your code up lots!
这篇关于请帮助..创建默认和最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!