简体   繁体   English

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

[英]Java: Selection sort of names entered by the user in alphabetical order

I already have a code for Selection sort using an array: 我已经有了使用数组的选择排序的代码:

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;
 } 
}

But i want to change this so that the user has to input those names instead. 但我想更改此名称,以便用户必须输入这些名称。 Such that the outprint keeps asks "Enter the names" and you put whatever amount of names you want (separated by commas) then does the above task. 这样,输出记录始终会询问“输入名称”,然后输入所需数量的名称(用逗号分隔),然后执行上述任务。

i Have tried replacing 我曾尝试更换

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

with: 与:

Scanner input = new Scanner(System.in);

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

But it doesn't work. 但这是行不通的。 Any suggestions? 有什么建议么?

You can add following code to enter comma separated String as user input. 您可以添加以下代码来输入逗号分隔的字符串作为用户输入。

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

Now you can use stringArray and execute rest of the code. 现在,您可以使用stringArray并执行其余代码。 Hoping this helps. 希望这会有所帮助。

Cheers ! 干杯!

Never mind guys, i figured out how to do it: 没关系的家伙,我想出了怎么做:

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;
 } 


}

You must initialise the array first. 您必须先初始化数组。

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]);
} 

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

相关问题 使用选择排序按字母顺序对名称的 ArrayList 进行排序时遇到问题 - Having trouble using selection sort to sort an ArrayList of names in alphabetical order 用户根据姓氏按字母顺序对输入的名称进行排序 - Sorting names entered by the user in alphabetical order according to the last name 如何应用选择方法按字母顺序排序? - How to apply Selection method to sort in alphabetical order? 如何使用String.equals和String.compareTo按字母顺序对用户输入的字符串名称进行排序 - How to sort user input String names in alphabetical order, using String.equals and String.compareTo Java:按长度排序单词列表,然后按字母顺序排序 - Java: Sort a list of words by length, then by alphabetical order 按字母顺序对 java 中的字符串列表进行排序 - Sort a list of strings in java in alphabetical order 按字母顺序对文本文件中的数据进行排序-Java - Sort data in a text file in alphabetical order - Java 在Java中不按数组的字母顺序对字符串排序 - Sort a string in alphabetical order without arrays in java 尝试使用Java按字母顺序对用户输入进行排序,为什么此代码不起作用? - Trying to sort user input into alphabetical order with Java, why won't this code work? 如何使用 Arrays.sort() 按字母顺序输出名称? - How to use Arrays.sort() to output names in an alphabetical order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM