应用程序:当用户输入数据时,我正尝试将一种距离转换为另一种距离。但是,我输出的英寸到码和英寸到英尺的输出给出了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();
}
}
}
答案 0 :(得分:0)
将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();
}
答案 1 :(得分:0)
如评论中所述。如存储为int,分数将变为零。 使用此代码段。您将在结果中获得非零值
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();
}
}
}