【问题标题】:Stuck with receiving user input and then turning it into an Int value坚持接收用户输入,然后将其转换为 Int 值
【发布时间】:2023-03-22 06:09:01
【问题描述】:

所以为了上课,我必须用 C# 制作一个小 BMI 计算器,但我遇到了一些麻烦。

所以我有 2 个文本框,weightTxt 用于体重,heightTxt 用于身高,但我似乎无法获得用户输入的值,我可以将其用于 If-else 语句。

这是我将字符串转换为 int 所做的:

int weight = Int32.Parse(weightTxt.Text);
int height = Int32.Parse(heightTxt.Text);

在我看来,我认为这应该将每个框中的任何文本转换为 int 值,不是吗?

另外,附带问题,在 If 语句中为什么我不能用 Int 做这样的事情

if (weight >= 300 || weight <= 10)
{
    MessageBox.Show("Input not acceptable");
}

非常感谢,是的,我很菜鸟,请帮助我:)

编辑:这就是我的整个 .cs 的样子

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {


        int weight = Int32.TryParse(weightTxt.Text);
        int height = Int32.TryParse(heightTxt.Text);

        if (weight >= 300)
        {
            MessageBox.Show("Input not acceptable");
        }
        else
        {
            //do nothing?
        }

    }
}

}

【问题讨论】:

  • 您的代码看起来不错。也许它在用户输入之前就被调用了。
  • 解析weightTxt和heightTxt时是否收到错误?
  • 这段代码什么时候执行?点击按钮还是什么?
  • 您必须提供更多代码才能获得有用的答案。听起来像是一个范围界定问题
  • '这应该将每个框中的任何文本转换为 int 值',错误,如果不是数字字符串(它包含非数字字符),将尝试将其转换为整数会抛出异常。

标签: c# string parsing int


【解决方案1】:

这就是它可以完成的方式,使用完全验证。

class Form1 : Form
{
    public void OnButton1_Click(object sender, EventArgs e)
    {
        CalculateAndDisplayBMI();
    }

    private void CalculateAndDisplayBMI()
    {
        int weight = 0;
        int height = 0;

        if(!TryGetWeight(out weight))
        {
            MessageBox.Show("Invalid value for weight.")
            return;
        }

        if (!TryGetHeight(out height))
        {
            MessageBox.Show("Invalid value for height.")
            return;
        }

        double bmi = CalculateBmi(weight, height);
        bmiTxt.Text = $"{bmi}";
    }

    private bool TryGetWeight(out int weight)
    {
        if (!int.TryParse(weightTxt.Text, out weight))
            return false;

        if (weight <= 10 || weight >= 300) // kgs
            return false;
    }

    private bool TryGetHeight(out int height)
    {
        if (!int.TryParse(heightTxt.Text, out height))
            return false;

        if (height <= 100 || height >= 250) // cms
            return false;
    }

    private double CalculateBmi(int height, int weight)
    {
        double heightInMeters = height / 100d;
        return weight / (heightInMeters * heightInMeters);
    }
}

【讨论】:

    【解决方案2】:

    解析正确!:

    int weight = Int32.Parse(weightTxt.Text);
    int height = Int32.Parse(heightTxt.Text);
    

    你可以这样做:

    if (weight >= 300 || weight <= 10)
    {
        MessageBox.Show("Input not acceptable");
    }
    

    除非变量weight在另一个方法中被声明为局部变量!例如:

    void method1()
    {
        int weight = Int32.Parse(weightTxt.Text);
        int height = Int32.Parse(heightTxt.Text);
    } 
    

    您将无法在method2 中使用它,因为它不存在于此范围内:

    void method2()
    {
        if (weight >= 300 || weight <= 10)
        {
            MessageBox.Show("Input not acceptable");
        }
    }
    

    解决这个问题的方法是在类级别声明变量!

    class Form1 : Form
    {
    
        int weight;
        int height;
    

    并通过类使用变量。但请注意不要再次声明它们!

        void method1()
        {
            weight = Int32.Parse(weightTxt.Text);
            height = Int32.Parse(heightTxt.Text);
        } 
    
        void method2()
        {
            if (weight >= 300 || weight <= 10)
            {
                MessageBox.Show("Input not acceptable");
            }
        }
    

    【讨论】:

      【解决方案3】:

      唯一的错误似乎是使用了 TryParse。 请查看方法的签名:

      int weight;
      Int32.TryParse(weightTxt.Text,out weight);
      int height;
      Int32.TryParse(heightTxt.Text,out height);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多