Java:用户按字母顺序输入的名称选择类型

时间:2017-11-02 19:16:20

标签: java arrays sorting selection-sort

我已经有一个使用数组的选择排序代码:

public class SelectionSortTest
{ 

 public static void main(String[] args)
 {
      String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"};

      System.out.println("Unsorted array:");
      for(String element: stringArray)
           System.out.print(element + " ");
      System.out.println();

      selectionSort(stringArray);

      System.out.println("Sorted array:");
      for(String element: stringArray)
            System.out.print(element + " ");
      System.out.println();
 }


 static void selectionSort(Comparable[] array)
 {
      int smallindex;
      for(int i = 0; i < array.length; i++)
      {
           smallindex = i; // set first element as smallest
           for(int j = i + 1; j < array.length; j++) // find smallest
                if(array[j].compareTo(array[smallindex]) < 0)
                     smallindex = j;
           if(smallindex != i)
                swap(array, smallindex, i);
      }
 }


 static void swap(Object[] array, int index1, int index2)
 {
      Object temp = array[index1];
      array[index1] = array[index2];
      array[index2] = temp;
 } 
}

但我想改变这一点,以便用户必须输入这些名称。这样的外包就会一直要求&#34;输入名称&#34;并且你放了你想要的任何数量的名字(用逗号分隔)然后完成上述任务。

我尝试过替换

String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"}; 

使用:

Scanner input = new Scanner(System.in);

for (int i = 0; < array.length; i++)
{
System.out.print("Enter names " + (i + 1) + ": ");
array[i] = input.next():    

但它不起作用。有什么建议?

3 个答案:

答案 0 :(得分:0)

您可以添加以下代码以输入逗号分隔的字符串作为用户输入。

    Scanner input = new Scanner(System.in);
    String inputArray = input.next();
    String[] stringArray = inputArray.split(","); 

现在您可以使用stringArray并执行其余代码。希望这会有所帮助。

干杯!

答案 1 :(得分:0)

别介意的人,我想出了怎么做:

import java.util.Scanner;

public class selectionsort
{ 
public static void main (String args[])
{
    Scanner in = new Scanner (System.in);
    System.out.print ("Enter the number of names you wish to enter: ");
    int n = in.nextInt();
    String array[] = new String [n]; //Array to store the names in.

in.nextLine(); // < --- an extra next Line

for (int i = 0; i < array.length; i++)
{
    System.out.println("Please enter the name: ");
    array[i]= in.nextLine();

}
   System.out.println("Unsorted array:");
      for(String element: array)
           System.out.print(element + " ");
      System.out.println();

      selectionSort(array);
      System.out.println("SelectionSorted array:");
      for(String element: array)
            System.out.print(element + " ");
      System.out.println();


}
static void selectionSort(Comparable[] array)
 {
      int smallindex;
      for(int i = 0; i < array.length; i++)
      {
           smallindex = i; // set first element as smallest
           for(int j = i + 1; j < array.length; j++) // find smallest
                if(array[j].compareTo(array[smallindex]) < 0)
                     smallindex = j;
           if(smallindex != i)
                swap(array, smallindex, i);
      }
 }
static void swap(Object[] array, int index1, int index2)
 {
      Object temp = array[index1];
      array[index1] = array[index2];
      array[index2] = temp;
 } 


}

答案 2 :(得分:-1)

您必须先初始化阵列。

Scanner sc = new Scanner(System.in);
System.err.println("enter size of ur table");
int size = sc.nextInt();    
String array[] = new String[size];
for (int i = 0; i < size; i++) {
array[i] = sc.nextLine();
System.err.println("element " + i + "is :" + array[i]);
}