A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨芳 中级黑马   /  2013-2-2 14:01  /  1308 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 2013-2-4 21:58 编辑

/*
        读取键盘录入。
        System.out:对应是标准输出设备。控制台
        System.in:对应是标准输入设备。键盘。


        需求:
        通过键盘录入数据。
        当录入一行数据后,就将该行数据进行打印,
        如果录入的数据是over,那么停止录入。
*/
import java.io.*;
class ReadIn
{
        public static void main(String[] args) throws IOException
        {
                InputStream in=System.in;

                //int by=in.read();

                //System.out.println(by);
                /*
                int ch=0;
               
                while((ch=in.read())!=-1)
                {
                        System.out.println(ch);
                }
                */
                StringBuilder sb=new StringBuilder();
                while(true)
                {
                        int ch=in.read();

                        sb.append((char)ch);
                        if(ch=='\r')
                        continue;
                        if(ch=='\n')
                        {
                                String s=sb.toString();
                                if("over".equals(s))
                                
                                        break;
                                
                                       
                                                
                                System.out.println(s.toUpperCase());
                                //sb=new StringBuider();
                                sb.delete(0,sb.length());
                        }
                        else
                                 sb.append((char)ch);
                        
                }
                in.close();
        }
}

输入“Over"停不下来????

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

5 个回复

倒序浏览
package cn.itcast.day1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Over {

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
                BufferedReader bufr =
                        new BufferedReader(new InputStreamReader(System.in));
                String line;
                while((line=bufr.readLine())!=null)
                {
                        if("over".equals(line))
                                break;
                        System.out.println(line);
                }
                bufr.close();
        }

}
这样写就OK了
你这个程序在判断over的时候只是跳出了if,外面还有while(true)怎么停的下来
回复 使用道具 举报
while(true)
              {
                      int ch=in.read();
                     
                     sb.append((char)ch);
                      if(ch=='\r')
                      continue;
                      if(ch=='\n')
                      {
                              String s=sb.toString();
                              //System.out.println(s);
                              if("over\r\n".equals(s){
                            //   System.out.println("hhhhhhh");
                               break;
                              }
                                                                                 
                             System.out.println(s.toUpperCase());
                              //sb=new StringBuider();
                              sb.delete(0,sb.length());
                      }
                     // else
                              // sb.append((char)ch);
                     
              }
              in.close();
找了好久才找到错误,因为的你的字符串S中连回车符搞进去了,所以判断的时候,l是不想等的。这样写  if("over\r\n".equals(s){
就可以退出了。
回复 使用道具 举报
public static void main(String[] args) throws IOException
         {
                 InputStream in=System.in;
                 //int by=in.read();
                 //System.out.println(by);
                 /*
                 int ch=0;
                 
                 while((ch=in.read())!=-1)
                 {
                         System.out.println(ch);
                 }
                 */
                 StringBuilder sb=new StringBuilder();
                 while(true)
                 {
                         int ch=in.read();
                     //    sb.append((char)ch); 将这句话去掉就可以了,只是在下面添加,在上面添加会吧回车见加进去。
                         if(ch=='\r')
                         continue;
                         if(ch=='\n')
                         {
                                 String s=sb.toString();
                                 if("over".equals(s))
                                 
                                         break;
                                 
                                         
                                                
                                 System.out.println(s.toUpperCase());
                                 //sb=new StringBuider();
                                 sb.delete(0,sb.length());
                         }
                         else
                                  sb.append((char)ch);
                        
                 }
                 in.close();
         }
}

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
你可以用BufferedReader bufr = new BufferedReader(new InputStreamReader(System.out));
这样可以直接用bufr.readline();读取到的是字符串。
这样就不用考虑回车符咯
还有,跳出到最外层,可以给循环做个标记 比如 A:后面就是循环
然后在需要跳出的时候 break A;
回复 使用道具 举报
上面的答得不错顶个
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马