输入一个字符串,输入后统计有多少个偶数数字和奇数数字

编写这种IO程序,就不要再用System.in了,去使用Scanner扫描流
在这里两个程序都提供

用System.in遇到了一些困难,卡了一个多小时,很是恶心的小困难,主要是自己基础不牢
1.关于byte数组的有效长度的问题
2.关于System.in中输入结束后换行,换行符同时进入数据中的问题
3.正则表达式
4.String转为int数组的问题

package com;
import java.io.*;

public class T6 {

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

        InputStream input = System.in ;
        byte[] data = new byte[1024];

        System.out.println("请输入数字:");
        int len=input.read(data);//取得byte数组中有效数据的长度
        //这点很重要,如果不获取len则整个1024大小会读取到input中

        String str1 = new String(data,0,len,"UTF-8");//将byte数组转换为字符串

        String str = str1.replaceAll("(\r\n|\r|\n|\n\r)",""); //滤掉回车所占的两个位置
        //这是System.in不好的地方,所用Scanner吧

        judgeint(str);

        //String转为int数组
        int[] nub = new int[str.length()];
        for(int i=0;i < str.length();i++){
            //substring取出字符串中从i开始到i+1的字符,不包括i+1
            nub[i] =Integer.parseInt(str.trim().substring(i,i+1));
        }

        statistic(nub); 
    }
    //判断是否为全数字形式
    public static void judgeint(String s){
         String regex="^[0-9]*$";//正则表达式匹配全是数字
         if(s.trim().matches(regex)==true){
             System.out.println("匹配");
         }else{
             System.out.println("不匹配");
             System.exit(1);
         }   
    }
    //判断奇数偶数
    public static void statistic(int[] n){
        int even=0;
        int odd=0;
        for(int i=0;i<n.length;i++){
            if(n[i] % 2 == 0){
                if(n[i] == 0){

                }else{          
                    even++;
                }
            }else{
                odd++;
            }
        }
        System.out.println("输入的数据中偶数个数为:" + even);
        System.out.println("输入的数据中奇数个数为:" + odd);

    }

}

Scanner代码:

import java.util.Arrays;
import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n") ;
        String data = null ;                // 接收数据
        boolean flag = true ;               // 循环标记
        while(flag) {
            System.out.print("请输入一串数字:");
            if (scan.hasNext()) {
                data = scan.next().trim() ; // 接收数据
                if (data.matches("\\d+")) { // 由数字所组成
                    flag = false ;          // 循环结束
                } else {
                    System.out.println("输入数据不是数字,请重新输入!");
                }
            }
        }
        int oddCount = 0 ;                  // 奇数个数
        int evenCount = 0 ;                 // 偶数个数
        String result [] = data.split("") ; // 逐个拆分
        for (int x = 1 ; x < result.length ; x ++) {
            int temp = Integer.parseInt(result[x]) ;    // 取得每一个数字
            if (temp % 2 == 0) {            // 是偶数
                evenCount ++ ;
            } else {
                oddCount ++ ;
            }
        }
        System.out.println("奇数个数:" + oddCount);
        System.out.println("偶数个数:" + evenCount);
    }
}

猜你喜欢

转载自blog.csdn.net/guohaocan/article/details/81046093
今日推荐