打印从 1 到 100 的数字并用字符串替换一些数字 (fizzbuzz)

发布于 2024-10-31 20:43:41 字数 1191 浏览 4 评论 0原文

我正在从这里尝试 fizzbuzz 程序: 为什么程序员不能.. 编程?< /a>

"编写一个程序,打印从 1 到 100 的数字。但是对于三的倍数,打印“Fizz”而不是数字;对于五的倍数,打印“Buzz”。对于同时是三和五的倍数的数字打印“菲兹巴兹”。”

protected void btn1_Click(object sender, EventArgs e)
    {
        for (int i = 1; i < 101; i++)
        {
            if (i % 3 == 0 & i % 5 == 0)
            {
                Response.Write("fizzbuzz" + ",");
            }
            else if (i % 3 == 0)
            {
                Response.Write("fizz" + ",");
            }
            else if (i % 5 == 0)
            {
                Response.Write("buzz" + ",");
            }
            else
            {
                i = i + 0;
            }

            Response.Write(i +",");
        }

    }

我能够产生某种结果,例如:

1,2,嘶嘶声,3,4,嗡嗡声,5,嘶嘶声,6,7,8,嘶嘶声,9,嗡嗡声,10,11,嘶嘶声,12,13,14,嘶嘶声,15,16,17 ,嘶嘶声,18,19,嗡嗡声,20,嘶嘶声,21,22,23,嘶嘶声,24,嗡嗡声,25,26,嘶嘶声,27,28,29,嘶嘶声,30,31,32,嘶嘶声,33,34 、嗡嗡声、35、嘶嘶声、36、37、38、嘶嘶声、39 等等..

打印了 fizz 一词,但它没有取代 3,并且打印了 fizzbuzz,但它没有替换 15 等等...

I am trying the fizzbuzz program from here: Why Can't Programmers.. Program?

"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."

protected void btn1_Click(object sender, EventArgs e)
    {
        for (int i = 1; i < 101; i++)
        {
            if (i % 3 == 0 & i % 5 == 0)
            {
                Response.Write("fizzbuzz" + ",");
            }
            else if (i % 3 == 0)
            {
                Response.Write("fizz" + ",");
            }
            else if (i % 5 == 0)
            {
                Response.Write("buzz" + ",");
            }
            else
            {
                i = i + 0;
            }

            Response.Write(i +",");
        }

    }

I am able to produce some kind of result like:

1,2,fizz,3,4,buzz,5,fizz,6,7,8,fizz,9,buzz,10,11,fizz,12,13,14,fizzbuzz,15,16,17,fizz,18,19,buzz,20,fizz,21,22,23,fizz,24,buzz,25,26,fizz,27,28,29,fizzbuzz,30,31,32,fizz,33,34,buzz,35,fizz,36,37,38,fizz,39, and so on..

The word fizz was printed but it did not replace 3 and fizzbuzz was printed but it did not replace 15 and so ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

潦草背影 2024-11-07 20:43:41

无论您是否满足 if 条件,您仍然会在代码末尾打印 i

特别看看你的 for 循环:

for (int i = 1; i < 101; i++)         
{             
if (i % 3 == 0 & i % 5 == 0)             
{                 
Response.Write("fizzbuzz" + ",");             
}             
else if (i % 3 == 0)             
{                 
Response.Write("fizz" + ",");             
}             
else if (i % 5 == 0)             
{                 
Response.Write("buzz" + ",");             
}             
else             
{                 
i = i + 0;             
}              
Response.Write(i +",");   //look here you print i
} 

所以你需要移动最后一个 Response.Write(i + ",");在最后一个 else 条件中。查找此类错误的最简单方法是使用调试器并调试程序。然后您将很容易看到输出是什么。因此,一定要使用调试器并设置断点/监视并观察会发生什么。您的代码应更改为:

  for (int i = 1; i < 101; i++)         
    {             
    if (i % 3 == 0 & i % 5 == 0)             
    {                 
    Response.Write("fizzbuzz" + ",");             
    }             
    else if (i % 3 == 0)             
    {                 
    Response.Write("fizz" + ",");             
    }             
    else if (i % 5 == 0)             
    {                 
    Response.Write("buzz" + ",");             
    }             
    else             
    {                 
    Response.Write(i +",");   //look here you print i
    }              
    } 

请注意,您的 for 循环已通过增加 i 来处理此问题,从而删除了 i=i+1

编辑

