带有 IF 语句的嵌套循环 C#
试图解决一个简单的c#问题,但我做了,但我的老师希望我用一个nt依的循环而不是用布尔人来做。
问题:从输入号码中显示所有可除以3和7的数字。如果找不到数字,则显示“找不到数字”。
我如何用bool解决它:
Console.WriteLine("Put in a number: ");
int nummer = int.Parse(Console.ReadLine());
bool FoundLegitNumber = false;
for (int i = 1; i <= nummer; i++)
{
if (i % 3 == 0 && i % 7 == 0)
{
FoundLegitNumber = true;
Console.WriteLine($"The number {i} is evenly divisible by 3 and 7");
}
}
if (!FoundLegitNumber)
{
Console.WriteLine("Didnt find any number...");
}
我试图用一个循环解决它:
Console.WriteLine("Put in a number: ");
int nummer = int.Parse(Console.ReadLine());
for (int i = 0; i <= nummer; i++)
{
for (int j = 0; i >= j; j++)
{
if (i % 3 == 0 && i % 7 == 0)
{
Console.WriteLine($"The number {i} is evenly divisible by 3 and 7");
}
else if (j == 0)
{
Console.WriteLine("Didnt find any number...");
break;
}
}
}
我知道,这没有任何意义,但我不知道如何解决它。我知道如果(i%3 == 0&amp; i%7 == 0)是不正确的,我想给出J + 1。
Trying to solve a simple c# question and I did but my teacher wants me to do it with a nestled loop instead of with a boolean.
Problem: Show all numbers from input number that is divisible by 3 and 7. If no number is found show "No number found".
How i solved it with bool:
Console.WriteLine("Put in a number: ");
int nummer = int.Parse(Console.ReadLine());
bool FoundLegitNumber = false;
for (int i = 1; i <= nummer; i++)
{
if (i % 3 == 0 && i % 7 == 0)
{
FoundLegitNumber = true;
Console.WriteLine(quot;The number {i} is evenly divisible by 3 and 7");
}
}
if (!FoundLegitNumber)
{
Console.WriteLine("Didnt find any number...");
}
How I'm trying to solve it with a for loop:
Console.WriteLine("Put in a number: ");
int nummer = int.Parse(Console.ReadLine());
for (int i = 0; i <= nummer; i++)
{
for (int j = 0; i >= j; j++)
{
if (i % 3 == 0 && i % 7 == 0)
{
Console.WriteLine(quot;The number {i} is evenly divisible by 3 and 7");
}
else if (j == 0)
{
Console.WriteLine("Didnt find any number...");
break;
}
}
}
I know, it doesnt make any sense but I cant figure out how to solve it. I know that I want to give J + 1 if (i % 3 == 0 && i % 7 == 0) was not true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要一种涉及嵌套的方法而不会扭曲基本问题,也许您可以添加这样的分隔符数组:
If you want a method that involves a nested
for
without distorting the basic problem, maybe you could add a dividers array like this: