| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 650 人关注过本帖
标题:想知道如何轻松的输入输出
只看楼主 加入收藏
黄悦泽
Rank: 1
来 自:广东省河源市龙川县
等 级:新手上路
帖 子:2
专家分:4
注 册:2014-4-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
想知道如何轻松的输入输出
/*import

public class Xiti1 {

    *//**
     * @param args
     * @throws IOException
     *//*
    public static void main(String[] args) throws IOException {
        
        byte[] a = new byte[1000];
        int count = System.in.read(a);
        
        for (int i = 0; i < count; i++) {
            if (a[i] <= 'z' && a[i] >= 'a')
                a[i] = (byte) (a[i] - 32);
            else
                a[i] = (byte) (a[i] + 32);
        }
        
        for (int i = 0; i < count; i++) {
            System.out.print((char) a[i] + " ");
        }
        System.out.println();
        
        for (int i = 0; i < count; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();

    }

}
// 将一个字符串中的小写字母转换成大写字母,并将大写字母转换成小写字母。
问:使用read()输入字符串后,最后按enter键,所以count总会比输入的字符串要多出两个字节来保存enter键,但是我的程序最后得出的结果最后输出多出来的不是enter,而是- *,这是为什么?
还有就是我想知道还有其他方式输入字符串吗?比如stringbuffer,sting 之类的!求教!!谢谢了!
搜索更多相关主题的帖子: 如何 count import public 
2014-04-17 11:05
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:10 
下面是楼主原来的代码(为了通过编译做了小量修改)

程序代码:
import public class Xiti1 {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        byte[] a = new byte[1000];
        int count = System.in.read(a);

        for (int i = 0; i < count; i++) {
            if (a[i] <= 'z' && a[i] >= 'a')
                a[i] = (byte) (a[i] - 32);
            else
                a[i] = (byte) (a[i] + 32);
        }

        for (int i = 0; i < count; i++) {
            System.out.print((char) a[i] + " ");
        }
        System.out.println();

        for (int i = 0; i < count; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();

    }

}


输入:
AbC

输出:
a B c - *
97 66 99 45 42

问题点:
字母的大小写转换成功完成了
输出了多余的[- *]
2014-04-19 21:44
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
下面是修改后的代码:

程序代码:
import *;

public class Xiti2 {

    public static void main(String[] args) throws IOException {

        byte[] a = new byte[1000];
        int count = System.in.read(a);

        for (int i = 0; i < count; i++) {
            if ('a' <= a[i] && a[i] <= 'z') {
                a[i] = (byte) (a[i] - 32);
            } else if ('A' <= a[i] && a[i] <= 'Z') {
                a[i] = (byte) (a[i] + 32);
            }
        }

        for (int i = 0; i < count; i++) {
            System.out.print((char) a[i] + " ");
        }
        System.out.println();

        for (int i = 0; i < count; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();

    }

}


输入:
AbC

输出:
a B c
 
 
97 66 99 13 10

问题点:
在输出的字母和数字之间多了一些空行出来

原因:
查看输出的最后那两个数字为13和10
分别为回车键CR和换行键LF的ASCII码

再回头查看原来的输出和现在的输出的差别,就能知道
多余的[- *]是回车和换行的ASCII码+32之后的结果

参考:
http://baike.baidu.com/link?url=xU42szg6dkxh0rKNBSSlHtvn4Vh2nvP5tj6JqjE0tJAI5bCerDvV4vFhXd0-ZLnj
2014-04-19 21:49
xixiqiqi
Rank: 2
等 级:论坛游民
帖 子:22
专家分:71
注 册:2013-10-10
收藏
得分:10 
关于第1个问题产生的原因有两个:
第一,读字符用字符流读比较恰当,以上代码中的字节流读法把回车也读进去了
第二,之所以回车变成了- *是因为代码中判断字母逻辑上不正确
if (a[i] <= 'z' && a[i] >= 'a')  
  a[i] = (byte) (a[i] - 32);
else                               //  除了小写a~小写z外并不都是大写字母
  a[i] = (byte) (a[i] + 32);       //  回车-'r''n','r'+32等于-,'n'+32等于*

关于第2个问题,方法有很多:
比如:
char[] a = new char[100];
InputStreamReader reader = new InputStreamReader(System.in) ;
int count = reader.read(a) ;
比如:
InputStreamReader reader = new InputStreamReader(System.in) ;      
BufferedReader br = new BufferedReader(reader) ;
String data = br.readLine();
char[] a = data.toCharArray() ;
比如:
Scanner scan = new Scanner(System.in) ;
String data = scan.next() ;
char[] a = data.toCharArray() ;
2014-04-19 21:51
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
就我个人而言,是比较喜欢使用下面的形式获得输入结果的,如果感兴趣可以试一下。

String InputString = (new Scanner(System.in)).next();
2014-04-19 21:52
快速回复:想知道如何轻松的输入输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014298 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved