问题描述
嗨!
所以我想用以下规则制作电费计算器:
So I want to make an electricity bill calculator with this rule:
前100千瓦时, ; nbsp; b ; 每千瓦时25美分
First 100 Kwh, 25 cents per Kwh
接下来的100千瓦时(最高200千瓦时)中的每一个, 每千瓦时35美分
Each of the next 100 Kwh (up to 200 Kwh), 35 cents per Kwh
(使用的前100千瓦时,每个仍收取25美分的费用)
(the first 100 Kwh used is still charged at 25 cents each)
接下来的200千瓦时(最高400千瓦时)中的每一个 每千瓦时50美分
Each of the next 200 Kwh (up to 400 Kwh) 50 cents per Kwh
所有 KHwh 超过400, &bb ; nbsp; b ; 每KH 60美分
All KHwh over 400, 60 cents per KH
我的源代码存在的问题是存在一些格式问题.每次编译源代码时,编译器都会说"抛出的异常:mscorlib.dll中的'System.FormatException'".
The issue with my source code is that there are some formatting issue. Every time I compile my source code, the compiler would say "Exception thrown: 'System.FormatException' in mscorlib.dll".
这是我的代码:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Assignment_2
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
}
private void button1_Click(对象发送者,EventArgs e)
{
int uKhw = int.Parse(textBox1.Text);
double resKhw = double.Parse(textBox2.Text);
如果(uKhw> 2000)
{
MessageBox.Show("Khw不能超过2,000");
}
否则(uKhw< = 100)
{
resKhw = 0.25 + uKhw;
textBox2.Text = resKhw.ToString();
}
否则(uKhw> = 100&& uKhw< 200)
{
resKhw = 0.25 +(0.35 * uKhw);
textBox2.Text = resKhw.ToString();
}
否则(uKhw> = 200&& uKhw< 400)
{
resKhw = 0.25 +(0.50 * uKhw);
textBox2.Text = resKhw.ToString();
}
否则(uKhw> 400)
{
resKhw = 0.25 +(0.60 * uKhw);
textBox2.Text = resKhw.ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assignment_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int uKhw = int.Parse(textBox1.Text);
double resKhw = double.Parse(textBox2.Text);
if (uKhw > 2000)
{
MessageBox.Show("Khw cannot be more than 2,000");
}
else if (uKhw <= 100)
{
resKhw = 0.25 + uKhw;
textBox2.Text = resKhw.ToString();
}
else if (uKhw >= 100 && uKhw < 200)
{
resKhw = 0.25 + (0.35 * uKhw);
textBox2.Text = resKhw.ToString();
}
else if (uKhw >= 200 && uKhw < 400)
{
resKhw = 0.25 + (0.50 * uKhw);
textBox2.Text = resKhw.ToString();
}
else if (uKhw > 400)
{
resKhw = 0.25 + (0.60 * uKhw);
textBox2.Text = resKhw.ToString();
}
}
}
}
我希望你能帮助我.
谢谢.
推荐答案
private void button1_Click(对象发送者,EventArgs e)
{
int uKhw = int.Parse(textBox1.Text);
double resKhw = double.Parse(textBox2.Text);
private void button1_Click(object sender, EventArgs e)
{
int uKhw = int.Parse(textBox1.Text);
double resKhw = double.Parse(textBox2.Text);
我怀疑编译器会从编译器中抛出该异常.
当您尝试运行/调试程序时,很可能会发生这种情况.
该代码运行时,两个文本框中的值是什么?
如果字符串对于a
无效,则double.Parse()将引发异常.double类型,如果字符串对于int无效,则将引发int.Parse.
这就是为什么通常使用TryParse而不是Parse更好的原因-所以您
可以检查输入是否无效而不会触发异常.
Double.TryParse方法(字符串,双精度)
https://msdn.microsoft.com/en-us/library/994c0zb1 (v = vs.110).aspx
Int32.TryParse方法(字符串,Int32)
https://msdn.microsoft.com/en-us/library/f02979c7 (v = vs.110).aspx
-韦恩
I doubt that the compiler is throwing that exception from a compile.
It's more likely it happens when you try to Run/Debug the program.
What values are in the two TextBoxes when that code runs?
double.Parse() will throw an exception if the string is not valid for a
double type, and int.Parse will throw if the string isn't valid for an int.
That's why it's usually better to use TryParse instead of Parse - so you
can check for invalid input without an exception being triggered.
Double.TryParse Method (String, Double)
https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
Int32.TryParse Method (String, Int32)
https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
- Wayne
这篇关于C#格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!