对数组中大于输入值的数字求和

需要将数组中大于输入数字的所有数字相加。种子只是为了可以复制输出。


例子:


[12,16,45,3,32]


输入值:30


输出:


77


import java.util.*;


    public class SumAbove {

        public static void main(String[] args) {

            Scanner scnr = new Scanner(System.in);

            int seed = scnr.nextInt();

            Random rand = new Random(seed);



            System.out.println("Enter a positive integer between 1-100 to search above:");

            int minVal = scnr.nextInt();


            int[] arr = new int[rand.nextInt(100)+1];

            for (int i=0; i<arr.length; i++) {

                arr[i] = rand.nextInt(100)+1;

            }

            System.out.println(Arrays.toString(arr));



             int sum = 0;

             for (int i=0; i<arr.length; i++) {

             if (arr[i]>minVal) {

                    sum += i;

                }

             }

            System.out.println(sum);

        }

    }


三国纷争
浏览 153回答 4
4回答

狐的传说

sum += i;您sum += arr[i];也只需要一个循环(因为您知道第一个循环之前的最小值),而不是您想要的(如前所述)。喜欢,int minVal = scnr.nextInt(), sum = 0;int[] arr = new int[rand.nextInt(100) + 1];for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; arr[i] = rand.nextInt(100) + 1;&nbsp; &nbsp; if (arr[i] > minVal) {&nbsp; &nbsp; &nbsp; &nbsp; sum += arr[i];&nbsp; &nbsp; }}System.out.println(Arrays.toString(arr));System.out.println(sum);

长风秋雁

sum += i用。。。来代替sum += arr[i]。变量i只是位置。arr[i]是该位置的值。

繁花如伊

public static void main(String[] args) {&nbsp; &nbsp; int nums[] = { 12, 16, 45, 3, 32 };&nbsp; &nbsp; int value;&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; System.out.println("Enter a positive integer between 1-100 to search above: ");&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; value = sc.nextInt();&nbsp; &nbsp; for (int i = 0; i < nums.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (nums[i] > value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = nums[i] + sum;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum);}

慕侠2389804

以下是如何在 Java 8+ 中使用流来实现这一点。&nbsp; &nbsp; &nbsp; int nValues = 5;&nbsp; &nbsp; &nbsp; int minValue = 1;&nbsp; &nbsp; &nbsp; int maxValue = 30;&nbsp; &nbsp; &nbsp; Random r = new Random();&nbsp; &nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int[] values = r.ints(nValues, minValue, maxValue + 1).toArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// min to sum is the threshold&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int minToSum = r.nextInt(7) + 10; // between 10 an 16 inclusive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int sum = Arrays.stream(values).filter(m -> m > minToSum).sum();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("sum = " + sum + " for greater than " + minToSum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ " : " + Arrays.toString(values));&nbsp; &nbsp; &nbsp; }以下输出。sum = 65 for values greater than 11 : [2, 10, 14, 23, 28]sum = 92 for values greater than 10 : [13, 18, 15, 19, 27]sum = 94 for values greater than 12 : [25, 6, 14, 25, 30]sum = 54 for values greater than 10 : [14, 8, 14, 26, 5]sum = 22 for values greater than 15 : [15, 8, 13, 22, 14]sum = 28 for values greater than 13 : [3, 28, 9, 6, 5]sum = 87 for values greater than 13 : [5, 18, 25, 21, 23]sum = 31 for values greater than 13 : [16, 7, 12, 2, 15]sum = 42 for values greater than 15 : [7, 22, 20, 10, 5]sum = 40 for values greater than 12 : [2, 2, 13, 27, 9]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java