从其他表中绘制并保存三个随机值到数组

时间:2019-06-03 04:55:12

标签: java

我尝试从三个表中绘制随机值并将其保存到另一个表中。我不确定自己在做什么错,或者根本就没有正确的想法

String[] name = {"Audi 100", "Audi 200", "Audi 4000", 
                 "Audi 50", "Audi e-tron",
                 "Audi A2", "Audi A3", "Audi A4", 
                 "Audi A5", "Audi A6", "Audi F103",
                 "Audi 5000", "Audi 80", "Audi 90", 
                 "Audi A1", "Audi A7", "Audi A8",
                 "Audi Allroad", "Audi Cabriolet", 
                 "Audi Coupe", "Audi Coupe S"};

    int[] engine = {10*20};
    int[] numberOfSeats = {1,2,3,4,5};
    Random r = new Random();
    MotorVehicle[] model = new MotorVehicle[100];
    for(int i = 0; i < model.length; i++) model[i] = new
    MotorVehicle((int)(Math.random()*5)+1,name[r.nextInt(name.length)],new 
    Silnik((int)((Math.random()* 250)+50),(long) 
    ((Math.random()*6000l)+50l)));

1 个答案:

答案 0 :(得分:0)

由于我们没有MotorVehicle和Silnik类,所以我假设一些必要的数据成员可以测试您的程序。 另一件事是-我们没有达到engine []和numberOfSeats []数组的目的。

丢弃这些东西,您的程序看起来不错,并且不会引发任何绑定的异常。

class MotorVehicle{
  int motor_a;
  String motor_b;
  Silnik motor_c;
  public MotorVehicle(int i, String string, Silnik silnik) {
    // TODO Auto-generated constructor stub
    motor_a = i;
    motor_b = string;
    motor_c = silnik;
  }

}
class Silnik{
  int silinik_a;
  long silinik_b;
  public Silnik(int i, long l) {
    // TODO Auto-generated constructor stub
    silinik_a = i;
    silinik_b = l;
  }
}

