【问题标题】:Ask user to print out the amount of number they want c#要求用户打印出他们想要的数量 c#
【发布时间】:2018-12-03 20:19:50
【问题描述】:

我对如何从逻辑上思考我的代码有点问题。 我想要做的是让用户输入他们想要多少个数字,然后询问他们希望该数字序列从哪里开始。然后我会打印出数字。因此,如果用户输入 7,然后输入 4,结果将是 4 5 6 7 8 9 10。 到目前为止,这是我的代码

int userInInt, userIntStart;
Console.Write("How many integers do you want to print? ");
userInInt = Int32.Parse(Console.ReadLine());
Console.Write("What is the first integer you want printed? ");
userIntStart = Int32.Parse(Console.ReadLine());

for(int counts = userIntStart; userIntStart <= userInInt; userIntStart++)
{
    Console.WriteLine(userIntStart);
}

在执行此 for 循环后,我意识到它只会增加起始数字,直到 userInInt 这不是我想要的。我花了一段时间试图弄清楚我还需要什么。 谢谢

【问题讨论】:

  • 我认为你需要重新评估那个 for 循环。不要增加 userIntStart 变量。增加计数变量
  • 和Brian所说的一样,我认为你想要WriteLine的数字是counts,你应该在每次迭代后检查并增加counts - int counts = userIntStart; counts &lt;= userInInt + userIntStart; counts++;因为现在counts 未使用。否则,您可以完全省略第一条语句。
  • 但是如果我这样改变,结果不是 4 5 6 7 吗?我想尝试成为 4 5 6 7 8 9 10 例如,如果用户希望从 4 开始打印 7 个数字
  • 不,因为您会递增直到counts &lt;= userInInt + userIntStart。重要提示:+ userIntStart.
  • 哦,好吧,我想我明白为什么需要 +userIntStart 了。非常感谢!

标签: c# numbers user-input increment


【解决方案1】:

你给变量起的名字对于理解代码很重要,并且更容易思考。 userInInt 不反映变量的用途。

Console.Write("How many integers do you want to print? ");
int count = Int32.Parse(Console.ReadLine());
Console.Write("What is the first integer you want printed? ");
int start = Int32.Parse(Console.ReadLine());

i 通常被用作循环变量,因为在数学中它被用作索引。对于如何制定循环,您有不同的选择。最典型的是

for (int i = 0; i < count; i++)
{
   Console.WriteLine(start + i);
}

但您也可以将start 添加到循环变量的起始值和计数中。

for (int i = start; i < count + start; i++)
{
   Console.WriteLine(i);
}

您甚至可以增加多个变量:

for (int i = 0; i < count; i++, start++)
{
   Console.WriteLine(start);
}

【讨论】:

    【解决方案2】:

    如下更改你的 for 循环

    int userInInt, userIntStart;
    Console.Write("How many integers do you want to print? ");
    userInInt = Int32.Parse(Console.ReadLine());
    Console.Write("What is the first integer you want printed? ");
    userIntStart = Int32.Parse(Console.ReadLine());
    
         for(int counts = userIntStart; counts < userIntStart + userInInt; counts++)
         {
             Console.WriteLine(counts);
         }
    

    您的初始代码的问题是您的 for 循环是错误的,首先您应该将初始值分配给 counts,然后您应该在第二个参数中提供正确的退出条件,第三个参数是增量步骤,即 1,看看for循环语法here

    【讨论】:

      【解决方案3】:

      首先在您的代码中,您需要在增量步骤 (++) 中使用正确的变量名称。其次请注意,您需要使用单独的变量来跟踪整数的数量。就我而言,我为此使用了变量“i”。希望它会有所帮助。

           int userInInt, userIntStart;
              Console.Write("How many integers do you want to print? ");
              userInInt = Int32.Parse(Console.ReadLine());
              Console.Write("What is the first integer you want printed? ");
              userIntStart = Int32.Parse(Console.ReadLine());
              int i = 0;
      
              for (int counts = userIntStart; i<userInInt; counts++,i++)
              {
                  Console.WriteLine(counts);
              }
      
              Console.ReadLine();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-28
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 2015-07-06
        • 2013-10-06
        相关资源
        最近更新 更多