“循环”无法在C#中工作(“Loop” not working in C#)

编程入门 行业动态 更新时间:2024-10-17 02:54:18
“循环”无法在C#中工作(“Loop” not working in C#)

感谢社区,我能够编写我的第一个程序。 但是,我仍然需要一些帮助来改进程序:

Console.WriteLine("BMI Rechner"); Console.WriteLine("==========="); Console.WriteLine(); Console.Write("Körpergewicht in kg: "); int kg; kg = Convert.ToInt32(Console.ReadLine()); Console.Write("Größe in cm: "); int cm; cm = Convert.ToInt32(Console.ReadLine()); Console.Write("Geschlecht (m/w):"); string Geschlecht = Console.ReadLine(); bool Auswahl = false; switch(Geschlecht) { case "m": Auswahl = true; break; case "w": Auswahl = true; break; default: Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); break; } if (Auswahl != false) {Console.WriteLine("Eingabe wird verarbeitet");} double BMI = kg / ( (cm / 100.0) * (cm / 100.0) ); if (BMI < 19 & Geschlecht == "w") { Console.WriteLine("-> Untergewicht"); } else if (BMI >= 19 & BMI <= 24 & Geschlecht == "w") { Console.WriteLine("-> Normalgewicht"); } else if (BMI > 24 & Geschlecht == "w") { Console.WriteLine("-> Übergewicht"); } if (BMI < 20 & Geschlecht == "m") { Console.WriteLine("-> Untergewicht"); } else if (BMI >= 20 & BMI <= 25 & Geschlecht == "m") { Console.WriteLine("-> Normalgewicht"); } else if (BMI > 25 & Geschlecht == "m") { Console.WriteLine("-> Übergewicht"); } Console.ReadLine();

我在中间做了一个开关,以防止程序在输入错误的情况下崩溃。 它工作,我得到这些线:

Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich");

这应该是我的“循环”回到选项“m”和“w”。 但是,如果我现在输入m / w,程序就会关闭,这意味着它根本不起作用。

我的代码中是否有错误,或者我只是使用了错误的命令?

我为编程术语的命令道歉。 这是我第一次。

提前致谢!

thanks to the community here I was able to write my first program. However, I still need some help to improve the program:

Console.WriteLine("BMI Rechner"); Console.WriteLine("==========="); Console.WriteLine(); Console.Write("Körpergewicht in kg: "); int kg; kg = Convert.ToInt32(Console.ReadLine()); Console.Write("Größe in cm: "); int cm; cm = Convert.ToInt32(Console.ReadLine()); Console.Write("Geschlecht (m/w):"); string Geschlecht = Console.ReadLine(); bool Auswahl = false; switch(Geschlecht) { case "m": Auswahl = true; break; case "w": Auswahl = true; break; default: Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); break; } if (Auswahl != false) {Console.WriteLine("Eingabe wird verarbeitet");} double BMI = kg / ( (cm / 100.0) * (cm / 100.0) ); if (BMI < 19 & Geschlecht == "w") { Console.WriteLine("-> Untergewicht"); } else if (BMI >= 19 & BMI <= 24 & Geschlecht == "w") { Console.WriteLine("-> Normalgewicht"); } else if (BMI > 24 & Geschlecht == "w") { Console.WriteLine("-> Übergewicht"); } if (BMI < 20 & Geschlecht == "m") { Console.WriteLine("-> Untergewicht"); } else if (BMI >= 20 & BMI <= 25 & Geschlecht == "m") { Console.WriteLine("-> Normalgewicht"); } else if (BMI > 25 & Geschlecht == "m") { Console.WriteLine("-> Übergewicht"); } Console.ReadLine();

I made a switch thingie in the middle to prevent the program from crashing if a wrong input is given. It works and I get these lines:

Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich");

which is supposed to be my "loop" back to the options "m" and "w". However, if I enter m/w now, the program just closes, meaning that it doesn't work at all.

