从方法

时间:2016-05-02 21:28:17

标签: java arrays methods

我认为它从内存中打印代码而不是实际的数组。我用这种方式编码的原因是一切都必须在一个方法中。当我运行它打印char而不是整数。这就是我从记忆中找到它的原因。

enter code here

import java.util.Random;
import java.util.Scanner;


public class ArrayDemo
{
    public static void main(String[] args)
{
    int[] numbers1 = readInputs(5);
    int[] numbers2 = createNumbers(5);
    System.out.println("System Generate Array");
    printArray1(numbers1);
    System.out.println("User Input Array");
    printArray2(numbers2);
}
public static int[] createNumbers(int numberOfOutputs)
{
    Random generator = new Random();
    int Low = 1;
    int High = 25;
    int[] outputs = new int[numberOfOutputs];
    for (int i = 0; i < outputs.length; i++)
    {                                           
        outputs[i] = generator.nextInt(High - Low) + Low;
    }
    return outputs;
}    
public static int[] readInputs(int numberOfInputs)
{
  System.out.println("Enter " + numberOfInputs + " numbers: ");
  Scanner in = new Scanner(System.in);
  int[] inputs = new int[numberOfInputs];
  for (int i = 0; i < inputs.length; i++)
  {
     inputs[i] = in.nextInt();
  }
  return inputs;
}
public static void printArray1(int[] list1)
{
   System.out.println(list1 + " ");
}
public static void printArray2(int[] list2)
{
    System.out.println(list2 + " ");
}
}

0 个答案:

没有答案