大家好我得到了这些代码,但我需要对我的数组做一些约束:public static void main (String[] args)
{Random myRand = new Random ();
// Pick array size T from 1 to 100.
int T = 1 + myRand.nextInt(5);
System.out.println("T="+(T));
int C = 1 + myRand.nextInt(100);
System.out.println("C="+(C));
// Declare array of size T.
int [] production_cost=new int [T];
int []stock1= new int [T];
int[]stock2= new int [T];
int[]fix_cost1= new int [T];
int[]fix_cost2= new int [T];
int[] Demands=new int[T];
// Fill the array with random numbers.
{for (int i=0; i< T; i++){
production_cost[i] = myRand.nextInt(Integer.max(0,20));
stock1[i] = myRand.nextInt(Integer.max(0, 20));
stock2[i]= myRand.nextInt(Integer.max(0,20));
fix_cost1[i]=myRand.nextInt(Integer.max(10, 20) );
fix_cost2[i]=myRand.nextInt(Integer.max(20,30));
Demands[i] = myRand.nextInt(Integer.max(1,C));}
System.out.println("p2::"+Arrays.toString(production_cost));
System.out.println("h1::"+Arrays.toString(stock1));
System.out.println("h2::"+Arrays.toString(stock2));
System.out.println("K1::"+Arrays.toString(fix_cost1));
System.out.println("K2::"+Arrays.toString(fix_cost2));
System.out.println("d::"+Arrays.toString(Demands));
答案 0 :(得分:0)
您可以看到此链接:Fill an array with random numbers
您需要添加逻辑以将随机值分配给double []数组使用 randomFill方法。
更改
public static double [] list(){
public static double[] list() { anArray = new double[10]; for(int i=0;i<anArray.length;i++) { anArray[i] = randomFill(); } return anArray; }
要
public static void main(String args[]) { list(); print(); }
然后你可以在main方法中调用方法,包括list()和print()来生成随机双精度值并打印出来 控制台中的double []数组。
string str = "something"; Timer t = Timer(methodC, str, 0, 1000);
结果如下:
-2.89783865E8
1.605018025E9
-1.55668528E9
-1.589135498E9
-6.33159518E8
-1.038278095E9
-4.2632203E8
1.310182951E9
1.350639892E9
6.7543543E7