Output sum of even numbers between two integers

删除回忆录丶 提交于 2020-01-04 06:52:08

问题


I am working on a simple JAVA question in one of my college courses. I am stumped on this one program. I will display what I have so far and give the question I have to answer. I also looked at a similar question on StackOverflow, BUT it isn't the same problem so it DIDN'T help. The program I need to write is:

Write a program that uses 'while' loops to perform the following steps:

a.) Prompt the user to input two integers: 'firstNum' and 'secondNum' (firstNum must be less than secondNum)

b.) Output all the odd numbers between 'firstNum' and 'secondNum' inclusive.

c.) Output the sum of all the even numbers between 'firstNum' and 'secondNum' inclusive.

This is what I have so far... (I still need to calculate the even numbers and sum them up)

//import classes
import java.util.*;

public class chapter5no9
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {

    //Part A
    int firstNum;
    int secondNum;
    int sumEven;

    System.out.println("Please enter an integer: ");
    firstNum = console.nextInt();

    System.out.println("Please enter another integer less than the first integer: ");
    secondNum = console.nextInt();

    //Part B
    if (firstNum < secondNum)
    {
        System.out.print("Your second number is greater than the first.  So Please re-enter: ");
        secondNum = console.nextInt();
    }
    else
    {
        System.out.print("Odd Numbers: ");
        firstNum++;
        while (firstNum > secondNum)
        {
            if (secondNum % 2 != 0)
            {
                System.out.print(" " + secondNum);
            }
            secondNum++;
        }

        System.out.println();
        System.out.print("Sum of Even Numbers: ");
        firstNum++;
        while (firstNum > secondNum)
        {
            if (secondNum % 2 != 0)
            {
                System.out.print(" " + secondNum);
            }
            secondNum++;
        }
    }
}

}


回答1:


I created the loopCounter variable to handle the required iterations without changing the values inputted by the user. The following changes have been made to your code.

Part A: added a while loop to validate user input. Also changed logic in if statement.

Part B: used one loop to print odd numbers and total even numbers

   //Part A
   int firstNum;
   int secondNum;
   int sumEven=0;

   System.out.println("Please enter an integer: ");
   firstNum = input.nextInt();

   System.out.println("Please enter another integer less than the first integer: ");
   secondNum = input.nextInt();

   //Part B

   //validate input in a loop
   while(true)
   {
     if (firstNum > secondNum)
     {
         System.out.print("Your second number is larger than the first.  So Please re-enter: ");
         secondNum = input.nextInt();
     }
     else
     {
         break;
     }
   }

       System.out.print("Odd Numbers: ");
       int loopCounter=firstNum;
       while(loopCounter<secondNum)
       {
         if (loopCounter%2!=0)
         {
             System.out.print(" " + loopCounter);
         }//end if
         else
         {
             sumEven+=loopCounter;
         }//end else
         loopCounter++;
       }

       System.out.println();
       System.out.print("Sum of Even Numbers: ");    
       System.out.print(sumEven);

   }



回答2:


I would separate the two concerns of checking input and calculating the result. Here's how I would calculate it:

int sum = IntStream.rangeClosed(firstNum, secondNum).filter(i -> i % 2 == 0).sum();



回答3:


The issue is in your logic. You have this if statement:

if (firstNum < secondNum)
{
    System.out.print("Your second number is greater than the first.  So Please re-enter: ");
    secondNum = console.nextInt();
}

Which checks if the second number is greater than the first (which you want it to be) and then asks them to re-enter. You want to check if (secondNum < firstNum). You will need to reverse all your while and if statements that compare secondNum to firstNum.

Then you have this if (secondNum % 2 != 0) to check for odd numbers, which is correct, but you copy-pasted it to check for even numbers, which won't work, you will need to change that as well.

Then, you are outputting all the even integers inside your loop, when really you want to be adding it to an evenSum variable, and outputting that at the end of the loop:

System.out.println("The sum of all even integers is: " + evenSum);

That should be enough to help you, I'm not going to write your homework for you, that's not what we do here.




回答4:


package hello.world;
import java.util.Scanner;

public class HelloWorld {


    public static void main(String[] args) {

      Scanner sc = new Scanner(System.in); 
      int number ;
      int X = 0;
      System.out.print("enter a number plz : ");
      number = sc.nextInt();
      while (X <= number){  
        System.out.println(X);
        X ++;
      }  
    }
}


来源:https://stackoverflow.com/questions/33313482/output-sum-of-even-numbers-between-two-integers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!