我需要以下问题的帮助。首先我列出了 Numbers.java,下面是我对 Strings.java 的尝试。请帮忙!
编写一个类似于 Numbers.java 的程序 Strings.java,它读入一个 String 对象数组并对它们进行排序。您可以只复制和编辑 Numbers.java。
package hw05; /* Demonstrates selectionSort on an array of integers. */ import java.util.Scanner; public class Numbers { // -------------------------------------------- // Reads in an array of integers, sorts them, // then prints them in sorted order. // -------------------------------------------- public static void main(String[] args) { Integer[] intList; int size; Scanner scan = new Scanner(System.in); System.out.print("\nHow many integers do you want to sort? "); size = scan.nextInt(); intList = new Integer[size]; System.out.println("\nEnter the numbers..."); for (int i = 0; i < size; i++) intList[i] = scan.nextInt(); Sorting.selectionSort(intList); System.out.println("\nYour numbers in sorted order..."); for (int i = 0; i < size; i++) System.out.print(intList[i] + " "); System.out.println(); } }
我的弦乐课
package hw05;
/*
Demonstrates selectionSort on an array of strings.
*/
import java.util.Scanner;
public class Strings {
// --------------------------------------------
// Reads in an array of strings, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main(String[] args) {
String[] stringList;
String size;
Scanner scan = new Scanner(System.in);
System.out.print("\nHow many strings do you want to sort? ");
size = scan.nextLine();
stringList = new String[size];
System.out.println("\nEnter the strings...");
for (String i = 0; i < size; i++)
stringList[i] = scan.nextLine();
Sorting.selectionSort(stringList);
System.out.println("\nYour strings in sorted order...");
for (String i = 0; i < size; i++)
System.out.print(stringList[i] + " ");
System.out.println();
}
}