第十次作业

发布时间 2023-06-19 17:34:13作者: 晓宇落

1.输入6位密码,再次输入密码,如果不够6位,提示,位数不对,如果两次不一致,提示两次密码不一致。

package zuoye10;

import java.util.Scanner;

public class one {
    //1.输入6位密码,再次输入密码,如果不够6位,提示,位数不对,如果两次不一致,提示两次密码不一致。
    public static void pw() {
        Scanner input = new Scanner(System.in);
        String pass;
        String repass;
        System.out.println("请输入6位密码");
        pass = input.next();
        testfun_1(pass);
        System.out.println("请重新输入密码");
        repass = input.next();
        testfun_1(repass);
        testfun_2(pass, repass);
    }

    public static void testfun_2(String test, String retest) {
        if (!test.equals(retest)) {
            System.out.println("密码不一致");
            System.exit(0);
        }
    }

    public static void testfun_1(String test) {
        if (test.length() != 6) {
            System.out.println("位数不对");
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        pw();
    }

}

 2.输入一个字符串,判断里面数字、大写字母、小写字母,其他字符的个数

package zuoye10;

import java.util.Scanner;

public class two {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int math = 0;
        int wordup = 0;
        int worddo = 0;
        int other = 0;
        System.out.println("请输入字符串");
        String test = input.next();

        for (char i : test.toCharArray()) {
            if (i <= 57 && i >= 48) {
                math++;
            } else if (i <= 90 && i >= 65) {
                wordup++;
            } else if (i <= 122 & i >= 97) {
                worddo++;
            } else {
                other++;
            }
        }
        System.out.println("数字有:" + math + "个");
        System.out.println("大写字母有:" + wordup + "个");
        System.out.println("小写字母有:" + worddo + "个");
        System.out.println("其他字符有:" + other + "个");
    }
}

 3.输入一个字符串,如果开头是ok并且包含no,那么输入错误。

package zuoye10;

import java.util.Scanner;

public class three {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符串");
        String test = input.next();
        if (test.startsWith("ok") || test.contains("no")) {
            System.out.println("输入错误");
        }
    }
}

 4.输入三个单词,组合成pascal命名法的字符串。(选做)

package zuoye10;

public class four {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s_1 = "STU";
        String string_1 = s_1.substring(0, 1);
        String string_2 = s_1.substring(1, 3).toLowerCase();
        String s_2 = "manage";
        String string_3 = s_2.substring(0, 1).toUpperCase();
        String string_4 = s_2.substring(1, 6);
        String s_3 = "system";
        String string_5 = s_3.substring(0, 1).toUpperCase();
        String string_6 = s_3.substring(1, 6);
        System.out.println(string_1 + string_2 + string_3 + string_4 + string_5 + string_6);

    }
}

 

5、利用接口实现动态的创建对象[选做题]

• 5.1 创建4个类:苹果、香蕉、葡萄、园丁  

• 5.2 在三种水果的构造方法中打印一句话.以苹果类为例

package zuoye10;

public class apple implements fruit {
    public apple() {
        System.out.println("这是苹果类");
    }
}

package zuoye10;

public class banana implements fruit {
    public banana() {
        System.out.println("这是香蕉类");
    }
}

package zuoye10;

public class grape implements fruit {
    public grape() {
        System.out.println("这是葡萄类");
    }
}

package zuoye10;

public interface fruit {

}

package zuoye10;

import java.util.Scanner;

public class five {
    public fruit create()
    {
        String temp;
        fruit flag=null;
        Scanner input=new Scanner(System.in);
        System.out.println("请输入");
        temp=input.next();
        if(temp.equals("苹果"))
        {
            flag = new apple();
        }else if(temp.equals("香蕉"))
        {
            flag= new banana();
        }else if(temp.equals("葡萄"))
        {
            flag= new grape();
        }
        return flag;
    }
    public static void main(String[] args) {
        five test=new five();
        test.create();
    }
}