数字排序器程序无法找到符号编译错误

时间:2017-01-24 18:03:58

标签: java arraylist

我正在尝试创建一个数字排序程序,用户输入10个数字,然后将它们放入三个单独的数组列表中,列表包含奇数,偶数和负数。但是,当我运行该程序时,它告诉我:“找不到符号 symbol:方法add(int)“

这是我试图将正数放入数组列表的时候。我该如何解决这个问题?

import java.util.Scanner;
import java.util.ArrayList;

public class NumberSorter
{
public static void main(String [] args)
 {
  Scanner reader = new Scanner(System.in);
  Number[]numbers = new Number[10];
  Number[]oddList = new Number[10];
  Number[]evenList = new Number[10];
  for (int i = 0; i < numbers.length; i++)
  {
   System.out.println("Enter an integer: ");
   numbers[i] = reader.nextDouble();
   if (i % 2 == 0)
    evenList.add(i);
   return evenList;
   }
  }
 }

2 个答案:

答案 0 :(得分:1)

这样的事情?

import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

public class NumberSorter
{
public static void main(String [] args)
 {
  Scanner reader = new Scanner(System.in);
  Number[]numbers = new Number[10];
  ArrayList<Number>oddList = new ArrayList<Number>();
  ArrayList<Number>evenList = new ArrayList<Number>();
  for (int i = 0; i < numbers.length; i++)
  {
   System.out.println("Enter an integer: ");
   numbers[i] = reader.nextDouble();
   if (numbers[i].intValue() % 2 == 0)
   {
       evenList.add(numbers[i]);
   } else {
       oddList.add(numbers[i]);
   }
   }
  }
 }

要将其打印出列表,您可以将以下内容添加到最后:

for (Number num : oddList) {
      System.out.println("oddlist: " + num );
  }

  for (Number num : evenList) {
      System.out.println("evenlist: " + num );
  }

编辑 - 仅使用数组

import java.util.Scanner;

public class NumberSorter
{
public static void main(String [] args)
 {  
  Scanner reader = new Scanner(System.in);
  Number[]numbers = new Number[10];
  Number[] oddList = new Number[10];
  Number[] evenList = new Number[10];
  int oddListIndex = 0;
  int evenListIndex = 0;
  for (int i = 0; i < numbers.length; i++)
  {
   System.out.println("Enter an integer: ");
   numbers[i] = reader.nextDouble();
   if (numbers[i].intValue() % 2 == 0)
   {
       evenList[evenListIndex]=numbers[i];
       evenListIndex++;
   } else {
       oddList[oddListIndex]=numbers[i];
       oddListIndex++;
   }
   }
  System.out.println("evenlist:");
   for( int i = 0; i < evenListIndex; i++) {
       System.out.println(evenList[i]);
   }
  System.out.println("oddlist:");
   for( int i = 0; i < oddListIndex; i++) {
       System.out.println(oddList[i]);
   }

  }
 }

答案 1 :(得分:1)

public class SorterNumber {

public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    List<Double> negative = new ArrayList<>();
    List<Double> odd = new ArrayList<>();
    List<Double> even = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        System.out.println("Enter an integer: ");
        Double x = reader.nextDouble();
        if (Math.signum(x) == -1) {
            negative.add(x);
        } else if (x % 2 == 0) {
            even.add(x);
        } else if (x % 2 != 0) {
            odd.add(x);
        }
    }

    System.out.println(negative);
    System.out.println(even);
    System.out.println(odd);
}

}