日期:2014-05-20  浏览次数:20670 次

数组
从键盘出入10个整数,合法值为1、2或者3.不是这三个数则为非法数字。试着编程统计每个整数和非法数字的个数。
请各位大虾们讲解一下,谢谢!

------解决方案--------------------
就是在这个数组中判断出1、2、3是合法数字而其他的这是不是
------解决方案--------------------
如要要简单一点的话,这样就可以了。
Java code

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] ins = new int[10];
        int count = 0;
        //循环输入10个数字
        while(true){
            if(count == ins.length){
                break;
            }
            try {
                int in = scan.nextInt();
                ins[count] = in;
                count++;
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        int one = 0;
        int two = 0;
        int three = 0;
        int other = 0;
        //循环查找合法数字
        for(int i = 0; i < ins.length; i++){
            if(ins[i] == 1){
                one++;
            }else if(ins[i] == 2){
                two++;
            }else if(ins[i] == 3){
                three++;
            }else{
                other++;
            }
        }
        System.out.println("1的个数:" + one);
        System.out.println("2的个数:" + two);
        System.out.println("3的个数:" + three);
        System.out.println("其他数字的个数:" + other);
    }

------解决方案--------------------
探讨

如要要简单一点的话,这样就可以了。
Java code

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] ins = new int[10];
int count = 0;
//循环输入10个数字
……