gpt4 book ai didi

c# - 计算测量转换

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:17 29 4
gpt4 key购买 nike

我是 C# 的新手,正在上一门我很吃力的课。

作业是使用文本框值 (valuetextbox) 创建一个转换应用程序,并有 2 个列表框保存英寸、英尺和码的测量值。目标是根据第一个列表框 (selectedinitialunit) 中选择的测量单位转换值,并根据第二个列表框 (selectedconvertedunit) 中选择的测量单位在标签中发布值。我不知道如何根据在值文本框中输入的值在我的 selectedinitialunit 和我的 selectedconvertedunit 之间进行比较(数学)。

namespace Measurement_Conversion
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void convertedValueLabel_Click(object sender, EventArgs e)
{

}

private void exitButton_Click(object sender, EventArgs e)
{
Close();
}

private void resetButton_Click(object sender, EventArgs e)
{
valueTextBox.Text = "";
convertedValueLabel.Text = "";
DesiredUnitBox.SelectedIndex = -1;
InitialUnitBox.SelectedIndex = -1;
valueTextBox.Focus();
}

private void convertButton_Click(object sender, EventArgs e)
{

//Declare Variables
decimal initialvalue;
decimal convertedvalue;
string selectedInitialUnit;
string selectedConvertedUnit;



//Grab Initial Value
initialvalue = int.Parse(valueTextBox.Text);

//Determining Initial Unit has been selected.
if (InitialUnitBox.SelectedIndex != -1)
{
selectedInitialUnit = InitialUnitBox.SelectedItem.ToString();

}
else { MessageBox.Show("Please select an Initial Unit of measurement."); }


//Determining Desired Conversion Unit has been selected
if (DesiredUnitBox.SelectedIndex != -1)
{
selectedConvertedUnit = DesiredUnitBox.SelectedItem.ToString();
}
else { MessageBox.Show("Please select a desired conversion unit of measurement."); }

//Switch Statement
switch (selectedInitialUnit)
{
case "Inches":
convertedValueLabel.Text = ()
break;
case "Feet":
break;
case "Yards":
break;


}
}



}

我在纸上写的是这样说的——这不是代码,只是我的思维过程:

if selectedinitialunit = INCHES and selectedconvertedunit = INCHES
then messagebox.show("Please select a different selectedconvertedunit");

else selectedinitialunit = INCHES and selectedconvertedunit = FEET
then convertedvaluelabel.Text = initialvalue/12;

else selectedinitialunit = INCHES and selectedconvertedunit = YARDS
then convertedvaluelabel.Text = initialvalue/36;

其他选定的初始单位依此类推。

非常感谢您的帮助,我希望我没有在这里到处都是。

提前致谢!

最佳答案

你可以这样写代码——

        string selectedUnit = selectedinitialunit.SelectedItem.ToString();
string convertedUnit = selectedconvertedunit.SelectedItem.ToString();
double valueToConvert = double.Parse(initialvalue.Text);
if (selectedUnit.Equals(convertedUnit))
{
MessageBox.Show("Please select a different selectedconvertedunit");
}
else
{
switch (selectedUnit)
{
case "INCHES":
switch (convertedUnit)
{
case "FEET":
convertedvaluelabel.Text = (valueToConvert / 12.0).ToString();
break;
case "YARDS":
convertedvaluelabel.Text = (valueToConvert / 36.0).ToString();
break;
}
break;
}
}

您始终可以在代码中添加某种异常和错误处理以及验证。

关于c# - 计算测量转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35592546/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com