java2变量数据类型和运算符

 
复制代码
public class jh_11_加加减减运算符 {
    public static void main(String[] args) {
        int a = 5,b =2 ;
        
        a ++;// 对自身加1;
        System.out.println(a);
        
        b --;//对自身减1
        
        System.out.println(b);
        
        
        int count = 0;
        
        count ++;
        System.out.println(count);
        
        count ++;
        System.out.println(count);
        
        // 计数。
        
        // 1---100
//        ++
//        100 -- 1
//        100 -- 99 -- 98 --...
//        --
        
        
        
        
    }
}
复制代码
复制代码
public class jh_12_a加加和加加a的区别 {
    public static void main(String[] args) {
        int a = 5;
        int b = 5;
        
        int x  = a ++ * 2 ;
        System.out.println(x);
        int y = ++ b * 2;
        System.out.println(y);
        /*
         * a ++ 是先做运算然后再对自身加1
         * ++ a 是选自己加1,然后再做运算
         */
        System.out.println(a);
        System.out.println(b);
        
        
        
        
        
        
        // System.out.println( a ++ );// 5
        // a ++ 先输出然后再加1;
        // System.out.println( ++ b );
        // ++ b 先对自己加1然后再输出。
        
        
//        a ++ ;
//        ++ b;
//        
//        // 不管在哪,最终都是对自己加1;
//        System.out.println(a);
//        System.out.println(b);
        
    }
}

复制代码

import java.util.Scanner;

public class jh_13_键盘录入 {
    /*
     * 1 : 代码块 :{}---大括号括起来的内容。
     * 2: 函数: 完成特定功能 的代码块。
     * nextInt();
     * 3:使用, 调用函数。对象名.函数名();
     * 4: 键盘录入 Scanner 的对象。
     * Scanner sc = new Scanner(System.in);
     * 对象名.函数名();
     * sc.nextInt();
     * int a = sc.nextInt();
     */
    public static void main(String[] args) {
        // 创建键盘录入对象。
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入a的值:");
        // 在控制台输入一个整数,
        // 把输入的这个整数赋值给a。
        int b = 10;
        int a = sc.nextInt();
        System.out.println(a);
    }

}
public class jh_14_自动类型转换举例 {
    /*
     * 某班第一次Java考试平均分81.29,
     * 第二次比第一次多2分,
     * 计算第二次考试平均分?
     */
    public static void main(String[] args) {
        //某班第一次Java考试平均分81.29
        double firstScore = 81.29;
//        第二次比第一次多2分
        double secondScore ;
        secondScore = firstScore + 2;
        
        /*
         * 2 --- 2.0
         * 2.0 --- > 2
         */
        // 输出结果:
        System.out.println("第二次的平均分是: "+ secondScore);
        double a = 2;
        System.out.println(a);
        // 自动类型转换 隐式转换
        // 强制 ----  ----  ---- 
    }

}

  

public class jh_15_强制类型转换 {
    /*
     * 去年Apple笔记本所占市场份额是20,
     * 今年增长的市场份额是9.8,
     * 求今年所占份额?
     */
    public static void main(String[] args) {
//        去年Apple笔记本所占市场份额是20,
        int lastYear = 20;
        
//        今年增长的市场份额是9.8
//        double thisYear = lastYear + 9.8;
//        Type mismatch: cannot convert from double to int
//        类型不匹配:无法从 double 转换为int
        // (int) (lastYear + 9.8)
        //        lastYear + 9.8
        // 类。类类型。
        int thisYear = lastYear + (int)9.8;    
    }
}

  

public class jh_16_类型转换小结 {
    /*
     * x = 10;
     * 40 = 2 * x + 1
     * x???
     * 
     * 实现一个数字加密器,
     * 加密规则是:
     * 加密结果 = 
     * (整数*10+5)/ 2 + 3.14159,
     * 加   密结果仍为一整数
     * 
     * 3 --- + 0.9 --- 3.9
     * 3.9 --- -0.9 --- 3
     * 
     */
    public static void main(String[] args) {
        int num = 5;
//        (整数*10+5)/ 2 + 3.14159,
        int i = (int)((num * 10 + 5)/2 + 3.14159);
        System.out.println(i);
        
        
        
        
        
        
        
        
        
        
        
    }
}

  

public class jh_17_关系运算符 {
    public static void main(String[] args) {
        // 自然,猴子生活在树上。
        // 玉米长在水里面。
        // 小时候已经知道什么是对的,
        // 什么是错的。
        // ,思想品德,社会
        
        /*
         * 数学。
         *  5 大于 3 对
         *  5 小于 3 错。
         *  
         *  true True
         *  false  False
         */
        System.out.println(10);
        System.out.println(9.8);
        System.out.println("张三");
        System.out.println('男');
        // 输出true  false 
        
        System.out.println(true);
        System.out.println(false);
        // 布尔
        boolean t = true;
        System.out.println(t);
        boolean f = false;
        System.out.println(f);
        
        /*
         * 关系运算。
         * 
         * 5 大于 3 ---- 5 > 3
         * 5 小于 3 ---- 5 < 3
         * 5 大于等于 3 --- 5 >=  3
         * 5 小于等于 3 --- 5 <=  3
         * 5  等于 3  ---- 5 == 3
         * 
         * 5 大于 3 ---- 5 > 3
         * 
         * a = 5
         * b = 3
         * 5 > 3 ---- a > b
         */
        System.out.println(5 + 3);
        System.out.println(5 > 3);
        System.out.println("*****************");
        int a = 5;
        int b = 3;
        
        System.out.println(a > b);
        System.out.println(a + b);
        int sum = a + b;
        System.out.println(sum );
        boolean result01 = a > b;
        System.out.println(result01);
        
        System.out.println(a < b);
        System.out.println(a >= b);
        System.out.println(a <= b);
        System.out.println(a == b);
        
        // 等于 == 
        // 不等于。 != 
        // ! 非。
        System.out.println(!true);
        System.out.println(!!true);
        System.out.println(a != b);
        System.out.println( ! (a > b));
    }
}

  

