具有恒定位置的字符串的降序

时间:2015-04-10 03:36:54

标签: java arrays sorting

我需要从用户那里获取输入,如果用户输入字符串,程序应该按降序保持整数,并且字符串的位置应该处于恒定位置。我无法按降序保留整数。

public class sort {

    public static void main(String[] args) {
        int num;
        int i, j, temp;

        String nu;
        int y;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the number of character to be sort:");
        String num = input.nextLine();

        int length = num.length();
        String result = "";
        for (i = 0; i < length; i++) {
            Character character = num.charAt(i);
            if (Character.isDigit(character)) {
                result += character;
            }
        }

        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]);
    }
} 

2 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些可能有所帮助的修改。首先,Integer.ParseInt比nextInt()效果更好,因为nextInt()在nextLine()上留下回车符。此外,我觉得while循环可以更好地用于冒泡排序算法。

import java.util.*;
public class sort {

public static void main(String[] args) {
    int num;
    int i, temp;

    String nu;
    int y;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of character to be sort:");
    num = Integer.ParseInt(input.nextLine());

    int[] arr = new int[num];
    System.out.println("Enter " + num + " character ");
    for (i = 0; i < num; i++)
        arr[i] = Integer.ParseInt(input.nextLine());

    int swaps;
    while (swaps == 0) {
        swaps = 0;
        for (i = 0; i < arr.Length; i++) {
            if (arr[j] < arr[i + 1]) {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
                swaps++;
            }
        }
    }

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

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

}

答案 1 :(得分:0)

我改变了一些事情。首先,你声明了num两次,并且有一个不匹配的String = input.nextInt()。 try ... catch检查它是否为int输入,否则将其从char转换为int。你按降序排列它们的算法工作正常,但填充数组是不行的。

import java.util.InputMismatchException;
import java.util.Scanner;
public class sort {


public static void main(String []args) {

int i, j, temp;


int y;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of character to be sort:");
String  num = input.nextLine();   


int length = Integer.parseInt(num);
String result = "";


int array[] = new int[length]; 
System.out.println("Enter " + num + " character ");
for (i = 0; i < length; i++) {
  try{
    array[i]=input.nextInt();
  }catch(InputMismatchException e){
      Character c = input.next().charAt(0);
      array[i]=(int)(c.charValue());
  }
}

for (i = 0; i < ( length- 1 ); i++) {
  for (j = 0; j < length - 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 < length; i++) 
    System.out.println(array[i]);
}
}