public class Test extends Driver {
  public static void main(String[] args) {
    String[] name = { "Audi 100", "Audi 200", "Audi 4000", "Audi 50", "Audi e-tron", "Audi A2", "Audi A3", "Audi A4",
        "Audi A5", "Audi A6", "Audi F103", "Audi 5000", "Audi 80", "Audi 90", "Audi A1", "Audi A7", "Audi A8",
        "Audi Allroad", "Audi Cabriolet", "Audi Coupe", "Audi Coupe S" };

    int[] engine = { 10 * 20 };
    int[] numberOfSeats = { 1, 2, 3, 4, 5 };
    Random r = new Random();
    MotorVehicle[] model = new MotorVehicle[100];


    for (int i = 0; i < model.length; i++) {
      int rand5 = (int)(Math.random()*5) + 1;
      int randN = r.nextInt(name.length);
      String randName = name[randN];
      int rand250 = (int)(Math.random()*250)+50;
      long rand6000 = (long)(Math.random()*6000l)+501;
      model[i] = new MotorVehicle(rand5, randName,new Silnik(rand250, rand6000));
    }

    for(int i = 0;i<model.length;i++) {
      System.out.println(model[i].motor_a + " #  "+model[i].motor_b + " # "+model[i].motor_c.silinik_a + " # "+ model[i].motor_c.silinik_b );
    }
  }

输出:

1 #  Audi 200 # 123 # 3160
3 #  Audi A4 # 276 # 2625
2 #  Audi e-tron # 267 # 6153
1 #  Audi 100 # 100 # 741
3 #  Audi 4000 # 146 # 6335
3 #  Audi 200 # 174 # 1155
5 #  Audi Allroad # 164 # 5588
4 #  Audi 90 # 250 # 2021
2 #  Audi e-tron # 285 # 4536
5 #  Audi F103 # 92 # 5828
4 #  Audi A7 # 238 # 1049
3 #  Audi A6 # 271 # 2187
1 #  Audi Coupe # 105 # 1422
2 #  Audi A3 # 274 # 4337
3 #  Audi A4 # 211 # 2850
4 #  Audi A4 # 229 # 1048
3 #  Audi 4000 # 238 # 4981
3 #  Audi A8 # 208 # 2051
5 #  Audi 100 # 89 # 5998
4 #  Audi 200 # 77 # 5026
3 #  Audi A7 # 230 # 967
1 #  Audi A4 # 78 # 3321
5 #  Audi 4000 # 243 # 1612
1 #  Audi A7 # 85 # 5704
1 #  Audi A7 # 269 # 1914
5 #  Audi Coupe S # 136 # 6371
4 #  Audi A2 # 224 # 1079
1 #  Audi 4000 # 198 # 2126
5 #  Audi e-tron # 212 # 5673
2 #  Audi 50 # 134 # 3746
1 #  Audi A2 # 150 # 5832
1 #  Audi A5 # 247 # 2257
4 #  Audi Allroad # 180 # 6049
4 #  Audi e-tron # 222 # 3857
3 #  Audi F103 # 170 # 2547
1 #  Audi F103 # 165 # 2669
3 #  Audi 4000 # 266 # 4824
2 #  Audi 200 # 286 # 2328
5 #  Audi 50 # 145 # 5524
4 #  Audi 80 # 243 # 3674
2 #  Audi 90 # 250 # 5193
5 #  Audi A3 # 96 # 524
3 #  Audi A4 # 159 # 1081
2 #  Audi Allroad # 90 # 1882
3 #  Audi F103 # 62 # 5466
3 #  Audi Cabriolet # 148 # 4968
5 #  Audi 5000 # 161 # 2834
2 #  Audi Cabriolet # 242 # 3434
1 #  Audi e-tron # 138 # 4758
3 #  Audi 50 # 171 # 3119
3 #  Audi 90 # 287 # 5967
1 #  Audi 50 # 203 # 5071
5 #  Audi A7 # 171 # 3352
4 #  Audi 80 # 143 # 6441
2 #  Audi Allroad # 166 # 4768
1 #  Audi 5000 # 132 # 4580
3 #  Audi Cabriolet # 260 # 1412
2 #  Audi Coupe S # 253 # 2618
5 #  Audi A4 # 60 # 1015
3 #  Audi 80 # 149 # 1188
1 #  Audi 200 # 78 # 4793
5 #  Audi 200 # 213 # 1298
1 #  Audi e-tron # 213 # 5143
4 #  Audi A2 # 104 # 2000
2 #  Audi 50 # 227 # 5827
3 #  Audi Cabriolet # 139 # 1833
5 #  Audi 80 # 169 # 3545
4 #  Audi Coupe S # 63 # 4705
1 #  Audi A3 # 144 # 6161
3 #  Audi Coupe S # 103 # 1136
2 #  Audi A4 # 103 # 3810
5 #  Audi 200 # 59 # 1457
3 #  Audi 50 # 238 # 3969
3 #  Audi F103 # 148 # 2981
3 #  Audi Allroad # 160 # 5260
4 #  Audi A2 # 264 # 4053
2 #  Audi Coupe S # 179 # 1198
2 #  Audi 5000 # 199 # 4832
1 #  Audi Cabriolet # 126 # 4746
1 #  Audi 50 # 177 # 4372
5 #  Audi A8 # 152 # 5861
5 #  Audi A8 # 77 # 4636
5 #  Audi 90 # 242 # 1312
4 #  Audi Coupe S # 199 # 4370
3 #  Audi 80 # 140 # 3855
4 #  Audi A5 # 189 # 1913
2 #  Audi A8 # 125 # 2068
4 #  Audi 80 # 104 # 2864
3 #  Audi 200 # 263 # 2287
1 #  Audi Allroad # 160 # 4913
3 #  Audi A1 # 254 # 3408
5 #  Audi A6 # 99 # 5840
5 #  Audi 100 # 252 # 3840
3 #  Audi 90 # 165 # 665
1 #  Audi A5 # 214 # 544
5 #  Audi Coupe # 212 # 1058
5 #  Audi 50 # 112 # 5366
3 #  Audi Allroad # 158 # 3290
5 #  Audi 80 # 166 # 6249
4 #  Audi A4 # 121 # 6349