简体   繁体   English

使用sort方法和JOptionPane让用户输入10个数字,然后输出10个已排序的数字

[英]Use sort method and JOptionPane to have user input 10 numbers and output be the 10 numbers sorted

This is what I have so far: 这是我到目前为止的内容:

import javax.swing.JOptionPane; 导入javax.swing.JOptionPane; public class Aukeemhw3 { 公共课程Aukeemhw3 {

/**
 * 
 * @param numberSort the purpose is to sort 10 input numbers
 */

public static void numInputSort(int[] numberSort){

    for(int i = 0; i < (numberSort.length - 1); i++){

        int min = i;

                for(int j = i; j < (numberSort.length); j++){
                        if(numberSort[j] < numberSort[min])
                            min = j;
                }
                int temp = numberSort[min];
                numberSort[min] = numberSort[i];
                numberSort[i] = temp;
    }
}
/*
* @param args the command line arguments
*/
public static void main(String[] args) {
    // Request for user to enter 10 numbers

    String[] numbers;
    numbers = new String[10];

    JOptionPane.showInputDialog("Enter " + numbers + "Values: ");

    numImportSort(int[] numbers);


}

} }

I keep getting errors with everything I try when calling my method at the last line. 在最后一行调用我的方法时,我尝试执行的所有操作始终出错。 I can't figure out how to properly code my main argument to ask the user to input 10 numbers then call my sort method numInputSort to sort the 10 numbers being input. 我无法弄清楚如何正确编码我的主要参数来要求用户输入10个数字,然后调用我的排序方法numInputSort来对输入的10个数字进行排序。

Plenty of errors in the main method. 主要方法中存在大量错误。 What you should do to fix: 您应该做什么来解决:

  • JOptionPane returns String so you should enter your numbers so that it can be converted to numbers, with some sort of delimiter JOptionPane返回String,因此您应该输入数字,以便可以使用某种分隔符将其转换为数字
  • Split and convert to numbers 拆分并转换为数字
  • Assemble and int array and call the method 组装和int数组并调用方法

Something like 就像是

public static void main(String[] args) {
    // Request for user to enter 10 numbers

    int[] numbers = new int[10];

    String sNumbers =  JOptionPane.showInputDialog("Enter number values, split with comma: ");

    String[] singleNumbers = sNumbers.split(",");
    for (int i = 0; i < singleNumbers.length; i++) {
        numbers[i] = Integer.parseInt(singleNumbers[i].trim());
    }
    numInputSort(numbers);
    JOptionPane.showMessageDialog(null, Arrays.toString(numbers), "sorted",  JOptionPane.INFORMATION_MESSAGE);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM