我如何正确嵌套if else语句?为了总和?

时间:2015-02-17 21:09:26

标签: java

import java.util.Scanner;

public class DivisibleBy6or17 {
    public static void main(String[] args) {
        Scanner kbd = new Scanner (System.in);
        System.out.print("How many Values To Read ? ");
        int amount = kbd.nextInt();
        int[] values = new int[amount];
        int i = 0;
        double divisible = 0;
        int count = 0;
        while (i < amount) {
            System.out.print("Enter Value : ");
            int nbr = kbd.nextInt();
            if (nbr %6 == 0 || nbr % 17 == 0) {
                divisible += nbr;
                count++;
            }
            i++;
        }

        if (divisible == 0) {
            System.out.print("NONE DIVISIBLE");
        }
        else {
            System.out.print("Average: " + divisible/count);
        }
    }
}

所以我想要可以被16或17整除的数字的总和,那么我可以为总和插入一个if else然后为平均值做其他吗?我不知道在何处插入嵌套if语句的总和

1 个答案:

答案 0 :(得分:0)

System.out.println(divisible);将打印出总和,因为您只是将它们全部添加到该变量中。

请参阅https://stackoverflow.com/questions/28569527/how-would-i-manipulate-the-code-below-to-get-the-following-output/28570034#28570034

中的答案