问题描述
大家好,我是C#的新手。
我不知道为什么我的代码不想运行。
一旦我开始运行我的代码,cmd提示符上没有任何内容。
它只是显示一个空白屏幕。
我尝试过:
我必须编写一个C#控制台程序,将温度以摄氏度转换为华氏度。
显示-40摄氏度到+40摄氏度的所有值,步长为5度。
这是我到目前为止所得到的:
Hi guys, I'm new to C#.
I'm not sure why my code doesn't want to run.
Once I start to run my code, nothing comes up on the cmd prompt.
It just shows a blank screen.
What I have tried:
I have to write a C# console program that converts temperature in degrees Celsius to degrees Fahrenheit.
Showing all values from –40 degrees Celsius to +40 degrees Celsius in steps of 5 degrees.
This is what I got so far:
<pre>
using System;
namespace TemperatureConverter
{
class Program
{
static void Main()
{
int c, f;
for (c = -40; c <= 40; c =+ 5);
f = 9 / 5 * c + 32;
Console.WriteLine("c: {0}; f: {1}", c, f);
}
}
}
我不知道我的代码是否正确。
非常感谢你的帮助。
I don't know if my code is right or not.
Your assistance is much appreciated.
推荐答案
for (c = -40; c <= 40; c =+ 5);
末尾的分号将终止循环 - 所以它永远不会做任何事情
在体内。删除分号。
增量运算符也是错误的:
The semicolon at the end will terminate the loop - so it will never do anything
inside the body. Remove the semicolon.
And the increment operator is wrong as well:
c=+ 5
表示将c设置为5,而不是将5添加到c。你可能想要
means "set c to 5", not "add 5 to c". You probably want
c += 5
for (c = -40; c <= 40; c =+ 5);
f = 9 / 5 * c + 32;
Console.WriteLine("c: {0}; f: {1}", c, f);
如果你想在你的循环中有多个语句,你需要一个复合语句 - 这是一种说法把大括号括在它们周围的奇特方式。
9和5是整数,所以9/5也是一个整数:1。你需要浮点值:9.0和5.0而不是。
还有另外一个问题:如果你修复了这个并运行调试器中的这段代码,你可能看不到任何东西,因为命令提示符会很快打开,打印和关闭。最后添加
If you want more than one statement inside your loop, you need a compound statement - which is a fancy way of saying "put curly brackets round them".
9 and 5 are integers, so 9 / 5 is also an integer: 1. You need floating point values: 9.0 and 5.0 instead.
There's one other problem as well: if you fix this and run this code in the debugger, you probably won't see anything as the command prompt will open, print and shut really quickly. Add
Console.ReadLine();
,它将暂停直到您按ENTER键。
试试这个:
at the end, and it will pause until you press ENTER.
Try this:
static void Main()
{
float c, f;
for (c = -40; c <= 40; c += 5)
{
f = 9.0 / 5.0 * c + 32;
Console.WriteLine("c: {0}; f: {1}", c, f);
}
Console.ReadLine();
}
这篇关于为什么我的代码在说我的构建成功时没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!