java学习第二周(1)

java学习第二周(1)

模拟计算器

Java程序最初版本
 public static void main(String[] args) {
        //模拟简单计算器,可以运算+,—,*,/,%。
        //
        //- 接收三个参数,一个整数,一个运算符,另一个整数。
        //- 计算出运算结果。
        //- 无法运算时,返回null。
        int i=1;
        while (i!=0){
            Scanner scanner2=new Scanner(System.in);
            System.out.println("是否进行运算,1进行,0退出");
            i=scanner2.nextInt();
            if (i==1){
                Scanner scanner=new Scanner(System.in);
                System.out.println("请输入第一个数字");
                int num1=scanner.nextInt();

               System.out.println("请输入第二个数字");
               int num2=scanner.nextInt();

               Scanner scanner1=new Scanner(System.in);
               System.out.println("请输入运算符");
               String fu=scanner1.nextLine();

               if (fu.indexOf("+")!=-1){
                   num1=num1+num2;
                   System.out.println(num1);
               }
               else if (fu.indexOf("-")!=-1){
                   num1=num1-num2;
                   System.out.println(num1);
               }
               else if (fu.indexOf("*")!=-1){
                   num1=num1*num2;
                   System.out.println(num1);
               }
               else if (fu.indexOf("/")!=-1){
                   if (num2==0){
                       System.out.println("error");
                   }
                   else {
                      num1=num1/num2;
                       System.out.println(num1);
                   }
               }
               else if (fu.indexOf("%")!=-1){
                   num1=num1%num2;
                   System.out.println(num1);
               }
               else
                   System.out.println("null");
        }
            }
        }
Java程序改进版
 public static void main(String[] args) {
        /*
            模拟简单计算器,可以运算+,—,*,/,%。
            - 接收三个参数,一个整数,一个运算符,另一个整数。
            - 计算出运算结果。
            - 无法运算时,返回null。
         */
        Scanner scanner=new Scanner(System.in);
        int a=scanner.nextInt();
        String b=scanner.next();
        int c=scanner.nextInt();
        String count=count(a,b,c);
        System.out.println(a+b+c+"="+count);
    }

    private static String count(int a, String b, int c) {
        int count=0;
        if ("+".equals(b)){
            count=a+c;
        }
        else if ("-".equals(b)){
            count=a-c;
        }
        else if ("*".equals(b)){
            count=a*c;
        }
        else if ("%".equals(b)){
            count = a % c;
        }
        else{
            return null;
        }
        return count+"";
    }

密码检测

public static void main(String[] args) {
        //校验密码是否合法。
        //
        //- 必须至少8个字符。
        //- 必须至少2个大写字符。
        //- 必须只有字母和数字。
        int num=0;
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入密码");
        String pass=scanner.nextLine();
        char[] password=pass.toCharArray();
        if (password.length>=8){
            for (int i=0;i<password.length;i++)
            {
                for (int j=0;j<password.length;j++){
                    if ((password)[i]>=65&&(password)[i]<=90){
                        num++;
                    }
                }
                if (num<2){
                    System.out.println("密码不合法,大写字母太少");
                    break;
                }
                if ((password)[i]<48||((password)[i]>57&&(password)[i]<65)||((password)[i]>90&&(password)[i]<97)||(password)[i]>122){
                    System.out.println("密码不合法,有非法字符");
                    break;
                }
            }
        }
        else
            System.out.println("密码不合法,长度不够");
        System.out.println("密码合法");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值