簡體   English   中英

我的代碼有效,但我的程序無效,我該如何解決?

[英]My code works but my program doesn't, How can I fix this?

變量中有一部分不顯示:

decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);

現在,我相信我知道我已經導致文本框輸出是用戶插入的數字,而不是應該給出的結果。

我已經嘗試了多種方法來解決此問題,但沒有成功,是否有人可以幫助我進行編碼?

/*Distance Converter.
* In the English measurement system, 1 yard equals 3 feet and 1 foot equals 12 inches.
* Use this information to create an application that let's the user convert distances to and from inches, feet, and yards.
* The user enters the distance to be converted into a TextBox.
* A Listbox allows the user to select the units being converted from,
* and another ListBox allows the user to select the units being converted to.
* Note: Be sure to handle the situation where the user picks the asme units from both list boxes.
* The converted calue will be the same as the value entered. */

using System;
using System.Windows.Forms;

namespace _26DistanceConverter
{

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

      private void btExit_Click(object sender, EventArgs e)
      {
         Application.Exit();
      }

      private void btConvert_Click(object sender, EventArgs e)
      {
         //1ft = 12" - ft to inch 1"                        1/12ft - inch to ft
         //1 yard - 3 feet - yard to ft  1ft                1/3 yards - ft to yard
         //1 yard = 3 (ft) x 12" - yard to inch             1" = 1/ (3x12) - inch to yard

         const decimal inchToFoot = 1m / 12m;
         const decimal inchToYard = 1m / (3m * 12m);
         const decimal footToInch = 12m;
         const decimal footToYard = 1m/ 3m;
         const decimal yardToInch = 3m * 12m;
         const decimal yardToFoot = 3m;
         //tb Distance Covered equals txtInput
         decimal distanceToConvert = Convert.ToDecimal(tbDistanceCovered.Text);
         //tb Output = txtOutput

         ***decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);***

         string from = LstFrom.SelectedItem.ToString().ToUpper();
         string to = LstTo.SelectedItem.ToString().ToUpper();
         tbDistanceCovered.Text = Convert.ToString("n2" + tbOutput.Text);

         if (from == "Inches" && to == "Feet")
         {
            convertedDistance = distanceToConvert * inchToFoot;
         }
         else if (from == "Inches" && to == "Yards")
         {
            convertedDistance = distanceToConvert * inchToYard;
         }
         else if (from == "Feet" && to == "Inches")
         {
            convertedDistance = distanceToConvert * footToInch;
         }
         else if (from == "Feet" && to == "Yards")
         {
            convertedDistance = distanceToConvert * footToYard;
         }
         else if (from == "Yards" && to == "Inches")
         {
            convertedDistance = distanceToConvert * yardToInch;
         }
         else if (from == "Yards" && to == "Feet")
         {
            convertedDistance = distanceToConvert * yardToFoot;
         }
         else if (from == "Yards" && to == "Yards")
         {
            convertedDistance = distanceToConvert;
         }
         else if (from == "Inches" && to == "Inches")
         {
            convertedDistance = distanceToConvert;
         }
         else if (from == "Feet" && to == "Feet")
         {
            convertedDistance = distanceToConvert; //when using same units
         }
         else
            MessageBox.Show("Please enter a valid number", "Invalid Input");
      }

      private void btClear_Click(object sender, EventArgs e)
      {
         LstFrom.ClearSelected();
         LstTo.ClearSelected();
         tbOutput.Clear();
         tbDistanceCovered.Clear();
      }
   }
}

嘗試這個:

private void btConvert_Click(object sender, EventArgs e)
{
    // put this in the begining
    decimal convertedDistance = 0;

    // rest of your code will provide a value for convertedDistance 


    // and at the end put the value in output textbox with desired formatting
    tbOutput.Text = distanceToConvert.ToString("#.##");
}

還記得刪除這些行:

decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);

tbDistanceCovered.Text = Convert.ToString("n2" + tbOutput.Text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM