Java编程技术大全(上)cp4基础编程元素——运算符、表达式、语句与流程控制

  1. 编写程序,计算表达式“((12345679 * 9) > (97654321 * 3)) ? true : false ”的值。
boolean result = ((12345679 * 9) > (97654321 * 3)) ? true : false ;
System.out.println(result);
  1. 编写程序,实现生成一随机字母(a-z,A-Z),并输出。

参考链接:https://www.cnblogs.com/zhaoyan001/p/7677137.html

public class Main {
        //生成随机数字和字母,
        public String getStringRandom(int length) {
            Random random = new Random();
            String result = "";
            String lowOrCap = random.nextInt(2) % 2 == 0 ? "low" : "cap";  //随机生成一个数,相当于随机选择一种情况进行分类
            if (lowOrCap.equalsIgnoreCase("low")){
                int Temp = random.nextInt(26) + 65;
                result += result + (char)Temp;
            }else if (lowOrCap.equalsIgnoreCase("cap")){
                int Temp = random.nextInt(26) + 97;
                result += (char)Temp;
            }
            return result;
        }
        public static void  main(String[] args) {
            Main test = new Main();
            System.out.println(test.getStringRandom(1));
        }
}
  • 代码分析:
    random.nextInt(26);随机生成一个整数,范围[0,26)。
  1. 编写程序,实现产生(或输入)一随机字母(a-z,A-Z),转为大写形式,并输出。请分别使用三元运算和位运算实现。

参考链接:
https://www.cnblogs.com/xymqx/p/3734612.html


public class cp4_3 {
    //三目运算
    public void getCap_3(int a){
        if (a <= 90 || a >= 97){  //ASCII在 91到 96不是字母,需要过滤一下,不然会出现字符。
            int result = a >= 97 ? a-32:a;
                System.out.println("三元运算转换前" + (char) a);
                System.out.println("三元运算转换后" + (char) result);
        }
    }
    //位运算
    public void getCap_B(int a){
        if (a <= 90 || a >= 97){
            int result = a >= 97 ? a&0xdf:a;  // 与0xdf 进行与运算,小写变大写
            System.out.println("位运算转换前" + (char) a);
            System.out.println("位运算转换后" + (char) result);
        }
    }
    public static void main(String[] args) {
        Random random = new Random();
        int a = (int) (Math.random() * 58) + 65; //大写字母 65-90,小写字母 97-122 生成一个65到122之间的任意数值
        cp4_3 cp = new cp4_3();
        cp.getCap_3(a);
        cp.getCap_B(a);
    }
  • 代码分析:
    位运算中, 字母|0x20=小写字母字母&0xdf=大写字母
    因为以大写字母A为例,大写A(65)的二进制编码为100 0001,小写字母a(97)的二进制编码为110 0001,除了第5位之外其它位都是一样的,也就是说大小写是由第5位来区分的,第5位为1即为小写,第5位为0即为大写
    与运算中,两个二进制位上均为1,结果为1,有0则为0,因此当小写字母的二进制编码与223(1101 1111)进行与运算后,其他位置不变,第五位由0变为1,从而字母从小写变为大写。
    同理,在或运算中,两个二进制位上均为0,结果为0,有1则为1,大写字母与225(1111 1111)进行或运算后会变为小写字母。
  1. 编写程序,使用控制语句计算“1+2+3+…+100”的值。
public class Sum {
   public void forSum(){
        int sum = 0;
        for (int i = 0; i<=100;i++){
            sum += i;
        }
        System.out.println(sum);
    }
    public void doWhileSum(){
       int sum = 0;
       int i = 0;
       do {
           i++;
           sum += i;
       }while (i < 100);
       System.out.println(sum);
    }
    public void whileSum(){
       int sum = 0 , i = 0;
       while (i <= 100){
           sum += i;
           i++;
       }
       System.out.println(sum);
    }
    public static void main(String[] args) {
        Sum s= new Sum();
        Sum.forSum();
        Sum.doWhileSum();
        Sum.whileSum();
    }
}
  1. 编写程序,使程序产生1~12之间的某个整数(包括1和12),然后输出相应的月份的天数(注:2月按28天算)。
// 数组的方式
    public static void main(String[] args) {
        int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        Random random = new Random();

        int m = random.nextInt(13);
        System.out.println(m + "月有"+ month[m-1] + "天");

    }
  1. 编写程序,判断某一年是否是闰年。
public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = scan.nextInt();
        if (year % 4 == 0 && year % 100 != 0|| year % 400 == 0){
            System.out.println(year+"是闰年");
        }else {
            System.out.println(year+"不是闰年");
        }
    }
  • 代码分析:
    闰年的判断标准:
  1. 非世纪年能整除4且不能整除100。
  2. 世纪年(比如,1900)能整除400。

水平有限,如有错误,欢迎指正,谢谢!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值