Not sure if this is easier but here is another way to do this using lambda's:

            List<int> t;
            t = Enumerable.Range(1, 100).ToList();

            var fizzBuzz = t.Where(num => num % 3 == 0 && num % 5 == 0);
            var fizz = t.Where(num => num % 3 == 0);
            var buzz = t.Where(num => num % 5 == 0);
            var notFizzBuzz = t.Where(num => num % 3 != 0 && num % 5 !=0);

            //print fizzBuzz elements
            Console.WriteLine("Printing fizzBuzz elements...");
            foreach (int i in fizzBuzz)
                Console.WriteLine(i);

            //print fizz elements
            Console.WriteLine("Printing fizz elements...");
            foreach (int i in fizz)
                Console.WriteLine(i);

            //print buzz elements
            Console.WriteLine("Printing buzz elements...");
            foreach (int i in buzz)
                Console.WriteLine(i);

            //print other elements
            Console.WriteLine("Printing all others...");
            foreach (int i in notFizzBuzz)
                Console.WriteLine(i);

Whether you hit the if condition or not you are still printing i at the end of your code.

Look specifically at your for loop:

for (int i = 1; i < 101; i++)         
{             
if (i % 3 == 0 & i % 5 == 0)             
{                 
Response.Write("fizzbuzz" + ",");             
}             
else if (i % 3 == 0)             
{                 
Response.Write("fizz" + ",");             
}             
else if (i % 5 == 0)             
{                 
Response.Write("buzz" + ",");             
}             
else             
{                 
i = i + 0;             
}              
Response.Write(i +",");   //look here you print i
} 

So you need to move that last Response.Write(i + ","); in the last else condition. The easiest way to find bugs like these is to use the debugger and debug your program. You will then easily see what the output is. So definately use the debugger and set breakpoints / watches and watch what happens. Your code should change to this:

  for (int i = 1; i < 101; i++)         
    {             
    if (i % 3 == 0 & i % 5 == 0)             
    {                 
    Response.Write("fizzbuzz" + ",");             
    }             
    else if (i % 3 == 0)             
    {                 
    Response.Write("fizz" + ",");             
    }             
    else if (i % 5 == 0)             
    {                 
    Response.Write("buzz" + ",");             
    }             
    else             
    {                 
    Response.Write(i +",");   //look here you print i
    }              
    } 

Notice the removal of i=i+1 your for loop is handling this already by incrementing i.

Edit

Not sure if this is easier but here is another way to do this using lambda's:

            List<int> t;
            t = Enumerable.Range(1, 100).ToList();

            var fizzBuzz = t.Where(num => num % 3 == 0 && num % 5 == 0);
            var fizz = t.Where(num => num % 3 == 0);
            var buzz = t.Where(num => num % 5 == 0);
            var notFizzBuzz = t.Where(num => num % 3 != 0 && num % 5 !=0);

            //print fizzBuzz elements
            Console.WriteLine("Printing fizzBuzz elements...");
            foreach (int i in fizzBuzz)
                Console.WriteLine(i);

            //print fizz elements
            Console.WriteLine("Printing fizz elements...");
            foreach (int i in fizz)
                Console.WriteLine(i);

            //print buzz elements
            Console.WriteLine("Printing buzz elements...");
            foreach (int i in buzz)
                Console.WriteLine(i);

            //print other elements
            Console.WriteLine("Printing all others...");
            foreach (int i in notFizzBuzz)
                Console.WriteLine(i);
春风十里 2024-11-07 20:43:41

尝试这些更改

protected void btn1_Click(object sender, EventArgs e)
{
    for (int i = 1; i < 101; i++)
    {
        if (i % 3 == 0 & i % 5 == 0)
        {
            Response.Write("fizzbuzz" + ",");
        }
        else if (i % 3 == 0)
        {
            Response.Write("fizz" + ",");
        }
        else if (i % 5 == 0)
        {
            Response.Write("buzz" + ",");
        }
        else
        {
            Response.Write(i +",");
        }           
    }
}

您的 i = i + 0 显然没有执行任何操作,因为您将 0 添加到 i 的值。

