编写一个读取一组整数的程序,并打印偶数和奇数整数的总和

时间:2015-03-26 19:03:49

标签: java

这是我到目前为止所拥有的。我应该使用For循环和if/else语句编写此代码,但/我仍然坚持如何正确地执行此操作。如果有人可以告诉我如何正确使用For循环和if/else语句而不是给出答案,那将是很好的:

import java.util.*;

public class SumEvenOdd
{
    public static void main(String []args)
    {
        Scanner keyboard= new Scanner(System.in);

        int counter;
        int i= 0;
        int num=0;
        int sumOdd= 0;
        int sumEven= 0;

        System.out.println("Enter integers other then Zero: ");
        num=keyboard.nextInt();

        System.out.println("The numbers you entered are: ");

        for (i =num; i !=0; i=i)
                {
                    if (i % 2 == 0)
                        sumEven = sumEven + i;
                    else
                        sumOdd = sumOdd + i;
                    i = keyboard.nextInt();
                }
        System.out.println("Even sum: " + sumEven);
        System.out.println("Odd sum: " + sumOdd);
    }
}

4 个答案:

答案 0 :(得分:3)

你的循环永远不会执行,因为你的循环条件是假的,首先是:

for (i =num; i !=0; i=i) // i already equals 0 so i != 0 equates to false

你也不会使用i=i递增或递减,所以即使你的情况属实,你也会陷入无限循环。在for循环的每次迭代中,使用i++i的值增加1。

此外,您只需从用户那里接收一个号码。处理这种情况的一种简单方法是首先询问用户他们想先输入多少个数字,然后使用该输入循环多次要求数字相加。例如:

System.out.println("How many numbers do you want to enter? ");
num=keyboard.nextInt();

int[] addThese = new int[num]; // create an array of size num to store numbers


for(int i = 0; i < num; i++) {
    System.out.print(": ");
    addThese[i] = keyboard.nextInt();
}

// now use your for loop to iterate over addThese[] and find your sums
...

修改

你把自己(和我)与你的印刷陈述混淆了。你的程序运行正常,但我认为你并没有意识到这一点。

在循环中添加这样的内容,以便您知道它正在等待输入:

if (i % 2 == 0)
    sumEven = sumEven + i;
else
    sumOdd = sumOdd + i;

System.out.print(": ");  // <-- let the user know you're expecting more input   
i = keyboard.nextInt();

您可以使用我上面使用的数组来存储用户输入,这样您实际上可以告诉用户他们输入了什么数字。

答案 1 :(得分:1)

在你的应用程序中,只要不输入0,就不需要for循环,因为你打破了循环。

for循环用于迭代集合(对于每个循环)或迭代递增计数器,直到它满足中断(循环经典)。

do while(i!= 0)循环在您的场景中更合适。

答案 2 :(得分:1)

如果您希望在while循环中回答

import java.util.*;

/*
EXPLANATION
Question: write a program that reads a set of integers and tells the sum of the even and odd numbers
First: Initialize 4 variables(all integers)
Second: Take input and print title
Third: Take a while loop that will run when i is smaller than a
Fourth: take inputs of the numbers and check for condition od or even and add the numbers
Fifth: Break the while loop if input = 
*/

public class EvenOddSum
{
    public static void main(String[]args)
    {
        //initializing variables
        int InputNums = 0, OddNums = 0, EvenNums = 0, loopingVar = 0, PrintAmount;

        //initializing scanner class
        Scanner scanner = new Scanner(System.in);

        //Input using Scanner
        System.out.print("How many numbers you want to input: ");
        PrintAmount = scanner.nextInt();

        //Loop to execute if PrintAmount is bigger than or equal to The loop Variable
        while(loopingVar <= PrintAmount)
        {
            //increase Loop Variable by 1 if it is smaller than PrintAmount
            loopingVar++;

            //The input which will be sorted into odd or even
            System.out.print("Please input a number : ");
            InputNums = scanner.nextInt();

            //Conditional statements to Sort Input into Odd or even
            if (InputNums % 2 == 0)
            {
                //store input numbers into OddNums var if it is not divisible by 2
                OddNums = OddNums + InputNums;
            }
            else
            {
                //store input numbers into EvenNums var if it is divisible by 2
                EvenNums = EvenNums + InputNums;
            }

            if(loopingVar == PrintAmount)
            {
                //If the loop variable is equal to the print amount the break will end the loop
                break;
            }
        }

        //if the condition is true the sums are printed and the code is stopped
        if (loopingVar == PrintAmount)
        {
            System.out.println("Sum of even numbers is : " + OddNums);
            System.out.println("Sum of odd numbers is : " + EvenNums);
            System.exit(0);
        }

        //if InputNums is smaller than 0 there has been some error in the code
        if (InputNums < 0)
        {
            System.out.print("Invalid input");
            System.exit(0);
        }
    }
}

答案 3 :(得分:0)

    Scanner input = new Scanner(System.in);
    int num;
    int i;
    int x = 0;
    int y = 0;
    System.out.print("How many numbers you want to input: ");
    i = input.nextInt();
    for (;;) {
        i--;
        System.out.print("Please input a number : ");
        num = input.nextInt();
        if (num % 2 == 0) {
            x = x + num;
        } else {
            y = y + num;
        }
        if (num < 0) {
            System.out.print("Inivald input");
            System.exit(0);
        }
        if (i == 0) {
            System.out.println("Sum of even numbers is : " + x);
            System.out.println("Sum of odd numbers is : " + y);
            System.exit(0);
        }
    }