简体   繁体   English

循环求和

[英]Sum the odd numbers in a loop

public static void main(String[] args) {

    int maxNum, sum, counter; // input value
    Scanner input = new Scanner(System.in); 

    System.out.print("How many odd numbers should I add up?: ");
    maxNum = input.nextInt();   

    sum = 0;

    for (counter = 1; counter < maxNum; counter++)
    {            
       sum = counter + maxNum;                        
    }

    System.out.println("\n" + "The sum of the odd numbers between 1 and " + maxNum + " is: " + sum);                 
}

And according to that specific code, it's supposed to solve addition problems through odd numbers only. 并且根据该特定代码,它应该仅通过奇数来解决加法问题。

Now I tried it for one number, 5, and according to my output: 现在,根据输出,我尝试了一个数字5。

How many odd numbers should I add up?: 5

The sum of the odd numbers between 1 and 5 is: 9

It works. 有用。 But when I tried it out for another number, 10, something went wrong: 但是当我尝试另一个数字10时,出现了问题:

How many odd numbers should I add up?: 10

The sum of the odd numbers between 1 and 10 is: 19

I know my math problems, but odd numbers from 1 to 10 don't add up to 19, it adds up to 25. 我知道我的数学问题,但是从1到10的奇数加起来不会等于19,而是加起来等于25。

Something is wrong with the code. 代码出了点问题。 Can anyone figure out what went wrong? 谁能找出问题所在?

   for (counter = 1; counter < maxNum; counter+=2)
   {
      sum += counter;                                 
   }

You are adding the wrong number. 您输入的号码有误。

Your for loop is adding the wrong number and needs to skip every other number. 您的for循环添加了错误的数字,需要跳过其他所有数字。

for (counter = 1; counter <= maxNum; counter+=2)
{
   sum += counter;                                 
}

Output (if we add debug): 输出(如果我们添加调试):

counter = 1,sum = 1
counter = 3,sum = 4
counter = 5,sum = 9
counter = 7,sum = 16
counter = 9,sum = 25

Another way to do it is use a while loop: 另一种方法是使用while循环:

counter = 1;
while(counter < maxNum) 
{
   if (counter % 2 != 0) 
   {
      sum += counter;          
   }
   counter++;
}

Output (if we add debug): 输出(如果我们添加调试):

counter = 1,sum = 1
counter = 3,sum = 4
counter = 5,sum = 9
counter = 7,sum = 16
counter = 9,sum = 25

use this code for inside of loop. 将此代码用于循环内部。

if(counter%2==1){
    sum=sum+counter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Part_C_6
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Enter N");
            n = int.Parse(Console.ReadLine());

            Console.WriteLine(odd(n));

        }

        public static int odd(int x)
        {

            int j,ans=0;
            for (j = 1; j < x; j+=2)
            {
                ans = ans + j;
            }
            return ans;

        }

    }
}
public static void main(String[] args) {

int maxNum, sum, counter; // input value
Scanner input = new Scanner(System.in); 

System.out.print("How many odd numbers should I add up?: ");
maxNum = input.nextInt();   

sum = 0;

for (counter = 1; counter <= maxNum; counter+=2)

{

sum += counter;   

/* 
Working of loop
counter = 1,sum = 1
counter = 3,sum = 4
counter = 5,sum = 9
counter = 7,sum = 16
counter = 9,sum = 25                         
*/

}
System.out.println("\n" + "The sum of the odd numbers between 1 and " + maxNum + " is: " + sum);                 
}

In your for loop you increment counter by one 在for循环中,您将计数器增加1

if you need only the odd numbers you should change 如果只需要奇数,就应该更改

for (counter = 1; counter < maxNum; counter++)

to

for (counter = 1; counter <= maxNum; counter+=2)

and

sum = counter + maxNum;                                 

to

sum += counter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM