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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package com.itheima;

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

/**
* 5、 编写程序接收键盘输入的5个数,装入一个数组,并找出其最大数和最小数。
* @author mars
*/
public class Test5
{

        public static void main(String[] args) throws IOException
        {
                //提示输入数字
                System.out.println("请输入5个数字:");
                //读取键盘
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

                int[] z = new int[5];
                int count = 0;
                String line = null;
                while((line=bufr.readLine()) != null)//读一行
                {
                        if(isInt(line))//是否为数字
                        {
                                z[count++] = Integer.parseInt(line); //字符转成数字
                                if(count == 5)
                                        break;
                        }
                        else
                        {
                                System.out.println("输入不是数字,请重新输入:");//不是数字提示有误码,重有输入
                        }
                }
               
                //输出你所输入的数字
                for (int x=0; x<5; x++)
                {
                        System.out.print(z[x]+"  ");
                }
                System.out.println();
               
                getMaxMin(z);//找出最大值最小值
        }
       
        //最大值最小值
        public static void getMaxMin(int[] arr)
        {
                int max = 0;
                int min = 0;
                for (int x=0; x<arr.length; x++)
                {
                        if(arr[max]<arr[x])
                        {
                                max = x;
                        }
                        if(arr[min]>arr[x])
                        {
                                min = x;
                        }
                }
               
                System.out.println("最大数"+arr[max]);
                System.out.println("最小数"+arr[min]);
        }
       
        //判断是否为数字符串
        public static  boolean isInt(String str)
        {
                //字符串变为字符数组进行判断
                char[] arr = str.toCharArray();
               
                for (int x=0; x<arr.length; x++ )
                {
                        if((arr[x]>='0') && (arr[x]<='9'))
                        {}
                        else
                        {       
                                return false;       
                        }
                }
                return true;
        }
       

}

0 个回复

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