2013-11-26 184 views
0

(java初学者在这里) 我跟着这个Random shuffling of an array 但我想要做的是,而不是随机改变数组中的元素,然后在我的方法中使用它。我想确保更改列表的顺序是从[1] .. [2] .. [3] ..... (基本上增加) 我的最终目标是有一个数字表从洗牌阵列到增量阵列(改变)

INT [] A = {1,2,3,4,5,6},则它在我的方法

static void shuffleArray(double[] solutionArray) 
{ 
    Random rnd = new Random(); 
    for (int i = solutionArray.length - 1; i > 0; i++) 
    { 
     int index = rnd.nextInt(i + 1); 
     int value = 0; 
    // int charValue = value.digit(0); 
     String next = String.valueOf((double) (value + 1)); 
     System.out.println(next); 
     // Simple swap 
     double a = solutionArray[index]; 
     solutionArray[index] = solutionArray[i]; 
     solutionArray[i] = a; 
    } 

    } 

public static void main(String[] args) 
{ 
    double[] solutionArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 
    double[] solution = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 
    shuffleArray(solutionArray); 
    shuffleArray(solution); 

    for (int i = 0; i < 7; i++) 
    { 
     position.setspeed(solutionArray[0]); 
    } 
} 

但是当我将其设置或方法使用它我希望它从1去到6,而不是随机选择一个元素,或从链接中洗牌元素。

+1

我不明白,你能解释清楚一点吗?顺便说一下,'shuffleArray()'中的for循环永远不会终止。 – Blub

+0

道歉误导大家, 其应该是: 对(INT I = solutionArray.length - 1; I> = 0;我 - ){} – user3035890

回答

0

此行不可能是正确的:

for (int i = solutionArray.length - 1; i > 0; i++) 

如果是循环倒退,它应该是:

for (int i = solutionArray.length - 1; i >= 0; i--) 

如果是循环向前,它应该是:

for (int i = 0; i < solutionArray.length; i++) 
+0

道歉误导大家, 其应该是: for(int i = solutionArray.length - 1; i> = 0; i--){} user3035890

0

用于表

import java.util.Scanner; 

public class ArrayTable { 

public static void main(String[] args) { 
    int a[]={1,2,3,4,5,6,7,8,9,10}; 

    int solution[]=new int[10]; 


    Scanner sc=new Scanner(System.in); 
    System.out.println("Enter the number"); 
    int num=sc.nextInt(); 

    for(int i=0;i<a.length;i++) 
    { 
     solution[i]=a[i]*num; 
    } 
    for(int y:solution) 
    { 
     System.out.print(y+"\n"); 
    } 
} 
}