-7

我想知道如何在给定起点和终点的情况下动态填充数组?假设有可能?

我目前的方式(硬编码整数)

int xValues[] = {340,347,348,349,352,355,359,364,369,376,383,392,400,410,421,431,443,455,467,480,492,505,519,532,545,559,572,585,599,612,625,637,648,658,667,675,681,686,690,693,696,697,698,700};  
for (int i = 0; i < xValues.length; i++) 
{  

    x=xValues[i];

所需方式

int xValues[] = Range(340, 700);  // if possible unit of increment 1 or 10

谢谢你,编码愉快。

4

1 回答 1

2

If you are looking for a code that generates random numbers between the given range, the below code is for you

 import java.util.Scanner;
 import java.util.Random;
 public class Insert {
static Scanner input=new Scanner(System.in);
    public static void main(String [] args){
 Random rand=new Random();
 int max,min;
 System.out.println("enter the maximum number");
 max=input.nextInt();
 System.out.println("enter the minimum number");
 min=input.nextInt();
 int range=max-min+1;
 int arr[]=new int[100];
 for(int i=0;i<100;i++){
      int random=rand.nextInt(max-min+1)+min;
      arr[i]=random;  
  }

 for(int i=0;i<100;i++){
      System.out.println(arr[i]);

  }


 }
于 2013-08-21T14:41:04.337 回答