为什么我的代码在说我的构建成功时没有运行?

编程入门 行业动态 更新时间:2024-10-16 22:15:41
本文介绍了为什么我的代码在说我的构建成功时没有运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我是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.

推荐答案

你在那里安静了一些错误: You've made quiet a few errors there: 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(); }

更多推荐

为什么我的代码在说我的构建成功时没有运行?

本文发布于:2023-10-27 10:56:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!