【问题标题】:Logic error behind converting distance in C#在 C# 中转换距离背后的逻辑错误
【发布时间】:2018-03-25 01:13:01
【问题描述】:

应用程序:当用户输入数据时,我正在尝试将一种距离转换为另一种距离。但是,我的英寸到码和英寸到英尺的输出给我的逻辑错误为 0,而不是期望计算。英尺到码没有给我任何输出,码到码也在做同样的事情。我一直试图弄清楚 2 个小时,似乎无法理解。语法正确,逻辑与其他欲望输出相同。

我的代码:

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 Distance_Converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ConvertButton_click(object sender, EventArgs e)
        {
            int distance_to_convert;
            string lengthOption1;
            string lengthOption2;
            int feet = 12; 




            lengthOption1 = FromListBox.SelectedItem.ToString();

            distance_to_convert = int.Parse(distancetoconvertTextBox.Text);

            if ((FromListBox.SelectedIndex) != 1 && (ToListBox.SelectedIndex) != -1)

                lengthOption1 = FromListBox.SelectedItem.ToString();
                lengthOption2 = ToListBox.SelectedItem.ToString();
            {
                switch (lengthOption1)
                {
                    case "Inches":
                        if (lengthOption2 == "Inches")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            int inches_feet = distance_to_convert / feet;
                            //int feet = 12; 
                            ConvertedDistanceTextBox.Text = inches_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            int inches_yard = distance_to_convert / 36;
                            ConvertedDistanceTextBox.Text = inches_yard.ToString();
                        }
                        break;
                    case "Feet":
                        if (lengthOption2 == "Inches")
                        {
                            int feet_inches = distance_to_convert * 12;
                            ConvertedDistanceTextBox.Text = feet_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString(); ;
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            float  feet_yard = distance_to_convert / 3;
                            ConvertedDistanceTextBox.Text = feet_yard.ToString();
                        }
                        break;

                    case "Yards":
                        if (lengthOption2 == "Inches")
                        {
                            int Yards_inches = distance_to_convert * 36;
                            ConvertedDistanceTextBox.Text = Yards_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            int Yards_feet = distance_to_convert * 3;
                            ConvertedDistanceTextBox.Text = Yards_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString(); ;
                        }
                        break;
                }
            }

        }
        private void Exitbutton_click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

【问题讨论】:

  • 你在 distancetoconvertTextBox 中输入了什么值?
  • 它应该可以工作。在您的部门使用double 所以/ 36.0 而不是/ 36
  • 这行“ if ((FromListBox.SelectedIndex) != 1 && (ToListBox.SelectedIndex) != -1)” 应该都为 -1,还是你真的想拒绝 if FromListBox 的索引为 1?
  • @ForeverZer0- 好点,我删除了它,它仍然输出逻辑错误。这很奇怪,因为代码的其他部分有效。
  • @CodingYoshi 我试过了,但它仍然给我某些部分的逻辑错误。

标签: c# string if-statement logic


【解决方案1】:

将 int 作为存储结果的变量的数据类型会导致问题。如果将除法的结果存储在整数变量中,它只存储整数。

考虑将inches_yard 和其他变量的数据类型更改为float、decimal 或double。

                    else if (lengthOption2 == "Yards")
                    {
                        double inches_yard = distance_to_convert / 36;  //changed datatype from int to double here
                        x = inches_yard.ToString();
                    }

【讨论】:

  • G.更改该数据类型将表明对所有人进行更改。
  • 其实我已经知道怎么做了,但即使更改数据类型,它仍然给我同样的问题。
  • @ABerrio 我已经用相关代码更新了我的答案
  • G.这次真是万分感谢。我很感激!奇怪的是,我一直在改变一切,但我仍然遇到同样的问题。我做的和你展示的完全一样^^^。
  • 当你将一个整数 (distance_to_convert) 与另一个整数 (36) 相除时,结果将是整数部分(即使该值存储在双精度变量中)
【解决方案2】:

如 cmets 中所述。当您存储为 int 时,分数将为零。 使用此代码 sn-p。您将在结果中得到非零值

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 Distance_Converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ConvertButton_click(object sender, EventArgs e)
        {
            int distance_to_convert;
            string lengthOption1;
            string lengthOption2;
            int feet = 12; 
            lengthOption1 = FromListBox.SelectedItem.ToString();    
            distance_to_convert = int.Parse(distancetoconvertTextBox.Text);

            if ((FromListBox.SelectedIndex) != 1 && (ToListBox.SelectedIndex) != -1)

                lengthOption1 = FromListBox.SelectedItem.ToString();
                lengthOption2 = ToListBox.SelectedItem.ToString();
            {
                switch (lengthOption1)
                {
                    case "Inches":
                        if (lengthOption2 == "Inches")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            double inches_feet = (double)distance_to_convert / feet;
                            //int feet = 12; 
                            ConvertedDistanceTextBox.Text = inches_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            double inches_yard = (double)distance_to_convert / 36;
                            ConvertedDistanceTextBox.Text = inches_yard.ToString();
                        }
                        break;
                    case "Feet":
                        if (lengthOption2 == "Inches")
                        {
                            double feet_inches = (double)distance_to_convert * 12;
                            ConvertedDistanceTextBox.Text = feet_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString(); ;
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            float  feet_yard =(float) distance_to_convert / 3;
                            ConvertedDistanceTextBox.Text = feet_yard.ToString();
                        }
                        break;

                    case "Yards":
                        if (lengthOption2 == "Inches")
                        {
                            double Yards_inches = (double)distance_to_convert * 36;
                            ConvertedDistanceTextBox.Text = Yards_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            double Yards_feet = (double) distance_to_convert * 3;
                            ConvertedDistanceTextBox.Text = Yards_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            ConvertedDistanceTextBox.Text = distance_to_convert.ToString(); ;
                        }
                        break;
                }
            }

        }
        private void Exitbutton_click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

【讨论】:

  • 不需要在乘法上强制加倍。乘法可以正常工作。
  • @Hazarath。我做了这个确切的改变,但仍然有逻辑错误。我已经尝试了所有可能的方法,甚至更改为其他不同的数据类型,甚至输入了不同的数字。
  • @ABerrio 你有没有试过逐行调试,看看哪里漏掉了?
  • @Hararath Chillara。是的,我做到了。
  • 它在哪里为零..在输入级别或结果级别?
猜你喜欢
  • 1970-01-01
  • 2013-08-02
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多