【问题标题】:System.out.print doesn't workSystem.out.print 不起作用
【发布时间】:2016-03-30 21:20:04
【问题描述】:

我想编写一个简单的程序,要求您通过键盘输入数字(每个数字用空格或逗号分隔),然后将它们从低到高排序。

我的想法是要求用户通过键盘输入数字,将输入设置为 String 对象,然后将 String 传递给一个 for 循环,以确定字符是数字还是普通字符。如果字符是数字,它将被附加到字符串数组的字段并查找下一个数字并执行相同的操作,直到一个字符不是数字;在这种情况下,它将寻找另一个数字并重复相同的过程,但使用下一个字符串数组的字段。一旦程序通过了孔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)

【问题讨论】:

  • System.out.print(numbers[0] + ", ") - 很确定你的意思是 numbers[c]
  • “我已经在上一个问题中解释了我的程序应该做什么,所以这里有一个链接。”请让每个问题独立 - 并将问题中的代码缩减为 minimal reproducible example,其中包含与此问题相关的代码。
  • Jon Skeet:问题是我不知道为什么会出现这个问题,我不知道代码的哪一部分是相关的。无论如何,感谢您的第一个提示!
  • 爱德华·彼得斯:是的,你是对的。对不起。
  • @JavaNoob 请将初始化代码放入问题中好吗?我看不到。

标签: java arrays string sorting integer


【解决方案1】:

您正在为第一个n-1 次打印第一个数字:

if(c != numbers.length - 1){
    System.out.print(numbers[0] + ", "); <-- this
}else System.out.println(numbers[c] + ".");

你应该把它改成numbers[c]

【讨论】:

    【解决方案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

    您稍后会尝试通过执行numbers[b] = Integer.parseInt(numString[b]); 将其解析为Integer。这将引发异常!

    此外,您应该重新评估循环条件。你不应该从while(i &lt; (input.length() - 1))中的input.length()中减去a吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 2021-11-14
      • 1970-01-01
      • 2014-04-25
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多