2014-10-06 67 views
0

我想要做的是根据用户给定的值创建一个数组。用户必须给出阵列的长度加上最大值和最小值。我设法让程序达到输出正确数量值(正确长度)的程度,但它只是继续输出完全相同的数字(即添加的最大值和最小值)。根据我的研究,我尝试将它们转换为一个字符串,这样就不会发生,但它仍然无法正常工作。我尝试了几种不同的方法,包括:Integer.toString,String.valueOf,并创建一个全新的字符串。任何帮助将不胜感激。这里是到目前为止的代码:基于用户给定的值范围创建一个数组

public static void main(String[] args) { 

    //Create a Scanner object 
    Scanner input = new Scanner(System.in); 

    //Ask the user to enter the length of the array 
    System.out.println("Please enter the length of the array:"); 
    int arraylength = input.nextInt(); 

    //Ask the user to enter a max value 
    System.out.println("Please enter the max value:"); 
    int max = input.nextInt(); 

    //Ask the user to input the minimum value 
    System.out.println("Please enter the min value:"); 
    int min = input.nextInt(); 

    //Initialize the array based on the user's input 
    double [] userArray = new double[arraylength];        

    /** 
    *The program comes up with random numbers based on the length 
    *entered by the user. The numbers are limited to being between 
    *the minimum and maximum value. 
    */ 
    for (int i = min; i < userArray.length; i++) { 
     userArray[i] = Math.random() * max; 

    } 

    //This code is supposed to sort the array and print out all of the numbers in order, 
    //with minimum in the beginning and max in the end. 
    for (int i = 0; i < userArray.length; i++) { 

     selectionSort(userArray); 

      Integer.toString(min); 
      Integer.toString(max); 

      System.out.println(min + userArray[i] + max); 
    } 

    //This code uses the method average to find the average 
    average(userArray); 

//Close Scanner 
input.close(); 
    } 

public static double average(double[] data) { 

    double sum = 0; 
    for (int i = 0; i < data.length; i++) { 
     sum = sum + data[i]; 

    } 

    double average = sum/data.length; 

    return average; 

} 

public static void selectionSort(double[] list) { 
    for (int i = 0; i < list.length - 1; i++) { 
     //Find the minimum in the list[i...list.length-1] 
     double currentMin = list[i]; 
     int currentMinIndex = i; 

     for (int j = i + 1; j < list.length; j++) { 
      if (currentMin > list[j]) { 
       currentMin = list[j]; 
       currentMinIndex = j;      
      } 
     } 

     //Swap list[i] with list[currentMinIndex] if necessary 
     if (currentMinIndex != i) { 
      list[currentMinIndex] = list[i]; 
      list[i] = currentMin; 
     } 


    } 
} 


} 

现在,经过我补充说,位与平均计算,该方案没有工作一次,虽然它没有计算平均值(所以它只是创造了最大和最小的一个数组结束,排序)。它似乎是一种侥幸,因为这是完全相同的代码,并且从那以后就没有这样做过。尽管平均代码可能会影响其他代码?

回答

0

如果我理解正确的,你的问题,你需要改变这一行

System.out.println(min + userArray[i] + max); 

这样:

System.out.println(min.toString() + " " + userArray[i].toString() + " " + max.toString()); 

我的猜测是,这是你使用的是Java。该标签也会有帮助。

编辑: 你只需要您的数组进行排序,这些无能为力:Integer.toString(min); 所以打印程序可能看起来像这样:

selectionSort(userArray); 
for (int i = 0; i < userArray.length; i++) { 
    System.out.println(min.toString() + " " + userArray[i].toString() + " " + max.toString()); 
} 
+0

是的,它的Java。我改变了,对不起。我调整了代码,现在它读取:** bold ** selectionSort(userArray);对于(i = 0; i Jupiter8 2014-10-06 12:42:24

+0

在你的粗体部分而不是'Double.toString(userArray [])'你应该有'Double.toString(userArray [i])'。这样你就不会将数组转换为字符串,但只有一个double。 – JoriO 2014-10-06 15:33:35

相关问题