前100个偶数和奇数之和?
Sum of the first 100 even and odd number?
我需要一些帮助才能使此代码正常工作。我需要代码来计算前 100 个偶数和奇数。下面的代码显示了偶数部分,但我认为它们的工作方式相同,只是数字不同。我知道前100个偶数是2到200还是0到200?前100个奇数是1到199。(我还需要用while循环,我会用一个单独的while循环来确定代码如何计算总和?)
这是代码。
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
和往常一样,数学部分给我带来了麻烦。感觉自己的想法是对的,但是执行不出来
Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)
我需要一些帮助才能使此代码正常工作。我需要代码来计算前 100 个偶数和奇数。下面的代码显示了偶数部分,但我认为它们的工作方式相同,只是数字不同。我知道前100个偶数是2到200还是0到200?前100个奇数是1到199。(我还需要用while循环,我会用一个单独的while循环来确定代码如何计算总和?)
这是代码。
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
和往常一样,数学部分给我带来了麻烦。感觉自己的想法是对的,但是执行不出来
Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)