带有 IF 语句的嵌套循环 C#

发布于 2025-01-19 05:15:38 字数 1116 浏览 0 评论 0原文

试图解决一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-01-26 05:15:38

如果您需要一种涉及嵌套的方法而不会扭曲基本问题,也许您可​​以添加这样的分隔符数组:

int number = 21;
int[] dividers = new int[] { 3, 7 };

for (int i = 0; i <= number; i++)
{
    bool matchAllDividers = true;
    for (int j = 0; j < dividers.Length; j++)
    {
        if (i % dividers[j] != 0)
        {
            matchAllDividers = false;
            break;
        }
    }

    if (matchAllDividers)
    {
        Console.WriteLine("The number {0} is evenly divisible by all dividers".FormatWith(i));
    }
    else
    {
        Console.WriteLine("The number {0} is not evenly divisible by all dividers".FormatWith(i));
    }
}

If you want a method that involves a nested for without distorting the basic problem, maybe you could add a dividers array like this:

int number = 21;
int[] dividers = new int[] { 3, 7 };

for (int i = 0; i <= number; i++)
{
    bool matchAllDividers = true;
    for (int j = 0; j < dividers.Length; j++)
    {
        if (i % dividers[j] != 0)
        {
            matchAllDividers = false;
            break;
        }
    }

    if (matchAllDividers)
    {
        Console.WriteLine("The number {0} is evenly divisible by all dividers".FormatWith(i));
    }
    else
    {
        Console.WriteLine("The number {0} is not evenly divisible by all dividers".FormatWith(i));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文