Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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_Bubble Sort - Fatal编程技术网

在java中,将用户输入从高到低排序会给出错误的答案

在java中,将用户输入从高到低排序会给出错误的答案,java,bubble-sort,Java,Bubble Sort,下面是我的程序,它接受用户输入,然后从高到低排序: 此程序将用户的输入作为整数,然后将其转换为字符串,然后将其传递给排序(int[]sort)方法 public class SortLtoS { void convert(int n) // converts int to string { String temp = Integer.toString(n); int [] sort = new int[temp.length()];

下面是我的程序,它接受用户输入,然后从高到低排序:

此程序将用户的输入作为
整数
,然后将其转换为
字符串
,然后将其传递给
排序(int[]sort)
方法

public class SortLtoS { 

void convert(int n)                     // converts int to string
{
    String temp = Integer.toString(n);
    int [] sort = new int[temp.length()];
    for(int i=0;i<temp.length();i++)
    {
        sort[i] = temp.charAt(i)-0; 

    }

    sort(sort);
}
void sort(int[] sort)           // bubble sort the integer array
{

    int temp=0;         
    for(int j=0;j<sort.length;j++)
    {
        for(int k=1;k<(sort.length-j);k++)
        {
            if(sort[k-1]>sort[k])
                temp=sort[k-1];
            sort[k-1]=sort[k];
            sort[k]=temp;
        }

        for(int i=sort.length-1;i>=0;i--)
            System.out.println(sort[i]);
    }


}


public static void main(String[] args) {

    System.out.println("Enter the number to sort");
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    SortLtoS t = new SortLtoS();
    t.convert(a);


  }

 }
公共类排序{
void convert(int n)//将int转换为字符串
{
字符串温度=整数。toString(n);
int[]排序=新int[temp.length()];

对于(inti=0;i您想要的不是
sort[i]=temp.charAt(i)-0;
,它应该是
sort[i]=temp.charAt(i)-“0”;
,您想减去字符“0”的编码,而不是0的值。虽然我认为您不需要这一步,因为从main方法开始,您可以只扫描字符串,为什么要扫描int并将其转换为字符串?只是为了确保它是一个有效的数字

代码的另一个问题是,应该用
if
语句的大括号括住交换逻辑的所有三条语句。将数组
sort
的打印语句移出排序逻辑

您的
排序
函数应如下所示:

void sort(int[] sort) // bubble sort the integer array
{
    int temp = 0;
    for (int j = 0; j < sort.length; j++) {
        for (int k = 1; k < (sort.length - j); k++) {
            if (sort[k - 1] > sort[k]) {
                temp = sort[k - 1];
                sort[k - 1] = sort[k];
                sort[k] = temp;
            }
        }
    }

    for (int i = sort.length - 1; i >= 0; i--)
        System.out.println(sort[i]);

}
void排序(int[]sort)//对整数数组进行冒泡排序
{
内部温度=0;
for(int j=0;j排序[k]){
温度=排序[k-1];
排序[k-1]=排序[k];
排序[k]=温度;
}
}
}
对于(int i=sort.length-1;i>=0;i--)
System.out.println(sort[i]);
}

它通过使用收集框架来工作

public class SortLtoS  {

    public static void main(String[] args) {
        System.out.println("Enter the number to sort");
        Scanner sc = new Scanner(System.in);
        Integer number  = sc.nextInt();
        List<String> list = new ArrayList<String>();
        Integer rem;
        Integer n = 0;
        do {
            rem = number % 10;
            number = number / 10;

            list.add(rem.toString());
        } while (number > 0);

        Comparator comparator = Collections.reverseOrder();
        Collections.sort(list, comparator);
        String[] str = new String[list.size()];
        str = list.toArray(str);
        StringBuilder builder = new StringBuilder();

        for (String s : str) {
            builder.append(s);
        }
        System.out.println(builder.toString());
    }
}
公共类排序{
公共静态void main(字符串[]args){
System.out.println(“输入要排序的编号”);
扫描仪sc=新的扫描仪(System.in);
整数=sc.nextInt();
列表=新的ArrayList();
整数rem;
整数n=0;
做{
rem=数字%10;
数字=数字/10;
list.add(rem.toString());
}而(数量>0);
Comparator Comparator=Collections.reverseOrder();
集合。排序(列表、比较);
String[]str=新字符串[list.size()];
str=列表。toArray(str);
StringBuilder=新的StringBuilder();
for(字符串s:str){
建造商。附加;
}
System.out.println(builder.toString());
}
}