按降序反转

时间:2015-03-24 12:29:32

标签: java

对于用户给定的字符集,只选择整数并按降序对它们进行排序,并将它们放在其位置,使其他字符的位置保持不变。

因此,如果用户输入字符串和符号,则字符串和符号的位置应保持在相同位置,并且只有整数应按降序排列,而且我只能按降序维护。

示例输入

2,a,5,3,#,11,e

输出11,a,5,3,#,2,e

public class sort {

public static void main(String[] args) {
    int num, i, j, temp;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of character to be sort:");
    num = input.nextInt();

    int array[] = new int[num];
    System.out.println("Enter " + num + " character ");

    for (i = 0; i < num; i++) {
        array[i] = input.nextInt();
    }

    for (i = 0; i < (num - 1); i++) {
        for (j = 0; j < num - i - 1; j++) {
            if (array[j] < array[j + 1]) {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

    System.out.println("Sorted list of integers:");

    for (i = 0; i < num; i++) {
        System.out.println(array[i]);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

为了能够将符号和整数作为输入,您应该扫描字符串,而不是整数。然后使用Integer.parseInt(s)将sting转换为整数,其中s是一个字符串。

相关问题