(1)怎样读取从键盘输入的int型数值,在存入一个数组里面呢?
(2)假设输入一行,回车,输入新的一行,因为运行也是回车,怎样让编译器识别出有没有输入完呢?

解决方案 »

  1.   

    int i[];
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
    String input = br.readLine();
    String tmp = input.split(" +");
    i = new int[tmp.length];
    for(int j=0; j<i.length; j++)
        i[j] = Integer.parseInt(tmp[i]);
    }catch...直接一行输入不就行了,以空格隔开,或者你要一个一个输的话来个for循环一下,个数到了退出继续执行程序不就行了
      

  2.   

    2第二个问题,我觉得你何必那么写呢,用空格隔开不就行了,就像楼上的所说的!实在你要那么做,那就得多写一条输入语句了!
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
    String s,s1;
    s=in.readLine();
    s1=in2.readLine();
    System.out.println(s);
    System.out.println(s1);
      

  3.   

    1.
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();
      

  4.   

    非常感谢,但是第一个程序我不太明白split的用法,而且调试也有错误,可不可以写得详细些呢?
      

  5.   

    第一个程序要稍微修改一下,spilt之后是数组,String tmp[] = input.split(" +");
    split就是把字符串分割放到数组里,参数是分隔符,这里的" +"是一个正则表达式,表示1个或多个空格(+表示一个或多个,前面有个空格就表示一个或多个空格了)
      

  6.   

    i[j] = Integer.parseInt(tmp[i]);
    改成
    i[j] = Integer.parseInt(tmp[j]);
      

  7.   

    split的用法看一下jdk文档.它还有举例子的.
    它里面的参数是个正则表达式.
    public String[] split(String regex)The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result 
    : { "boo", "and", "foo" } 
    o { "b", "", ":and:f" } 
      

  8.   

    多谢believefym(feng),你改后的程序没问题,但是我写了一个跟它很相似的方法,却有错,可不可以帮我查一下?
    我想指定一个i,从一行int中找出第i个值返回:
    public static Integer[] readAInt(int i)    
       {                                      
          try{        
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String input = br.readLine();
            String[] tmp = input.split(" +");
            int[] a = new int[tmp.length];
            a[i] = Integer.parseInt(tmp[i]);                
            }catch(IOException e)
             {
              System.out.println("read a integer has some problem!");
             }
            return a; 
        }
      

  9.   

    public static int[] readAInt() {
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(
    System.in));
    String input = br.readLine();
    String[] tmp = input.split(" +");
    int[] a = new int[tmp.length];
    for (int i = 0; i < a.length; i++)
    a[i] = Integer.parseInt(tmp[i]);
    return a;
    } catch (IOException e) {
    System.out.println("read a integer has some problem!");
    return null;
    }
    // return a;
    }
      

  10.   

    你的a是int[],但函数返回的是Integer[]
    一个是基本类型,一个是类
      

  11.   

    你要返回一个值输出就不要返回一个数组了,直接输出arrayName[index]就行了 public static int readAInt(int j) {
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(
    System.in));
    String input = br.readLine();
    String[] tmp = input.trim().split(" +");
    int[] a = new int[tmp.length];
    for (int i = 0; i < a.length; i++)
    a[i] = Integer.parseInt(tmp[i]);
    return a[j];
    } catch (IOException ioe) {
    System.out.println("read a integer has some problem!");
    return -1;
    }catch(Exception e){
    e.printStackTrace();
    return -1;
    }
    // return a;
    }