每次循环重复打印不同的控制台。

发布于 2025-02-02 20:21:08 字数 1165 浏览 2 评论 0原文

一切都很好。如果用户输入多个重复第一个循环的问题,我希望这个问题说“请输入2号”,等等。是否可以在相同的console.writeline中执行此操作?

using System;

namespace Looping
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many numbers will you provide?");
            int numbersProvide = Convert.ToInt32(Console.ReadLine());

            int i;

            for (i = 0;  i < numbersProvide; i++)
            {
                Console.WriteLine("Please enter number:");
                int enterNum = Convert.ToInt32(Console.ReadLine());

                int j;
                int sum = 0;
          
                double addDiv;

                for (j = 1; j <= 25; j++)
                {
                    if (enterNum % j == 0)
                    {
                       addDiv = enterNum / j;
                       Console.WriteLine(enterNum + " is divisible by " + j + "(" + addDiv + ")");
                        sum += j;
                    }
                }
                Console.WriteLine("The sum of the quotient is: " + sum);
            }
            Console.ReadKey();
        }
    }
}

Everything runs perfectly. If a user enters more than one to repeat the first for loop I want the question to say "Please enter number 2" and so forth. Is there way to do this within the same Console.WriteLine?

using System;

namespace Looping
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many numbers will you provide?");
            int numbersProvide = Convert.ToInt32(Console.ReadLine());

            int i;

            for (i = 0;  i < numbersProvide; i++)
            {
                Console.WriteLine("Please enter number:");
                int enterNum = Convert.ToInt32(Console.ReadLine());

                int j;
                int sum = 0;
          
                double addDiv;

                for (j = 1; j <= 25; j++)
                {
                    if (enterNum % j == 0)
                    {
                       addDiv = enterNum / j;
                       Console.WriteLine(enterNum + " is divisible by " + j + "(" + addDiv + ")");
                        sum += j;
                    }
                }
                Console.WriteLine("The sum of the quotient is: " + sum);
            }
            Console.ReadKey();
        }
    }
}

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

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

发布评论

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

评论(1

魔法唧唧 2025-02-09 20:21:08

只需使用循环的索引变量i

Console.WriteLine($"Please enter number {i + 1} :");

Just use the index variable i of your loop

Console.WriteLine(
quot;Please enter number {i + 1} :");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文