簡體   English   中英

如何使用 1-1000 的整數創建 100 的數組?

[英]How To create Array Of 100 With Integers From 1-1000?

我知道如何用 1-100 的整數創建一個 100 的數組,這將是:

int [] array = new int[100];                       // Sorted Array of 100
for (int a = 0; a < array.length; a++) {
    array[a] = a + 1;
}

但我的問題是如何創建一個 100 的數組,其中包含 1-1000 之間的一些排序整數。 任何幫助將不勝感激!

這個怎么樣?

int [] array = new int[100];
for (int a = 0; a < array.length; a++) {
    array[a] = (a + 1) * 10;
}

很簡單,如果你沒有其他要求。

編輯:為了使它幾乎排序(就像每 10 個未排序的元素一樣),有很多方法。 一,使用 BevynQ 的解決方案,可以是:

Random r = new Random();
int [] array = new int[100];
for (int a = 0; a < array.length; a++) {
    if ((a + 1) % 10 != 0) {
        array[a] = (a + 1) * 10;
    } else {
        array[a] = r.nextInt(1000);
    }
}

這是一個使用隨機的簡單解決方案

Random r = new Random();
int [] array = new int[100];
int last = 0;
for (int a = 0; a < array.length; a++) {
    last = last + r.nextInt(10) + 1;
    array[a] = last;
}

您甚至可以創建一個包含特定序列的數據元素的數組,例如素數、因子或某些序列(如斐波那契數列)。

例子:

class Fibonacci { 
    public static void main(String args[]) { 
        int array[] = new int[100]; 
        System.out.println("*****Fibonacci Series*****"); 
        int f1, f2=0, f3=1; 
        for(int i=1;i<=100;i++) { 
            array[i] = f3;
            f1 = f2; 
            f2 = f3; 
            f3 = f1 + f2; 
        } 
    } 
}

你甚至可以讓它的排序方式可以很容易地被用戶改變。 這是要編寫的更多代碼,但本質上是通過交換數組中的一定數量的點來工作的。 該數字可以由用戶更改。 我在交換數字之前將 0 設置為 100,但重要的是它是一個有序的數學模式。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package partlysorted;

import java.util.Scanner;

/**
 *
 * @author Library computer
 */
public class PartlySorted {
/**
 * @param args the command line arguments
 */
  public static void main(String[] args) {
    // TODO code application logic here


    //scanner for user input
    Scanner input = new Scanner(System.in);

    //intro
    System.out.println("Welcome to the partly sorted pogram");
    System.out.println("This will make a partly sorted list of integers");

    //the numbers
   int[] nums = new int[100];

   //how unsorted for it to be
   int suffels = -1;

   //when to show a typo message 
   boolean firstLoop = true;

   while(suffels  < 0 || suffels > 100)
   {
        if(firstLoop) 
        {
           System.out.println("Please enter how sorted sorted you want (0 to 100, no decimals)");
        }
        else
        {
            System.out.println("Looks like you made a typo");
            System.out.println("Please enter a integer from 0 to 100");
        }
        suffels = input.nextInt();
        firstLoop = false;
   }



   //fill it sorted first

   for(int i = 0; i < nums.length; i++)
   {
       nums[i] = i;
   }

   //suffle the array
   for(int swaps = 0; swaps < suffels; swaps++)
   {
       int firstPlace = (int)(Math.random() * 100);
       int secondPlace = (int)(Math.random() * 100);

       //swap the places
       int temp = nums[firstPlace];
       nums[firstPlace] = nums[secondPlace];
       nums[secondPlace] = temp;
   } 

   //printing it out
   for(int n: nums)
   {
       System.out.println(n);
   }
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM