黑马程序员技术交流社区

标题: 转换流的问题 [打印本页]

作者: 我是小黑    时间: 2013-9-10 22:19
标题: 转换流的问题
我想利用转换流把,键盘录入的任意五个数字,找到最大最小值,可是没成功,附上代码,跪求指点




package ioe;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.xml.sax.InputSource;

/*
* 接受键盘上的任意五个数字,存入数组并且找出最大数和最小数,在打印出来
* //把键盘上输入的数字变为字符数组
* */
public class Array {
        public static void main(String[] args) {
                man();
        }

        public static void man() {
                BufferedReader br = null;
                BufferedWriter bw = null;
                char[] s = new char[6];
                System.out.println("请输入任意五个数字");
                try {
                        int mun = 0;

                        while (mun >= 5) {
                                // 接受读取,键盘上输入的内容,字符
                                br = new BufferedReader(new InputStreamReader(System.in));

                                // 输出流,输出到打印太上
                                bw = new BufferedWriter(new OutputStreamWriter(System.out));
                                // 把键盘上的转换为字符流,并且建立缓冲区

                                // int leng=null;//字符串

                                br.read(s);// 读取每行的字符创,存入s中

                                mun++;
                        }// 循环的括号
                } catch (IOException e) {
                }
                int tem = 0;
                int met = 0;
                for (int i = 0; i < s.length; i++) {
                        if (s[i] < s[++i]) {
                                tem = s[i];
                        } else {
                                met = s[++i];
                        }
                }
                try {
                        bw.wait();
                        bw.wait();
                } catch (InterruptedException e) {

                        e.printStackTrace();
                } finally {
                        try {
                                if (br != null)
                                        br.close();
                        } catch (IOException e) {
                                System.out.println("1");
                        }
                        try {
                                if (bw != null)
                                        bw.close();
                        } catch (IOException e) {
                                System.out.println("2");
                        }
                }
        }
}



作者: 常在河边走_    时间: 2013-9-10 22:23
把char数组转化为int数组看看
作者: 深知一生短暂    时间: 2013-9-11 01:40
本帖最后由 深知一生短暂 于 2013-9-11 01:55 编辑

额。。。你代码写得很乱。。。。。while循环内还不断新建流对象。。。。。。。。
我根据你的需求重新写了一遍。。。流对象的用法格式希望你熟记!!
代码粘上来怪怪的。。。那些数组in 【i】竟然全没了,详细你还是对比 我的截图吧。。。能下载。。。
import java.io.*;
import java.util.Arrays;
public class fiveNumber {
        public static void main(String[] args)throws IOException {
                // TODO Auto-generated method stub
                BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));  //输入转换流,用BufferedReader包装

                String line=null;
                int[] in=new int[5];
                int i=0;
                while(i<5&&(line=bufr.readLine())!=null)
                {        
                        try{
                        in【i】=Integer.parseInt(line);
                        i++;
                        }
                        catch(NumberFormatException e){                                //这里抛异常你先不用管,假如看不懂        
                                if(!line.matches("[0-9]+"))                                
                                                System.out.println("出现非数字字符");
                                else
                                {
                                        System.out.println("数字太大");
                                }
                        }                                
                }

                System.out.println(Arrays.toString(in));
                System.out.println("最大值:"+max(in));
                System.out.println("最小值:"+min(in));               
        }        
        public static int min(int[] in)   //求最大值函数
        {
                int min=in[0];
                for(int i=1;i<in.length;i++)
                {
                        if(min>in)min=in
【i】;
                }
                return min;
        }        
        public static int max(int[] in)   //求最小值函数
        {
                int max=in[0];
                for(int i=1;i<in.length;i++)
                {
                        if(max<in)max=in
【i】;
                }
                return max;
        }

}


五个数字输入.jpg (44.16 KB, 下载次数: 9)

你的需求

你的需求

运行结果.jpg (45.88 KB, 下载次数: 11)

你的需求的运行结果

你的需求的运行结果

作者: 深知一生短暂    时间: 2013-9-11 11:26
楼主,有问题就再问问,如果没有问题就把问题改为已解决吧




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2