[英]How do I inserting a loop in an while, if-else statement in C#?
我正在嘗試完成一種我的作業,這應該要求一個小數。 如果寫的話。 而不是,文本“你需要用(,)瑞典標准寫下你的十進制數”。
在這里,我希望程序循環回來,因此用戶必須重試,直到用(,)輸入十進制數。
在此之后,程序將詢問您要將其舍入到的小數位數。 但是目前我的程序只是輸出了你需要使用的文本,根據瑞典語標准FOLLOWED by text“你想要將它舍入到多少小數”,如果錯誤則不會循環第一個語句。
我目前嘗試改變一些事情,但它經常導致程序崩潰。 我目前使用“別破;” 在地方和代碼沒有完成但功能。 完成我的任務需要更多代碼,但目前仍然存在循環問題
static void Main(string[] args)
{
double value1;
int round1;
while (true)
{
Console.Write("Enter a decimal-number: ");
string StrValue = Console.ReadLine();
bool success = Double.TryParse(StrValue, out value1);
if (!success)
{
Console.WriteLine("You need to use (,) as decimal-sign according to Swedish standard!"); //From here I want a loop if . is used instead of ,
}
Console.Write("With how many decimal-numbers do you want to round it down to?: ");
string StrRound = Console.ReadLine();
bool success1 = Int32.TryParse(StrRound, out round1);
if (!success1)
{
Console.WriteLine("Only use whole numbers when rounding");
}
else break;
}
}
}
}
我希望程序在第一個語句之后循環,但我無法弄清楚它為什么沒有,以及我應該如何解決這個問題!
用while循環替換if條件,試試這個:
static void Main(string[] args)
{
double value1;
int round1;
while (true)
{
Console.Write("Enter a decimal-number: ");
string StrValue = Console.ReadLine();
bool success = Double.TryParse(StrValue, out value1);
while (!success)
{
Console.WriteLine("You need to use (,) as decimal-sign according to Swedish standard!"); //From here I want a loop if . is used instead of ,
StrValue = Console.ReadLine();
success = Double.TryParse(StrValue, out value1);
}
Console.Write("With how many decimal-numbers do you want to round it down to?: ");
string StrRound = Console.ReadLine();
bool success1 = Int32.TryParse(StrRound, out round1);
if (!success1)
{
Console.WriteLine("Only use whole numbers when rounding");
}
else break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.