工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2133|回复: 4

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

[复制链接]
发表于 2008-11-27 20:00 | 显示全部楼层 |阅读模式
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是我输入的。。
还想问下重定向是什么意思。。
先谢谢楼下的朋友帮忙
发表于 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了
回复

使用道具 举报

 楼主| 发表于 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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-20 18:09

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表