而且无论 if/else 块的结果如何(放在它后面),您都会将数字打印到响应中,因此应该将其移至 else 中(意味着仅在 if 或 else if 不匹配时才打印。

Try these changes

protected void btn1_Click(object sender, EventArgs e)
{
    for (int i = 1; i < 101; i++)
    {
        if (i % 3 == 0 & i % 5 == 0)
        {
            Response.Write("fizzbuzz" + ",");
        }
        else if (i % 3 == 0)
        {
            Response.Write("fizz" + ",");
        }
        else if (i % 5 == 0)
        {
            Response.Write("buzz" + ",");
        }
        else
        {
            Response.Write(i +",");
        }           
    }
}

Your i = i + 0 obviously does nothing, since you are adding 0 to the value of i.

And you are printing the number to the response regardless of the result of the if/else block (it's put after it), so it should be moved into else (meaning only print if the if, or else if did not match.

谁的年少不轻狂 2024-11-07 20:43:41

将 Response.Write(i +","); 移动到最后的 else 中

move Response.Write(i +","); into your final else

迟到的我 2024-11-07 20:43:41
protected void btn1_Click(object sender, EventArgs e)
{
    for (int i = 1; i < 101; i++)
    {
        if (i % 3 == 0 & i % 5 == 0)
        {
            Response.Write("fizzbuzz" + ",");
        }
        else if (i % 3 == 0)
        {
            Response.Write("fizz" + ",");
        }
        else if (i % 5 == 0)
        {
            Response.Write("buzz" + ",");
        }
        else
        {
            i = i + 0; //this is totally useless
            Response.Write(i + ",");

        }

        //Response.Write(i +","); //This will always write the number, even if you wrote fizz or buzz
    }

}
protected void btn1_Click(object sender, EventArgs e)
{
    for (int i = 1; i < 101; i++)
    {
        if (i % 3 == 0 & i % 5 == 0)
        {
            Response.Write("fizzbuzz" + ",");
        }
        else if (i % 3 == 0)
        {
            Response.Write("fizz" + ",");
        }
        else if (i % 5 == 0)
        {
            Response.Write("buzz" + ",");
        }
        else
        {
            i = i + 0; //this is totally useless
            Response.Write(i + ",");

        }

        //Response.Write(i +","); //This will always write the number, even if you wrote fizz or buzz
    }

}
你曾走过我的故事 2024-11-07 20:43:41

它的另一个简单实现:

for (int i = 1; i <= 100; i++)
        {
            Console.WriteLine((i % 3 == 0) ? ((i % 5 == 0) ? "FizzBuzz" : "Fizz") : ((i % 5 == 0) ? "Buzz" : i.ToString()));
        }
Console.ReadKey();

Another simple implementation of it:

for (int i = 1; i <= 100; i++)
        {
            Console.WriteLine((i % 3 == 0) ? ((i % 5 == 0) ? "FizzBuzz" : "Fizz") : ((i % 5 == 0) ? "Buzz" : i.ToString()));
        }
Console.ReadKey();
樱&纷飞 2024-11-07 20:43:41
    public static void PrintMod3And5FromInterval(int start, int end)
    {
        if (end < start)
        {
            Console.WriteLine("End number should be higher than start.");
        }
        else
        { 
            string result = "";
            for (int x = start; x <= end; x++)
            {
                if (x % 3 == 0)
                    result += "fizz";
                if (x % 5 == 0)
                    result += "buzz";
                if (result == "")
                    Console.WriteLine(x);
                else
                    Console.WriteLine(result);
                result = "";
            }
        }
    }
    static void Main(string[] args)
    {
        PrintMod3And5FromInterval(1, 100);
        Console.Read();
    }
    public static void PrintMod3And5FromInterval(int start, int end)
    {
        if (end < start)
        {
            Console.WriteLine("End number should be higher than start.");
        }
        else
        { 
            string result = "";
            for (int x = start; x <= end; x++)
            {
                if (x % 3 == 0)
                    result += "fizz";
                if (x % 5 == 0)
                    result += "buzz";
                if (result == "")
                    Console.WriteLine(x);
                else
                    Console.WriteLine(result);
                result = "";
            }
        }
    }
    static void Main(string[] args)
    {
        PrintMod3And5FromInterval(1, 100);
        Console.Read();
    }
我喜欢麦丽素 2024-11-07 20:43:41

这是我最初的解决方案...

for (let number = 1; number <= 100; number ++) {
if (number % 3 === 0 && number % 5 === 0) {
    console.log(number + "fizzbuzz");
} else if (number % 5 === 0) {
console.log(number + "buzz");
} else if (number % 3 === 0)
console.log(number + "fizz");
else  {
console.log(number);
}
}     

但这一个要短得多...

for (let n = 1; n <= 100; n++) {
  let output = "";
    if (n % 3 == 0) output += "Fizz";
    if (n % 5 == 0) output += "Buzz";
  console.log(output || n);
}

Here was my original solution...

for (let number = 1; number <= 100; number ++) {
if (number % 3 === 0 && number % 5 === 0) {
    console.log(number + "fizzbuzz");
} else if (number % 5 === 0) {
console.log(number + "buzz");
} else if (number % 3 === 0)
console.log(number + "fizz");
else  {
console.log(number);
}
}     

But this one is much shorter...

for (let n = 1; n <= 100; n++) {
  let output = "";
    if (n % 3 == 0) output += "Fizz";
    if (n % 5 == 0) output += "Buzz";
  console.log(output || n);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文