简体   繁体   English

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

[英]Summing Numbers in an Array Greater than an Inputted Value

Need to add all numbers in an array that is greater than an inputted number.需要将数组中大于输入数字的所有数字相加。 The seed is just so the output can be replicated.种子就是这样可以复制 output。

Example:例子:

[12,16,45,3,32] [12,16,45,3,32]

Inputted Value: 30输入值:30

Output: Output:

77 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);
        }
    }

Instead of sum += i;而不是sum += i; you want sum += arr[i];你想要sum += arr[i]; (as already noted), you also only need one loop (since you know the minimum before the first loop). (如前所述),您还只需要一个循环(因为您知道第一个循环之前的最小值)。 Like,喜欢,

int minVal = scnr.nextInt(), sum = 0;
int[] arr = new int[rand.nextInt(100) + 1];
for (int i = 0; i < arr.length; i++) {
    arr[i] = rand.nextInt(100) + 1;
    if (arr[i] > minVal) {
        sum += arr[i];
    }
}
System.out.println(Arrays.toString(arr));
System.out.println(sum);

Replace sum += i with sum += arr[i] .sum += i替换为sum += arr[i]

The variable i is just the position.变量i只是 position。 arr[i] is the value at that position. arr[i]是 position 处的值。

public static void main(String[] args) {

    int nums[] = { 12, 16, 45, 3, 32 };
    int value;
    int sum = 0;

    System.out.println("Enter a positive integer between 1-100 to search above: ");
    Scanner sc = new Scanner(System.in);
    value = sc.nextInt();

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > value)
            sum = nums[i] + sum;
    }
    System.out.println(sum);
}

Here is how you can do it with streams in Java 8+.以下是 Java 8+ 中的流的方法。

      int nValues = 5;
      int minValue = 1;
      int maxValue = 30;
      Random r = new Random();
      for (int i = 0; i < 10; i++) {
         int[] values = r.ints(nValues, minValue, maxValue + 1).toArray();

         // min to sum is the threshold
         int minToSum = r.nextInt(7) + 10; // between 10 an 16 inclusive


         int sum = Arrays.stream(values).filter(m -> m > minToSum).sum();

         System.out.println("sum = " + sum + " for greater than " + minToSum
               + " : " + Arrays.toString(values));
      }

The following output.以下 output。

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]


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在忽略所有大于第一个值的数字时,如何在数组中找到max int? - How to find max int in an array, while ignoring all numbers greater than first value? 找出数组中数字的平均值,找到大于平均值的数字 - Find the average of numbers in an array and find the numbers greater than the average Java递归:确定数组中有多少个数字大于索引1 - Java Recursion: Determine how many numbers in an array is greater than index 1 如何处理大于Double.MAX_VALUE的数字 - How to handle numbers greater than Double.MAX_VALUE 返回和求和数组的奇数 - Returning and Summing the Odd Numbers of an Array 检查数组值是否大于用户输入值的方法 - Method that checks if an array value is greater than a user-inputed value 如何读取浮点值并打印小于或大于该值的最接近的数字? - How do I read a floating point value and print the closest numbers less than or greater than that value? 如果元素值大于10,是否可以在数组中乘以元素? - Is it possible to Multiply elements in the array if the elements value is greater than 10? 在O(1)中找到数组值大于x的第一个索引 - find first index in array value greater than x in O(1) 数组中的值(非索引)大于3会导致java.lang.ArrayIndexOutOfBoundsException - Value (not index) greater than 3 in array causes java.lang.ArrayIndexOutOfBoundsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM