fck 发表于 2008-11-27 20:00

java的标准IO输入的简单问题

import java.io.*;

public class Echo {
      private static int n1,Time1;
      private static String name1;
      public static void selectA(int select) throws IOException {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(System.in));
                BufferedInputStream din = new BufferedInputStream(System.in);
                int s = select + 'a' - 1;
                switch((char)s) {
                        case 'a': {
                              System.out.println("Input the number:");
                              n1=din.read();
                              for(int i=1;i<=n1;i++) {
                                        System.out.print("The No."+i+" name:");
                                        name1=in.readLine();
                                        System.out.print(" and the time:");
                                        Time1=din.read();
                              }
                              in.close();
                              din.close();
                              call();break;
                     }
                }
      }
      
      public static void call() {
                System.out.println(n1 + " " + name1 + " " +Time1);
      }
      
      public static void main(String[] args) throws IOException {
                BufferedInputStream stdin = new BufferedInputStream(System.in);
                int s;
                System.out.print("input the number (1):");            
                s = stdin.read();
                selectA(s);   //为什么没有条用到这个函数?
                System.out.print("continue to input:");
                stdin.read();   //为什么我运行的时候就不能再继续在键盘输入呢?
                stdin.close();
      }
}


运行的输出是input the number(1):1
                     continue to input:
1是我输入的。。
还想问下重定向是什么意思。。
先谢谢楼下的朋友帮忙

dreamwalker 发表于 2008-11-27 20:04

JAVA的问题答不了你
重定向说说我的理解吧:
可以把输入输出当成一条不能分叉(即不可以同时由两个地方流水进来,也不可以向两个地方流水出去)的水管的两端,
重定向就是把这条水管的输入或输出重新设置

可以参考下UNIX管道方面的内容。

紫玄 发表于 2008-11-27 20:36

首先
selectA(s); 是执行了的 但是不满足swith里面的条件所以你看不到结果 这要检查你思路问题
你可以在selectA(int select)方法一开头打印一下传入的数字 再在执行switch前打印一下(char)s就会发现问题

第二
你运行开始时输入的是1其实不是 你输入的是"1"和"回车"两个
所以第二次读取的是你的回车
不信你可以再第二次的时候直接打印System.out.print(stdin.read()) ;
这时会输出13
就是回车的code了

fck 发表于 2008-11-28 13:20

case的条件一开始我用的是int,但是发现也是不能匹配,换成了char之后,不知道怎么把int转成char。。
看了下JDK,好像没有读入char的方法。。

[ 本帖最后由 fck 于 2008-11-28 13:21 编辑 ]

紫玄 发表于 2008-11-28 13:55

那可以看看
java.io.Console或者java.util.Scanner
页: [1]
查看完整版本: java的标准IO输入的简单问题