Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组:将数字分成独立的、排列的数字,输出问题_Java_Arrays_Sorting_Output - Fatal编程技术网

Java数组:将数字分成独立的、排列的数字,输出问题

Java数组:将数字分成独立的、排列的数字,输出问题,java,arrays,sorting,output,Java,Arrays,Sorting,Output,我正在尝试创建一个代码,其中包含如下数字: "787191230" 并将此返回: "0 1 1 2 3 7 7 8 9" 现在,我的错误是,我的代码以某种方式正确地打印出了第一个,然后开始在一个模式中添加大量的0。每次增加的0的数量似乎一直在下降,但这还是很烦人的 我的主代码(完成所有操作的地方): 最后,但并非最不重要的是:输出: 1 5 6 7 8 9 0 0 0 0 0 0 0 1 1 2 4 6 8 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2

我正在尝试创建一个代码,其中包含如下数字:

"787191230"
并将此返回:

"0 1 1 2 3 7 7 8 9"
现在,我的错误是,我的代码以某种方式正确地打印出了第一个,然后开始在一个模式中添加大量的0。每次增加的0的数量似乎一直在下降,但这还是很烦人的

我的主代码(完成所有操作的地方):

最后,但并非最不重要的是:输出:

1 5 6 7 8 9 
0 0 0 0 0 0 0 1 1 2 4 6 8 9 9 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 7 7 8 8 9 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 3 4 4 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 6 8 9 

任何帮助,主要是暗示,都会让我非常感激!我不知道这是怎么回事!提前感谢。

您将count作为NumberSorter的静态成员,可能应该将其作为方法作用域变量保留。

您将count作为NumberSorter的静态成员,可能应该将其作为方法作用域变量保留。

您的
getNumDigits
行为不正确

static int count = 0;
private static int getNumDigits(int number)
{
    while(number>0){
        number=number/10;
        count++;
    }
    return count;
}
你为什么一直在周围数数?只需声明一个局部变量:

private static int getNumDigits(int number)
{
    int count = 0;
    while(number>0){
        number=number/10;
        count++;
    }
    return count;
}

您的
getNumDigits
行为不正确

static int count = 0;
private static int getNumDigits(int number)
{
    while(number>0){
        number=number/10;
        count++;
    }
    return count;
}
你为什么一直在周围数数?只需声明一个局部变量:

private static int getNumDigits(int number)
{
    int count = 0;
    while(number>0){
        number=number/10;
        count++;
    }
    return count;
}

Yopu已将
count
声明为静态,但您从未重置它,因此对于每个新测试,
count
从上一个测试的最后一个值开始

它不需要是静态的或可广泛访问的,所以只需将其移动到
getNumDigits()
函数中即可

试试这个:

private static int getNumDigits(int number)
{
    int count = 0;
    while(number>0){

            number=number/10;
        count++;
    }
    return count;
}

Yopu已将
count
声明为静态,但您从未重置它,因此对于每个新测试,
count
从上一个测试的最后一个值开始

它不需要是静态的或可广泛访问的,所以只需将其移动到
getNumDigits()
函数中即可

试试这个:

private static int getNumDigits(int number)
{
    int count = 0;
    while(number>0){

            number=number/10;
        count++;
    }
    return count;
}

尝试以下简单代码:

public static void main(String args[])
{
    int[] cases = {567891, 901912468, 864213507, 898777, 234422, 29826};
    for( int test : cases )
    {
       String numStr=String.valueOf(test);
       char[] digits=numStr.toCharArray();
        Arrays.sort(digits);
       String sorted=new String(digits);

        for (int i = 0; i < sorted.length(); i++) {
            System.out.print(" "+sorted.charAt(i));                
        }
        System.out.println();
    }
}

尝试以下简单代码:

public static void main(String args[])
{
    int[] cases = {567891, 901912468, 864213507, 898777, 234422, 29826};
    for( int test : cases )
    {
       String numStr=String.valueOf(test);
       char[] digits=numStr.toCharArray();
        Arrays.sort(digits);
       String sorted=new String(digits);

        for (int i = 0; i < sorted.length(); i++) {
            System.out.print(" "+sorted.charAt(i));                
        }
        System.out.println();
    }
}

非常感谢你!我不知道静态计数是如何让java垃圾邮件0攻击我的,但它确实解决了我的问题!谢谢!非常感谢你!我不知道静态计数是如何让java垃圾邮件0攻击我的,但它确实解决了我的问题!谢谢!