Math.random数组长度?

时间:2019-02-26 14:06:37

标签: java

这是我的代码,要求我们为每个数组提取一个值,以使getter为math.random和.length。我正在尝试所有的东西,但不能继续前进。我能来吗?谢谢大家

public Integer [] year = {2010, 2008, 2018, 2015, 1994,};

(编辑:这是代码。如何创建带有3种情况的随机和开关?)

utility package;
import java.util.Random;
public class TavoloLavoro {
  private static Integer [] year = {2010, 2008, 2018, 2015, 1994,};
  private static string [] brand = {"Fiat", "Ford", "Ducati", "BMW", "Yhamaha"};
  private static integer [] displacement = {1200, 250, 1400, 1000, 600};
  Entire static private [] times = {2,4};
  private static integer [] ports = {5, 3};
  private static power supply [] power supply = {power supply.
  private static integer [] capacity = {1000, 2000, 3000};
}

1 个答案:

答案 0 :(得分:1)

您的问题尚不清楚,但据我所知,您想从年份数组中选择一个随机元素。 顺便说一句,不要让您的全球变量公开!!!

private Random random = new Random(); //u need to import java.util.Random
private int year[] = new int[]{2010, 2008, 2018, 2015, 1994}; //you had a comma behind the last element...

private int random_value_from_array = year[random.nextInt(year.length)];