日期:2014-05-20  浏览次数:20786 次

关于java输入
要实现排序功能,但发现不知道怎么输入若干任意整数到一数组上并打印出来.,然后我要对数组进行操作.具体代码求,别笑我


------解决方案--------------------
帮楼主做了一个,并加上了错误处理:

import java.util.Arrays;
import java.util.Scanner;

public class Test {

  public static void main(String[] args){    

    Scanner sc = new Scanner(System.in);
    System.out.print( "请输入需要排序数的个数: ");
    int k = 0;
    try {
      k = sc.nextInt();
    }catch(Exception e){
      System.out.println( " ** 输入错误,请重新运行 ** ");
    }
    
    int[] num = new int[k];
    for(int i=0; i <num.length; i++) {
      System.out.printf( "请输入%d个数: ", i+1);
      try {
        num[i] = sc.nextInt();
      }catch(Exception e){
        System.out.println( " ** 输入错误,请重新输入 ** ");
        sc.next();
        --i;
      }
    }

    // 使用 Java 内置的方法进行排序
    Arrays.sort(num);
    for(int i=0; i <num.length; i++){
      System.out.println(num[i]);
    }
  }
}