import java.util.Scanner;

public class jh_18_如何使用boolean类型 {
    /*
     * 从控制台输入张三同学的成绩,
     * 与李四的成绩(80分)比较,
     * 输出“张三的成绩比李四的成绩高吗?” 
     * 的判断结果
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
//        从控制台输入张三同学的成绩
        int a = 10;
        System.out.println(a );
        System.out.println("请输入张三的成绩:");
        int zhangScore = sc.nextInt();
//        与李四的成绩(80分)比较
//        李四的成绩(80分
        int liScore = 80;
        
//        张三的成绩比李四的成绩高吗?
        boolean result = zhangScore > liScore;
        
        // 输出结果
        System.out.println("高吗?"+result);
        
        
        
        
    }
}
public class jh_19_运算符的优先级 {
    /*
     * 表达式(3+40%6)>(9/2*3)的结果是什么?
     * 数学,先乘除,后加减。
     * 最高的优先级, ()
     * 最低的是赋值。 = 
     * 优先级顺序:算术运算符 > 关系运算符 > 逻辑运算符
    当运算符比较多,
    + - 
    无法确定运算符执行顺序时,
    可以使用小括号控制一下顺序
    (3 + 40) % 6  > 9/2*3
+ 3 * 4
     * true != (result = 5 > 3)
     */
    public static void main(String[] args) {
        boolean result = 
                (3 + 40 % 6 ) > (9/2*3);
        System.out.println(3 + 40 % 6);
        System.out.println(9/2*3);
        System.out.println(result);
        /*
         * 0 -- false 
         * 非零 --- true
         * "" --- false
         * 非空--- true
         * 容器。盒子。
         * 空容器 -- false.
         * 非空 --- true. 
         * 
         */
        System.out.println("*********");
        System.out.println('a' > 1);
        System.out.println(true != (result = 5 > 3));
        System.out.println(5 + 3 * 4);
        System.out.println((5 + 3) * 4);
        System.out.println(true == false);
//        System.out.println(true > false);
    }
}

  

public class jh_20_学员操作_打印购物小票 {
    /*
     * 各商品的消费金额之和 * 折扣
     */
    public static void main(String[] args) {
        // 商品名称
        String tx = "T恤";
        String wqx = "网球鞋";
        String wqp = "网球拍";
        
        // 单价
        int txPrice = 245;
        int wqxPrice = 570;
        int wqpPrice = 320;
        
        // 个数
        int txNum = 2;
        int wqxNum = 1;
        int wqpNum = 1;
        
        // 计算各个商品的金额:
        int txMoney = txPrice * txNum;
        int wqxMoney = wqxPrice * wqxNum;
        int wqpMoney = wqpPrice * wqpNum;
        
        // 输出截图上半部分。
        System.out.println("*********消费单*************");
        System.out.println("购买物品\t单价\t个数\t金额");
        System.out.println(tx +"\t¥"+txPrice+"\t"+txNum+"\t¥"+txMoney);
        System.out.println(wqx+"\t¥"+wqxPrice+"\t"+wqxNum+"\t¥"+wqxMoney);
        System.out.println(wqp+"\t¥"+wqpPrice+"\t"+wqpNum+"\t¥"+wqpMoney);
        System.out.println();
        
        // 给出折扣
        double discount = 0.8;// 8 折
        
        // 计算消费总金额--- allMoney:
        double allMoney = (txMoney+wqxMoney+wqpMoney)*discount;
        
        // 给出实际交费金额 1500
        int money = 1500;
        
        // 计算找钱。leftMoney ;
        double leftMoney = money - allMoney;
        
        // 计算积分。 100元积 3分。
        int integral = (int) (allMoney / 100 * 3);
        
        // 按截图内容 输出结果
        
        System.out.println("折扣\t"+(int)(discount*10)+"折");
        System.out.println("消费总金额:  ¥"+allMoney);
        System.out.println("实际交费\t" + money);
        System.out.println("找钱:\t"+leftMoney);
        System.out.println("积分:\t"+integral);
        
        /*
         * 1 :给出九个变量名称。存储内容。
         * 2:输出截图上半部分
         * 3:做运算。
         * 4:输出运算后的结果。
         */
    }
}

  

  

原文地址:https://www.cnblogs.com/weiamd/p/11114006.html