Is there an error in my code or am I just using the wrong command?

I apologize for my command of the programming lingo. This is my first time.

Thanks in advance!

最满意答案

说实话,我没有在你的代码中看到任何循环。

您可以使用do...while循环执行您所说的内容:

do { //... }while (Geschlecht != "m" && Geschlecht != "w");

do...while循环将在每次迭代之后检查(不是之前,意味着它将始终至少在循环中输入一次)。 在这种情况下,条件说只要Geschlecht不是“m”并且不是“w”就会循环。

将此应用于您的代码,您将获得:

//... bool Auswahl = false; string Geschlecht; do { Console.Write("Geschlecht (m/w):"); Geschlecht = Console.ReadLine(); switch(Geschlecht) { case "m": Auswahl = true; break; case "w": Auswahl = true; break; default: Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); break; } }while (Geschlecht != "m" && Geschlecht != "w"); if (Auswahl != false) {Console.WriteLine("Eingabe wird verarbeitet");} //etc...

请注意我已经取出了两个变量,即Auswahl和Geschlecht 。 原因是因为两者都需要在循环之外可用,特别是条件中需要Geschlecht 。


吉米 评论说,有可能以不再需要Auswahl方式这样做......这是一种方法(与其他任何一样有效):

//... Console.Write("Geschlecht (m/w):"); string Geschlecht = Console.ReadLine(); while (Geschlecht != "m" && Geschlecht != "w"); { Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); Console.Write("Geschlecht (m/w):"); Geschlecht = Console.ReadLine(); } Console.WriteLine("Eingabe wird verarbeitet"); //etc...

在这种情况下,我们有一个while循环,在循环内的代码中只有在满足条件时才会执行(也就是说,在每次迭代之前验证条件)。

您可能还会考虑为Geschlecht提供不同的数据类型,因为它只有“m”和“w”的有效值...但我离题了。


我想建议在dotnetperls上输入C#Loops Constructs作为C#中不同类型循环的介绍。

To be honest I don't see any loop in your code.

You could do what you say using a do...while loop as such:

do { //... }while (Geschlecht != "m" && Geschlecht != "w");

The do...while loop will check after the each iteration (not before, meaning it will always enter in the loop at least once). In this case the condition says that it will loop as long as Geschlecht is not "m" and is not "w".

Applying this to your code, you will get:

//... bool Auswahl = false; string Geschlecht; do { Console.Write("Geschlecht (m/w):"); Geschlecht = Console.ReadLine(); switch(Geschlecht) { case "m": Auswahl = true; break; case "w": Auswahl = true; break; default: Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); break; } }while (Geschlecht != "m" && Geschlecht != "w"); if (Auswahl != false) {Console.WriteLine("Eingabe wird verarbeitet");} //etc...

Please notice I have taken two variable out of the loop, those are Auswahl and Geschlecht. The reason is because both are needed to be available outside of the loop, in particular Geschlecht is needed in the conditional.


Jimmy comments that it is possible to do this in such way that Auswahl is no longer needed... here is one way to do it (as valid as any other):

//... Console.Write("Geschlecht (m/w):"); string Geschlecht = Console.ReadLine(); while (Geschlecht != "m" && Geschlecht != "w"); { Console.WriteLine("Ungültige Eingabe"); Console.WriteLine("(m)ännlich/(w)eiblich"); Console.Write("Geschlecht (m/w):"); Geschlecht = Console.ReadLine(); } Console.WriteLine("Eingabe wird verarbeitet"); //etc...

In this case we have a while loop, in code inside the loop will only execute if the condition is met (that is, the condition is verified before each iteration).

You may also consider to have a different data type for Geschlecht since the only valid values it has are "m" and "w"... but I digress.


I want to suggest the entry C# Loops Constructs at dotnetperls as an introducion to the different kinds of loops in C#.

更多推荐

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

发布评论

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

>www.elefans.com

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