public static void Main()
{
EvenNumbers();
}
public static void EvenNumbers()
{
int written = int.Parse(Console.ReadLine());
for (int i = 0; i <= written; i++)
{
if (i % 2 == 1)
continue;
Console.WriteLine(i);
}
}
}
}
Yes I'm aware I've declared the code in a different method from the main method despite with a program this simple there being no need to its just for simple practice. I'm just wondering as you can see in the code it detects the input of the user and then from that input uses a for loop and counts up to it evenly. However, I want it to count up to it by odds if I put an odd number in. How could I do this?
是的我知道我已经使用与main方法不同的方法声明了代码,尽管有一个程序这么简单,不需要它只是为了简单的练习。我只是想知道你可以在代码中看到它检测到用户的输入,然后从该输入使用for循环并均匀计数。但是,如果我输入一个奇数,我希望它能以赔率计算。我怎么能这样做?
3 个解决方案
#1
0
Check for i % 2 != written % 2
.
检查i%2!=写入%2。
#2
0
To print odd or even numbers, the line if (i % 2 == x)
needs to have x=1
to print even numbers and x=0
to print odd numbers. If you take written % 2
you will get 0
if it is even and 1
if it is odd. Since you want to print even numbers if the input number is even and odd numbers if the input is odd, you store a number in the variable modNumber
that you will then use in place of x
in if (i % 2 == x)
. The line int modNumber = written % 2 == 1 ? 0 : 1
in English means "The int modNumber equals 0 if written % 2 == 1, otherwise it equals 1".
要打印奇数或偶数,行if(i%2 == x)需要x = 1来打印偶数而x = 0才能打印奇数。如果你写了%2,如果是偶数则得0,如果是奇数则得1。如果输入数字为偶数,则要打印偶数,如果输入为奇数,则需要输入奇数数字,然后在变量modNumber中存储一个数字,然后在if(i%2 == x)中使用该数字代替x。行int modNumber =写入%2 == 1?英语中的0:1表示“如果写入%2 == 1,则int modNumber等于0,否则等于1”。
public static void EvenNumbers()
{
int written = int.Parse(Console.ReadLine());
int modNumber = written % 2 == 1 ? 0 : 1;
for (int i = 0; i <= written; i++)
{
if (i % 2 == modNumber)
continue;
Console.WriteLine(i);
}
}
#3
0
You just need to count by twos. That will work for either even or odd numbers. For odd numbers, you need to start at 1, and for even numbers, you need to start at 0. You can easily figure out which you need by doing a single mod
operation. x % 2
will be 1 for odd numbers, and zero for even numbers.
你只需要两个人数。这适用于偶数或奇数。对于奇数,您需要从1开始,对于偶数,您需要从0开始。您可以通过执行单个mod操作轻松找出所需的数字。对于奇数,x%2将为1,对于偶数,x%2将为零。
public static void Count()
{
int written = int.Parse(Console.ReadLine());
for (int i = written % 2; i <= written; i += 2)
{
Console.WriteLine(i);
}
}
#1
0
Check for i % 2 != written % 2
.
检查i%2!=写入%2。
#2
0
To print odd or even numbers, the line if (i % 2 == x)
needs to have x=1
to print even numbers and x=0
to print odd numbers. If you take written % 2
you will get 0
if it is even and 1
if it is odd. Since you want to print even numbers if the input number is even and odd numbers if the input is odd, you store a number in the variable modNumber
that you will then use in place of x
in if (i % 2 == x)
. The line int modNumber = written % 2 == 1 ? 0 : 1
in English means "The int modNumber equals 0 if written % 2 == 1, otherwise it equals 1".
要打印奇数或偶数,行if(i%2 == x)需要x = 1来打印偶数而x = 0才能打印奇数。如果你写了%2,如果是偶数则得0,如果是奇数则得1。如果输入数字为偶数,则要打印偶数,如果输入为奇数,则需要输入奇数数字,然后在变量modNumber中存储一个数字,然后在if(i%2 == x)中使用该数字代替x。行int modNumber =写入%2 == 1?英语中的0:1表示“如果写入%2 == 1,则int modNumber等于0,否则等于1”。
public static void EvenNumbers()
{
int written = int.Parse(Console.ReadLine());
int modNumber = written % 2 == 1 ? 0 : 1;
for (int i = 0; i <= written; i++)
{
if (i % 2 == modNumber)
continue;
Console.WriteLine(i);
}
}
#3
0
You just need to count by twos. That will work for either even or odd numbers. For odd numbers, you need to start at 1, and for even numbers, you need to start at 0. You can easily figure out which you need by doing a single mod
operation. x % 2
will be 1 for odd numbers, and zero for even numbers.
你只需要两个人数。这适用于偶数或奇数。对于奇数,您需要从1开始,对于偶数,您需要从0开始。您可以通过执行单个mod操作轻松找出所需的数字。对于奇数,x%2将为1,对于偶数,x%2将为零。
public static void Count()
{
int written = int.Parse(Console.ReadLine());
for (int i = written % 2; i <= written; i += 2)
{
Console.WriteLine(i);
}
}