显示一串文字的代码

问题描述:

我在大学的第一个学期,我们在做C#。我必须制作一个可以在摄氏温度和华氏温度之间转换的Winforms应用程序。我把所有的代码都放下了(基本上),但我需要将结果输出到标签。我可以用lblOutput = Output.ToString();将答案发送给标签,但我需要一条消息,如“[输入]摄氏度将会[输出]华氏度”。我尝试在“ToString”之后放入括号之间,但是出现错误。显示一串文字的代码

我除了这个之外还有其他一切代码。我一直在寻找最后几天想出来,但我找不到答案。任何帮助是极大的赞赏。

编辑:

我设置的代码出现在按钮的内部。我有两个单选按钮,表示放入文本框的值是远 - > Cels或Cels - >远。

这就是我在我的按钮编码。如果有什么方法可以改进它,请告诉我。

private void btnConvert_Click(object sender, EventArgs e) 
{ 
    decimal Input = Convert.ToDecimal(txtInput.Text); //grabs the input that the user entered 

    if (rbtnCelsius.Checked == true) //Test to see if the Celsius radio button is checked 
    { 
     decimal Output = ((Input - 32) * 5)/9; //If yes, it uses this formula to convert the input from Farenheit to Celsius 
     txtOutput.Text = Output.ToString(); //Outputs the message to the user, showing the Celsius end point 
    } 
    else //Says that the Celsius Radio Button is not checked, meaning that the Farenheit radio button is 
    { 
     decimal Output = (Input * 9)/5 + 32; //Moves onto this formula, converts Celsius to Farenheit 
     txtOutput.Text = Output.ToString(); //outputs the message to the user, showing the Farenheit end point 
    } 

    //txtOutput.Text = Output.ToString(); 
    //Commented out because it gives an error saying that "Output does not exist in current context" 
} 
+0

我假设“WFA”是一个“窗体窗体应用程序”? –

+0

是的。我在我的问题中声明了Windows窗体应用程序,并且我不想再次打开它。 –

您可以使用string.format替换字符串中的{0}或{1}这样的标记,也可以直接使用字符串连接。我想避免直接给你答案,因为你说这是作业,但如果你寻找这个c#的例子,你应该能找到自己的方式。

+0

这实际上是有帮助的。在编写控制台应用程序时,我之前对将多个变量添加到文本中有点困惑。在我编写看起来像“+直径+”的代码之前,显示一个值。看起来用{0}和{1}编写它然后在最后添加变量会更容易(我相信它就是这样工作的)。老实说,编程是我唯一挣扎的类。在这里发帖就像是我最后的解决方法(我已经和老师谈过这个问题了,但我还没有问这个问题) –

+0

编程对我来说也不是那么容易,而且我做了程序设计学位!我现在作为一名软件工程师在核工业工作,只是继续练习,你会到达那里:) –

int fahrenheit, celsius; 
// your code to set the two variables 
string yourLabelText = String.Format("{0} Celcius is {1} Fahrenheit", celsius.ToString(), fahrenheit.ToString()); 

yourLabel.Text = yourLabelText; 

Here is a reference for String.Format()。 (感谢Lukazoid!)

+0

这是MSDN参考'String.Format' http://msdn.microsoft.com/en-us/library /system.string.format.aspx – Lukazoid

+0

“yourLabelText”是我必须声明的东西吗?我有问题在表单中声明变量(它告诉我他们从未使用过)。我编辑了我的原文,以显示我正在编写的代码。 此外,@Lukazoid非常感谢您的支持,但它有一点不同,让代码从控制台和窗体显示文本。我习惯于编写控制台应用程序,所以我很难尝试让它在窗体中工作。 –

+0

@MattMiller'yourLabelText'在与它相同的行上声明。你可以把它写出来,只需使用'yourLabel.Text = String.Format(.....'。有意义吗? – 2011-11-09 01:30:49

我不确定你的问题是关于什么的,但是如果你需要将你的输出格式化为一个短语,你可以这样做。

private void btnConvert_Click(object sender, EventArgs e) 
{ 
    decimal Input = Convert.ToDecimal(txtInput.Text); //grabs the input that the user entered 
    // declare this outside the loop so you can use it later. 
    decimal output = 0M; 
    if (rbtnCelsius.Checked == true) //Test to see if the Celsius radio button is checked 
    { 
     output= ((Input - 32) * 5)/9; //If yes, it uses this formula to convert the input from Farenheit to Celsius 

    } 
    else //Says that the Celsius Radio Button is not checked, meaning that the Farenheit radio button is 
    { 
     output= (Input * 9)/5 + 32; //Moves onto this formula, converts Celsius to Farenheit 

    } 

    txtOutput.Text =string.Format("{0} Celsius will is {1} Fahrenheit",input,output); 
}` 

p.s.请记住使用try分析方法来确保txtInput完全可以转换为十进制。

+0

基本上,我必须让代码从文本框中读取一个输入,并且我有两个单选按钮,一个是从摄氏度到华氏度的公式选择,另一个是相反的。我认为最简单的方法是在IF语句中编码它,条件是检查转换为摄氏的单选按钮。 'txtOutput.Text = String.Format(...'必须同时显示这两个选项,这就是为什么我把它编码后我有我的计算。我必须切换Cel和Far的顺序取决于 –

如果你关心的是,但对于某些情况下,你可以通过调用Output.ToString(),使之更加人性化,你可以把它像这样

得到类似12.243333333我不知道
Output.ToString("F2") 

哪个会将其更改为12.24。你可以像一起使用string.Format像人们建议的那样。

+0

这是非常有用的,谢谢,我不喜欢如何转换36远的Cels是“2.222222222222222222”。我不知道我可以做到这一点。是否将“F2”放入括号内限制它到小数点后两位?这对我们知道很有帮助。 –

+2

是的,它是ToString方法的参数。还有很多其他的,你可以在MSDN上找到更多细节[链接](http://msdn.microsoft.com/ EN-US /库/ dwhawy9k.aspx) –