我想编写一个简单的程序,要求您通过键盘输入数字(每个数字用空格或逗号分隔),然后将它们从低到高排序。
我想到的是要求用户通过键盘输入数字,将输入设置为String对象,然后通过for循环传递String,以确定字符是数字还是普通字符。如果caracter是一个数字,它将附加到一个String数组的字段并查找下一个数字并执行相同的操作,直到一个字符不是数字;在这种情况下,它会查找另一个数字并重复相同的过程,但使用下一个String数组的字段。一旦程序通过了String String的长度,String Array的每个字段都将转换为int,因此可以对其进行排序然后打印。
现在的问题是我无法打印出已排序的数字。 它让我输入随机数并对它们进行排序,但它不会打印出结果。
以下是源代码:
import java.util.Scanner;
public class StartHere {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Type random numbers: ");
String input = scanner.nextLine();
String[] numString = new String[input.length()];
int[] numbers = new int[numString.length];
int a = 0;
int i = 0;
for (i = 0; i < input.length(); i++){ // Each numString field is initialized as " ".
numString[i] = "";
}
i = 0;
while(i < (input.length() - 1)){
if (Character.isDigit(input.charAt(a))) { // If the character at input[a] is a digit
numString[i] += Character.toString(input.charAt(a)); // The char is appended to the numString so the hole String field can be
// converted to an integer later.
a++; // We go to the next character.
}
if ((numString[i] != null) && (!Character.isDigit(input.charAt(a)))) { // If the numString[i] field is occupped and the character
// at input[a] is not a digit.
i++; // We go to the next field.
}
}
for (int b = 0; b < numString.length;) { // Inside this for loop each field of the numString array
// is converted to an integer.
if (numString[b] != null) {
numbers[b] = Integer.parseInt(numString[b]);
b++;
} else {
b++;
}
}
quickSort(numbers, 0, numbers.length - 1); // Sorts the numbers from smaller to higher.
for (int c = 0; c < numbers.length - 1; c++){ //
if(c != numbers.length - 1){
System.out.print(numbers[0] + ", ");
}else System.out.println(numbers[c] + ".");
}
}
public static void quickSort(int[] numbers, int left, int right) { // Defines the quickSort method.
int pivot = numbers[left]; // We take the first element as pivot.
int l = left; // Searches from left to right.
int r = right; // Searches from right to left.
int aux;
while (l < r) { // While searches are not cross.
while (numbers[l] <= pivot && l < r)
l++; // Searches for an element higher than the pivot.
while (numbers[r] > pivot)
r--; // Searches for an element smaller than the pivot.
if (l < r) { // If this have not been crossed.
aux = numbers[l]; // Interchange.
numbers[l] = numbers[r];
numbers[r] = aux;
}
}
numbers[left] = numbers[r];
numbers[r] = pivot;
if (left < r - 1) {
quickSort(numbers, left, right - 1);
}
if (r + 1 < right) {
quickSort(numbers, r + 1, right);
}
} }
我该怎么办?提前谢谢!
编辑:现在我初始化了String[] numString
的每个字段,这就是控制台所说的:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at StartHere.main(StartHere.java:34)
答案 0 :(得分:2)
您正在打印第一个n-1
次的号码:
if(c != numbers.length - 1){
System.out.print(numbers[0] + ", "); <-- this
}else System.out.println(numbers[c] + ".");
您应该将其更改为numbers[c]
答案 1 :(得分:2)
这个方法确实存在问题:
while(i < (input.length() - 1)) {
if (Character.isDigit(input.charAt(a))) {
numString[i] += Character.toString(input.charAt(a));
a++; // We go to the next character.
}
if ((numString[i] != null) && (!Character.isDigit(input.charAt(a)))) {
i++; // We go to the next field.
}
}
由于您从未初始化String
数组numString
,执行numString[i] += Character.toString(input.charAt(a));
会为null6
输入6
,而不仅仅是6
1}}。
您稍后尝试通过执行Integer
将其解析为numbers[b] = Integer.parseInt(numString[b]);
。这将抛出异常!
此外,您应该重新评估循环条件。您是否也应该从a
中的input.length()
中减去while(i < (input.length() - 1))
?