Java语言实现数组的键盘输入(综合几种方法实现)

来源:互联网 发布:java怎么引入包 编辑:程序博客网 时间:2024/06/05 23:58
package com.suanfa;
import java.io.*;
import java.util.Scanner;
//public class Sort_1 {
// /**利用字节输入流来输入数据
//  * DataKeyboardInput2.java
//  * @param args
//  */
// public static void main(String[] args) {
//  // TODO Auto-generated method stub
//        byte buf[]=new byte[10];   //字节数组,输入为10个
//        String str;
//        int num = 0;
//        try{
//         //把数据读入到字节数组中
//         System.in.read(buf);
//         //利用字节数组创建字符串
//         str=new String(buf,0);
//         //把字符串数据转换为整型数据
//         num=Integer.parseInt(str.trim());
//        }catch(Exception e){
//         System.out.print(e);
//        }System.out.println(num);
// }
//}
public class Sort_1{
 
 /**几种整数的输入方法融合
  * @author yintao
  */
 public static void main(String[] args) throws IOException {
//以下为输入数组长度
 //所用方法为文本扫描
 Scanner input=new Scanner(System.in);
       int N=input.nextInt();
         String str="";
         int[] a=new int[N];
         System.out.println("输入的数组的长度为"+a.length);
         //以下实现的是数组的输入
         //所用方法 利用字符输入流
         int num = 0;
        for(int i=0;i<a.length;i++){ 
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         try{
          str=br.readLine();//读取读取文本行
          num=Integer.parseInt(str);// 将字符串参数作为有符号的十进制整数进行解析
         }catch(IOException e){
          System.out.println(e.toString());
         }
         a[i]=num;
        }
        for(int i=0;i<a.length;i++){ 
        System.out.print(a[i]+" ");}
 }
}