[英]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.