随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

(说明一下,输入的时候开头不能有空格,可以在getOdevity()方法中捕获一下异常就不会停止程序了,就懒得重新改了)下面是使用了空格开头的格式。

 

 

说明:

改进加上了选项类:

  选项一:就是上一个案例中的,对输入的多个数字进行统计(可以统计多位数,只要是空格分开的就算一个数字)

  选项二:对输入的一串数字进行统计(只能统计个位数,因为这一串数字的长度就表示数字的个数)

首先是接口规范:

public interface IMenu {    // 规范菜单类
    public void choose();
}
public interface IInputNumber {     // 从键盘获取数据
    public String[] inputNumMore();
    public String[] inputNumOne();
}
public interface IGetOdevityMore {  // 获取奇偶数
    public int[] getOdevity(String[] inputStringArr);
}

然后是功能类:

public class GetOdevityMore implements IGetOdevityMore {
    @Override
    public int[] getOdevity(String[] inputStringArr) {    // 获取奇偶数个数的方法
        int[] res = new int[2]; // 表示奇偶数个数
        res[0] = 0; // 表示奇数个数
        res[1] = 0; // 表示偶数个数
        if (inputStringArr.length != 0) {
            int[] getIntNum = new int[inputStringArr.length];
            for (int i = 0; i < inputStringArr.length; i++) {   // 将传入的字符串类型的数字转换为int类型的数字并存储在数组中
                getIntNum[i] = Integer.parseInt(inputStringArr[i]);
            }
            for (int i : getIntNum) {
                if (i % 2 == 1) {   // 奇数加一
                    res[0]++;
                }
                if (i % 2 == 0) {   // 偶数加一
                    res[1]++;
                }
            }
        }else {
            System.out.println("数据为空!");
        }
        return res;
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class InputNumber implements IInputNumber {
    @Override
    public String[] inputNumMore() {
        BufferedReader inputString = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("请输入想要的数字,用空格隔开:");
        try {
            String input = inputString.readLine();
            if (input.matches("\\d+( +\\d+)+ *|(\\d+ +)+")) {  // 匹配数字
                return input.split("\\s+|\\s");  // 匹配空格
            } else if (input.matches("( +\\d+)+")) {
                System.out.println("错误!开头不能输入空格");
            } else {
                System.out.println("错误!请输入数字!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String[] inputNumOne() {
        BufferedReader inputString = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("请输入一串想要的数字:");
        try {
            String input = inputString.readLine();
            /*
            if (input.matches("\\d( \\d+ )+ *|(\\d+ )+\\d|( *\\d+)+|( *\\d+ )+ *")){
            1. 开头无空格,结尾有多个空格
            2. 开头结尾都无空格
            3. 开头有多个空格,结尾没有空格
            4. 开头结尾都有多个空格
            * */
            if (input.matches("\\d+ *")) {
                return input.split("");
            } else if (input.matches("( +\\d+)+")) {
                System.out.println("错误!开头不能输入空格");
            } else {
                System.out.println("错误!请输入数字!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }        return null;
    }

}
import java.util.Scanner;

public class Menu implements IMenu {
    public Menu() {
    }

    public void choose() {
        int[] res;
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.print("\n【1】多个数字统计奇偶数\n【2】一串数字统计奇偶数\n选择【1】或【2】:");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    res = new GetOdevityMore().getOdevity(new InputNumber().inputNumMore());
                    System.out.println("\n奇数:" + res[0] + "\t偶数:" + res[1]);
                    break;
                case 2:
                    res = new GetOdevityMore().getOdevity(new InputNumber().inputNumOne());
                    System.out.println("\n奇数:" + res[0] + "\t偶数:" + res[1]);
                    break;

            }
            System.out.print("\n是否继续重新输入并统计?\n【1】继续\n【任意数字】结束\n输入【1】或【任意数字】:");
            int reChoose = sc.nextInt();
            if (reChoose != 1) {
                flag = false;
                System.out.println("\n结束!");
            }
        }
    }
}

然后是工厂类进行菜单获取:

public class Factory {
    public static IMenu getIntance(){
        return new Menu();
    }
}

最后是客户端:

import java.util.Arrays;

public class MAIN {
    public static void main(String[] args) {
        Factory.getIntance().choose();
    }
}

结果输出:

 

posted on 2022-02-25 17:09  时间完全不够用啊  阅读(201)  评论(0编辑  收藏  举报