使用循环和控制结构显示所有提出的奇数和偶数的总和

Display the sum of all raised odd and even number using loop and control structures

我需要创建一个 java 程序,它将接受 10 个整数,如果输入的数字是奇数,则将其提高到 ^ 1 次方,如果用户输入另一个奇数,则再次提高到下一次方 ^ 2,与偶数相同,但功率从 10 开始,并且会在每个输入的偶数中减少。之后,我必须显示所有提出的偶数和奇数的总和。

困难的是不允许使用数组,java.util.Math。

例如:

程序要求用户输入 10 个整数:

用户输入(整数 1-10):

1 ^ 1 = 1(奇数)

2^10=1024(偶数)

3^2=9(奇数)

4^9=262114(偶数)

5 ^ 3 = 125(奇数)

6^8=1679616(偶数)

7 ^ 4 = 2 401(奇数)

8 ^ 7 = 2 097 152(偶数)

9^5=59049(奇数)

10 ^ 6 = 1 000 000(偶数)

输出: 偶数总和

奇次幂的总和

谢谢 Devon,我尝试 运行 这个程序,但我有逻辑错误。

public class SumOfOddEvenMain {

public static void main(String[] args) {
    SumOfOddEven temp = new SumOfOddEven();
    System.out.println(temp.Run());
}

}

import java.util.Scanner;

public class SumOfOddEven {
private long sumOfOdds;
private long sumOfEvens;
private int countOfOdds;
private int countOfEvens;

public SumOfOddEven() {
    countOfOdds = 1;
    countOfEvens = 10;
    sumOfOdds = 0;
    sumOfEvens = 0;
}
public String Run() {
    Scanner in = new Scanner(System.in);

    for(int i = 0; i < 10; i++){
        System.out.println("Enter integer number " + i);
        int number = in.nextInt();

        if(number % 2 == 0) {
            for(int j = countOfEvens; j > 1; j--) {
                number *= number;
            }

            sumOfEvens += number;
            countOfEvens--;
        }
        else {
            for(int k = countOfOdds; k > 1; k--) {
                number *= number;
            }

            sumOfOdds += number;
            countOfOdds++;
        }
    }

    in.close();
    return "Sum of odds: " + sumOfOdds + "\nSum of evens: " + sumOfEvens;
}

}

程序输出:

Enter integer number 0
1
Enter integer number 1
2
Enter integer number 2
3
Enter integer number 3
4
Enter integer number 4
5
Enter integer number 5
6
Enter integer number 6
7
Enter integer number 7
8
Enter integer number 8
9
Enter integer number 9
10
Sum of odds: -495568963
Sum of evens: 0
public class SumOfOddEven {
    private long sumOfOdds;
    private long sumOfEvens;
    private int countOfOdds;
    private int countOfEvens;

    public SumOfOddEven() {
        countOfOdds = 1;
        countOfEvens = 10;
        sumOfOdds = 0;
        sumOfEvens = 0;
    }
    public String Run() {
        Scanner in = new Scanner(System.in);

        for(int i = 0; i < 10; i++){
            System.out.println("Enter integer number " + i);
            long number = in.nextLong();

            if(number % 2 == 0) {
                for(int j = countOfEvens; j >= 1; j--) {
                    number *= number;
                }

                sumOfEvens += number; 
                countOfEvens--;
            }
            else {
                for(int k = countOfOdds; k >= 1; k--) {
                    number *= number;
                }

                sumOfOdds += number;
                countOfOdds++;
            }
        }

        in.close();
        return "Sum of odds: " + sumOfOdds + "\nSum of evens: " + sumOfEvens; 
    }

}

然后主要是:

public static void main(String[] args) {
    SumOfOddEven temp = new SumOfOddEven();
    System.out.println(temp.Run());
}

您可以通过简单的条件检查以确保输入有效,以查看输入是否为整数或作业要求检查的任何其他规定。如果该条件为真,则只需让 运行() 再次调用自身并重置计数变量。

您可以按照以下方式进行:

import java.util.Scanner;

public class SumOfOddEven {    
    public static void main(String[] args) {
        int sumOddInts = 0, sumEvenInts = 0, countOddInts = 1, countEvenInts = 10;
        Scanner in = new Scanner(System.in);
        for (int i = 1; i <= 10; i++) {
            System.out.println("Count: " + i + ", enter an integer (from 1 to 10): ");
            int number;
            do {
                number = in.nextInt();
                if (!(number >= 0 && number <= 10)) {
                    System.out.println("Try again, enter an integer (from 1 to 10): ");
                } else {
                    if (number % 2 == 0) {
                        sumEvenInts += power(number, countEvenInts--);
                    } else {
                        sumOddInts += power(number, countOddInts++);
                    }
                }
            } while (!(number >= 0 && number <= 10));
        }
        in.close();
        System.out.println("The sum of even powered: " + sumEvenInts);
        System.out.println("The sum of odd powered: " + sumOddInts);
    }

    static int power(int n, int power) {
        int num = 1;
        for (int i = 1; i <= power; i++) {
            num *= n;
        }
        return num;
    }
}

注意:上面给出的程序限制了数字的幂次,按要求限制在1到10之间,避免整数溢出

样本运行:

Count: 1, enter an integer (from 1 to 10): 
12
Try again, enter an integer (from 1 to 10): 
3
Count: 2, enter an integer (from 1 to 10): 
5
Count: 3, enter an integer (from 1 to 10): 
6
Count: 4, enter an integer (from 1 to 10): 
7
Count: 5, enter an integer (from 1 to 10): 
8
Count: 6, enter an integer (from 1 to 10): 
2
Count: 7, enter an integer (from 1 to 10): 
1
Count: 8, enter an integer (from 1 to 10): 
9
Count: 9, enter an integer (from 1 to 10): 
10
Count: 10, enter an integer (from 1 to 10): 
4
The sum of even powered: 204688256
The sum of odd powered: 59421