根据用户给出的值范围创建数组

时间:2014-10-06 12:09:20

标签: java arrays sorting input

我要做的是根据用户给出的值创建一个数组。用户必须给出数组的长度加上最大值和最小值。我已经设法让程序输出正确的数值(正确的长度),但它只是输出完全相同的数字(最大和最小添加)。根据我的研究,我尝试将它们转换为字符串,这样就不会发生,但它仍然无法正常工作。我尝试了几种不同的方法,包括: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;
        }


    }
}


}

现在,在我用平均计算添加了这个位之后,程序确实工作了一次,虽然它没有计算平均值(所以它只创建了一个最小值和最大值的数组,排序)。它似乎是一个侥幸,因为这是完全相同的代码,从那以后它没有这样做。虽然平均代码可能会以某种方式影响其余部分?

1 个答案:

答案 0 :(得分: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());  
}