2014-10-19 99 views
0

我必须得到一个数字序列并计算它需要多少次,然后将其放入一个数组中,以便我可以对数组进行排序并查找第10个最大值。我可以得到计数,但似乎不能填充数组,我要么在数组中的每个索引处输入最终计数,要么只是将索引0处的数组值增加到所有数字的计数,任何人都可以提供帮助。如何用给定值填充数组

public class seBuilder 

{ 

//int count=0; 


//int [] myArray= new int[10]; 


    public static void main (String args[]) 
    { 
     int count=0; 
     int [] myArray= new int[13]; 
     int z=0; 
     for(int i=2;i<=myArray.length;i++) 
     { 
     z=valuegetter(i); 
     System.out.println(z); 

     } 

     //arraycounter(myArray, z); 


    } 

    public static int valuegetter(int num) 
    { 
    int count=0; 
    do// do while loop 
    { 
     //if its an odd number 
     if(num%2==1){ 
      num=(num*3)+1;//do this from the question 
      count++;// add one to count 
     } 
     //if num is 2 this will catch and make the code break out 
     else if(num==2){ 
      //devide num by 2, this will give you 1 allways 
      System.out.println(num/2); 
      count++;//adds 1 again 
     } 
     else 
     { 
      num=num/2;//this will use if number is even 
      count++;//adds one to count 
     } 

    } 
     while(num>2);//the condition to jump out of loop 
     //System.out.println("there are "+count+" sequences in that mumber"); 
    return count; 
    } 


    public static int[] arraycounter(int myArray[], int count) 
    { 

     for(int i=0;i<=myArray.length-1;i++) 
     { 
      myArray[i]=count; 
      System.out.println(" "+myArray[i]); 
     } 
     return myArray; 
    } 

    public static int tenhigh() 
    { 

    } 
} 
+1

很抱歉,但目前还不清楚 – Nabin 2014-10-19 16:04:09

回答

3

试图改变你的主这样的:

public static void main (String args[]) 
{ 
    int count=0; 
    int [] myArray= new int[13]; 
    int z=0; 
    for(int i=2;i<=myArray.length;i++) { 
    z=valuegetter(i); 
    System.out.println(z); 
    arraycounter(myArray, z, i); 

    } 
    for (int i=0; i < myArray.length; i++) { 
     System.out.print(myArray[i] + ", "); 
    } 
} 

也能改变你arrayCounter方法是:

public static int[] arraycounter(int myArray[], int count, int i) 
{ 
    int j = i - 2; 
    myArray[j] = count; 
    return myArray; 
} 
+0

那非常工作一种享受感谢很多的帮助.. – bri 2014-10-19 18:56:10

+0

不客气,很高兴我可以帮助! – Edwin 2014-10-19 23:54:56

0

如果要填充数组与给定值,使用Arrays.fill,从java.utils.Arrays类。

+0

感谢您的帮助 – bri 2014-10-19 18:57:03