数组不打印或进入阵列?

时间:2017-01-06 12:41:32

标签: java arrays random import

所以我必须生成一个数组(用户输入的大小),限制为0-9;但每次我尝试

  

与Array.toString()

它编译错误。即使我导入了编译器声明我无法导入或解析的数组。 开始

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Frequency
{
  int var1,var2,var3,var4,max,min,var5,var6,var7,var8;
  int [] a;

  Scanner dab = new Scanner(System.in);
  public void rand()
  {
    Random rande = new Random();
    System.out.println("How many Random Values ");
    var1 = dab.nextInt();
    for (int k = 0; k > var1; k++)
    {
      a[k] = (int)(Math.random() * 10);
    }
    System.out.println(Arrays.toString(a));
  }
}

测试仪:

public class FrequencyTester
{
  public static void main(String[]args)
  {
    Frequency yeet = new Frequency();
    yeet.rand();
    System.out.println("test");
  }
}

2 个答案:

答案 0 :(得分:1)

你的第一个错误是For循环k必须小于var1

for (int k = 0; k < var1; k++)

答案 1 :(得分:0)

试试这个

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
class Frequency
{
  int var1,var2,var3,var4,max,min,var5,var6,var7,var8;
  int [] a;

  Scanner dab = new Scanner(System.in);
  public void rand()
  {
    Random rande = new Random();
    System.out.println("How many Random Values ");
    var1 = dab.nextInt();
    a=new int[var1];
    for (int k = 0; k < var1; k++)
    {
      a[k] = (int)(Math.random() * 10);
    }
    System.out.println(Arrays.toString(a));
  }
}

public class FrequencyTester
{
  public static void main(String[]args)
  {
    Frequency yeet = new Frequency();
    yeet.rand();
    System.out.println("test");
  }
}