Java – you need to read in the ArrayList and separate positive and negative numbers

I need to read 10 integers into ArrayList and program them into two groups: "positive integers" and "negative integers"

I have two ArrayLists and an IF statement to determine where each integer should go, but I don't know what variables to use to make this work This is what I have done so far:

import java.util.*;
public class Sort {
  public static void main (String [] args) {
      ArrayList<Integer> pos = new ArrayList<Integer>();
      ArrayList<Integer> neg = new ArrayList<Integer>();
      Scanner sc = new Scanner(system.in);

      int i=0 ;

      while(i <= 10) {
          System.out.println("enter an integer");
          if(??? < 0) {  //here is where my question is
              neg.add(sc.nextInt());
          } else {
              pos.add(sc.nextInt());
          }

          i++;  
      }
      System.out.println("positive numbers" + pos);
      System.out.println("negative numbers" + neg);
   }
}

Solution

while(i<=10){
while(i<=10){
    System.out.println("enter an integer");
    int next_int = sc.nextInt();
    if(next_int < 0) {  //here is where my question is
        neg.add(next_int);
    } else {
        pos.add(next_int);
    }

    i++;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>