要求用户打印出他们想要的数字数量 c#
Ask user to print out the amount of number they want c#
我对如何从逻辑上思考我的代码有点问题。
我想要做的是让用户输入他们想要的数字数量,然后询问他们希望该数字序列从哪里开始。然后我会打印出数字。因此,如果用户输入 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 循环
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.
你给变量起的名字对于理解代码很重要,并且更容易思考。 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);
}
在您的代码中,您首先需要在增量步骤 (++) 中使用正确的变量名称。其次请注意,您需要使用单独的变量来跟踪整数的数量。就我而言,我为此使用变量 '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();
我对如何从逻辑上思考我的代码有点问题。 我想要做的是让用户输入他们想要的数字数量,然后询问他们希望该数字序列从哪里开始。然后我会打印出数字。因此,如果用户输入 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 循环
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.
你给变量起的名字对于理解代码很重要,并且更容易思考。 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);
}
在您的代码中,您首先需要在增量步骤 (++) 中使用正确的变量名称。其次请注意,您需要使用单独的变量来跟踪整数的数量。就我而言,我为此使用变量 '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();