我目前正在使用Visual Studio中的Windows窗体进行第一次作业,并且有点卡住了。
我正在尝试测试用户输入是否为数字,如果输入不是数字,则弹出一个小消息框并提醒用户。 (我已完成此部分)。我遇到的问题是在Visual Studio中继续出现错误
这是格式化问题吗?我应该重新格式化我的代码吗?你可能会看到我是C#(和编程)的新手。
我的代码:
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 wtgCalculator {
public partial class Form1 : Form {
const double maleratio = 0.536;
const double femaleratio = 0.492;
public Form1() {
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void Form1_Load(object sender, EventArgs e) {
}
private void label1_Click(object sender, EventArgs e) {
}
private void textBox1_TextChanged_1(object sender, EventArgs e) {
}
private void WaistNumericCheck() {
double waist;
string inpwaist = heighttextbox.Text;
if (double.TryParse(inpwaist, out waist)) {
MessageBox.Show("Value must be numeric"); //this is where the problem is
}
}
private void HeightNumericCheck() {
//todo
}
private void button1_Click(object sender, EventArgs e) {
//record inputs to variables
WaistNumericCheck();
HeightNumericCheck();
double height = double.Parse(heighttextbox.Text);
double waist = double.Parse(waisttextbox.Text);
//check is inputs are within boundry
CheckDimensions(height, waist);
//test
// ShowResult(height, waist);
}
private void CheckLimits(double height, double waist) {
double result = CalculateRatio(height, waist);
if (Female.Checked) {
if (result < femaleratio) {
MessageBox.Show("Your risk of obesity related cardiovasular is low");
}
if (result > femaleratio) {
MessageBox.Show("Your risk of obesity related to cardiovascular is high");
}
}
if (Male.Checked) {
if (result < maleratio) {
MessageBox.Show("Your risk of obesity related cardiovasular is low");
}
if (result > maleratio) {
MessageBox.Show("Your risk of obesity related cardiovasular is High");
}
}
//testing
MessageBox.Show(result.ToString());
}
private void ShowResult(double height, double waist) {
//double result = CalculateRatio(height, waist);
//if (Female.Checked) {
// if (result < femaleratio) {
// MessageBox.Show("Your risk of obesity related cardiovasular is low");
// }
// if (result > femaleratio) {
// MessageBox.Show("Your risk of obesity related to cardiovascular is high");
// }
//}
//if (Male.Checked) {
//}
}
private static void CheckDimensions(double height, double waist) {
if (height <= 120) {
MessageBox.Show("Height must be greater than 120cm");
}
if (waist <= 60) {
MessageBox.Show("Waist must be greater than 60cm");
}
}
private void Gender_CheckedChanged(object sender, EventArgs e) {
button1.Enabled = true;
}
private static double CalculateRatio(double height, double waist) {
double finalratio = waist / height;
return finalratio;
}
}
}
再次感谢,如果需要更多信息,请与我们联系。
答案 0 :(得分:0)
你并没有真正正确地处理这个问题。如果无法转换该值,则.Parse()
假设会抛出异常。 .TryParse()
的重点是避免这种情况。因此,您的方法需要进行清理和编辑才能真正实现目标:
private bool WaistNumericCheck(out double waist)
{
bool canParse = double.TryParse(heighttextbox.Text, out waist);
if ( !canParse ) {
MessageBox.Show("Value must be numeric");
}
return canParse;
}
private bool HeightNumericCheck(out double height)
{
// todo -> same pattern as above
}
private void button1_Click(object sender, EventArgs e)
{
double height = 0, waist = 0;
if( WaistNumericCheck(waist) && HeightNumericCheck(height) )
{
CheckDimensions(height, waist);
/* ... the rest of your code */
}
}
这会根据解析是否成功修改您的测试方法以返回true / false,并且out
是您的解析变量,这样您就不必再次解析它们了。
我建议您在某些时候查看MSDN文档:http://msdn.microsoft.com/en-us/library/994c0zb1%28v=vs.110%29.aspx