简体   繁体   English

无法从20个随机生成的数字中打印最大的数组数字

[英]Having trouble in printing the largest array number from the 20 randomly generated numbers

Ok so I been working on this assignment all day for the past 3 days but I haven't had any luck. 好吧,过去三天我整天都在从事这项工作,但是我没有任何运气。 I wasn't going to ask for help but I finally gave up. 我本来不想寻求帮助,但我最终放弃了。 But there is also one more thing I need to implement to the code. 但是我还需要对代码实施另一件事。 This is what I gotta implement "Find the length of the longest continuous series of positive numbers in the array data. If the contents were: 4 5 0 2 . . . -1 88 78 66 -6. The length would be 3. For this problem, 0 is considered non-negative but not positive". 这就是我要实现的“查找数组数据中最长的连续正数序列的长度。如果内容为:4 5 0 2 ... -1 88 78 66 -6。长度为3。对于这个问题,“ 0被认为是非负但不是正”。 Plus I have an issue where I can't print the largest int in the array of 20. 另外,我有一个问题,我无法打印20个数组中最大的int。

import java.util.Random;
import java.util.ArrayList;
public class arrayops {

public static int findLargest(ArrayList<Integer> nums) {
      int greatestnum = nums.get(0);
      for (Integer item : nums) {
          if (item > greatestnum) {
              greatestnum = item;
          }
      }
      return greatestnum; 
  }
  public static int randomData(ArrayList<Integer> nums) {
      int[] array = new int [20];
      Random random = new Random();
      for (int i = 0; i < array.length; i++) {
          array[i] = -100 + random.nextInt(201);
      }
      return -100 + random.nextInt(201);
  }

  public static void main(String[] args) {
      ArrayList<Integer> nums = new ArrayList<Integer>();
      nums.add(1);
      nums.add(4);
      nums.add(13);
      nums.add(43);
      nums.add(-25);
      nums.add(17);
      nums.add(22);
      nums.add(-37);
      nums.add(29);
      System.out.println("The Greatest Number from the hardcoded numbers " + findLargest(nums));
      System.out.println("The Greatest number from the random numbers " + randomData(nums));
     }
    } 

The findLargest method: findLargest方法:

public static int findLargest(ArrayList<Integer> nums) {
  int greatestnum = 0;
  int greatestLen = 0;
  for (Integer item : nums) {
      if (item > 0) {
          greatestLen++ ;
          if(greatestLen > greatestnum)
              greatestnum = greatestLen;
      }
      else
          greatestLen = 0;
  }
  return greatestnum; 
}

Logic used: 使用的逻辑:

  1. Keep the length of the longest chain encountered, and the length of current chain, in two separate variables (greatestnum and greatestLen respectively) 将遇到的最长链的长度和当前链的长度保存在两个单独的变量中(分别为greatestnum和estestLen)

  2. Increment greatestLen every time a positive number is encountered. 每次遇到正数时,将maximumLen递增。 If the number if less than or equal to zero, reset this count. 如果数字小于或等于零,请重置此计数。

  3. If the length of current chain is greater than the previous longest chain, sent the longest chain size to current chain size. 如果当前链的长度大于先前的最长​​链,则将最长链的大小发送到当前链的大小。

The problem is you created a list with random numbers but never put that list into the findLargest method. 问题是您创建了一个带有随机数的列表,但从未将该列表放入findLargest方法中。 You also never created a method to find the consecutive positive numbers. 您也从未创建过查找连续正数的方法。 If you didn't know how to go about coding it, I recommend drawing out an algorithm on paper. 如果您不知道如何编码,建议您在纸上画出一种算法。

Largest value in ArrayList... ArrayList中的最大值...

public static int findL(ArrayList<Integer> nums)
{
    int top = nums.get(0);
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i)>top)
        {
            top = nums.get(i);
        }
    }
    return top;
}

Largest number of consecutive positives... 连续正数最多...

public static int positiveString(ArrayList<Integer> nums)
{
    int longest = 0;
    int count = 0;
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i) > 0)
        {
            count++;
        }
        else
        {
            if(longest<count)
            {
                longest = count;
            }
            count = 0;
        }
    }
    return longest;
}

If you want to arrange the numbers into order you can simply use java.util.TreeSet . 如果要按顺序排列数字,可以简单地使用java.util.TreeSet Then use the method last() to get the largest number. 然后使用方法last()获得最大数量。

public static int findLargest(ArrayList<Integer> nums) {
    return new TreeSet<Integer>(nums).last();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM