Day04-java语言基础(下)

Day4-java语言基础(下)

这里运算符和类型提升是练习用的,可以跳过直接进入控制台录入

运算符

算数运算符 + - * / % ++ --

赋值运算符 = += -= *= /= %=

关系运算符 > < >= <= == !=

逻辑运算符 && || !

三元运算符 ? :

算数运算符练习

public class TestOperation1{
    public static void main(String[] args){
        //算数运算符 + - * / %
        int a = 5;
        int b = 10;
        System.out.println(a+b);//15
        System.out.println(b-a);//5
        
        int c = 7;
        int d = 2;
        System.out.println(c*d);//14
        System.out.println(c/d);//3
        System.out.println(c%d);//1
    }
}
  • 自增,自减操作
public class TestOperation2{
    public static void main(String[] args){
        /*
        一元运算符 ++ --
        ++ 自增1
        1)如果++是一条语句 a++;与++a;都等价于a=a+1;
        */
        /*
        int a = 12;
        a = a + 1;
        System.out.println(a);
        */
        int a = 12;
        a++;
        //同上述结果一样,a++;替换成++a;结果也一样 
        //--自减1同理
        System.out.println(a);
        
        
        /*
        2)如果++不是一条语句 区分a++与++a
        a++ 先操作再运算 拿原值操作 运算再自增1
        ++a 先运算再操作 拿原值先自增1 拿着自增后的值操作
        【本质上看其先运算还是先操作】
        */
        
        /*
        int a = 12;
        int b = a++;//int b = a; 再运算a++
        System.out.println(b);//12
        System.out.println(a);//13
        */
        
        /*
        int a = 12;
        int b = ++a;//先运算++自增1,a=13,再操作 int b=a
        System.out.println(b);//13
        System.out.println(a);//13
        */
        
        
        /*
        int a = 12;
        int b = a++ + 9;//int b = a + 9;
        System.out.println(b);//21
        System.out.println(a);//13
        */
        
        /*
        int a = 12;
        int b = ++a + 9;//int b = 13+9=22;
        System.out.println(a);//13
        System.out.println(b);//22
        */
            
        /*    
        int a = 12;
        int b =(a++ + 3)*2;//int b = (a + 3)*2;
        System.out.println(a);//13
        System.out.println(b);//30
        */
        
        /*    
        int a = 7;
        int b = (a++) + (a++);
        System.out.println(b);//15
        System.out.println(a);//9
        */
        
        int a = 7;
        int b = (a++) + (++a);
        System.out.println(a);//9
        System.out.println(b);//16
    }
}

赋值运算符练习

public class TestOperation3{
    public static void main(String[] args){
        /*
        赋值运算符
        // = += -= *= /= %=
        */
        /*
        int a = 10;
        a = a + 5;
        System.out.println(a);//15
        */
        int a = 10;
        a += 5;//相当于a = a + 5;
        System.out.println(a);//15
        
        int b = 10;
        b *= 3;//相当于b = b * 10;
        System.out.println(b);//30
        
        int c = 20;
        c /= 3;//c = c/3;
        System.out.println(c);//6
        
        int d = 20;
        d %= 3;//c = c%3;
        System.out.println(d);//2
        
        
        //面试题
        /*
        byte y = 5;
        y = y+1;//类型不兼容,强转y = (byte)(y+1);可以
        System.out.println(y);
        */
        byte y = 5;
        y += 1;//这样运行不报错,它会先进行类型转换后再赋值
        System.out.println(y);//6
    }
}

关系运算符练习

public class TestOperation4{
    public static void main(String[] args){
        //关系运算符
        // > < >= <= == !=
        int a = 10;
        int b = 20;
        System.out.println(a > b);//false
        System.out.println(a < b);//true
        
        int c = 5;
        int d = 10;
        System.out.println(c == d);//false
        System.out.println(c != d);//true
        System.out.println((2 * c) == d);//true
    }
}

逻辑运算符练习

public class TestOperation5{
    public static void main(String[] args){
        //逻辑运算符
        // && || !
        boolean a = true;
        boolean b = false;
        System.out.println(a && b);//false 全真为真
        System.out.println(a || b);//true 一真为真
        
        boolean c = 10 > 5;
        boolean d = 8 < 20;
        System.out.println(c && d);//true
        System.out.println(c || d);//true
        
    }
}

三元运算符练习

public class TestOperation6{
    public static void main(String[] args){
        //三元(目)运算符
        //布尔表达式?表达式1:表达式2
        double score = 100.0;
        //1、三元运算符可以进行逻辑判断
        System.out.println(score == 100.0 ? "真棒" : "加油");
        //2、三元运算符可以判断完为变量赋值
        String str = score == 100.0 ? "真棒" : "加油";
        System.out.println(str);
    }
}

类型提升

public class TestTypeUp{
    public static void main(String[] args){
        //类型提升
        int a = 10;
        double b = 20.5;
        double result1 = a + b;
        System.out.println(result1);//double类型

        int c = 5;
        float d = 1.2F;
        float result2 = c + d;
        System.out.println(result2);//float类型

        int e = 100;
        long f = 50L;
        long result3 = e + f;
        System.out.println(result3);//long类型

        byte g = 10;
        int h = 20;
        int result4 = g + h;
        System.out.println(result4);//int类型

        byte j = 5;
        short k = 5;
        int result5 = j + k;//注意这里为int
        System.out.println(result5);//int类型
        /*
        特殊:任何类型与String相加(+)时,实为拼接,其结果自动提升为String。
        口诀:+两端有字符串整体提升为String类型
			 +两端无字符串类型+算术运算符加法运算
        */
        System.out.println(12 + 34);//46
        System.out.println(12 + 'A' + 34);//12+65+34=111
        System.out.println(10 + 3.14);//13.14
        System.out.println(12 + "哈哈" + 34);//12哈哈34 string类型

        //将任意类型转换为字符串类型,加空字符串""
        System.out.println(12 + "" + 34);//1234 这是一个string类型
    }
}

控制台录入

使⽤顺序:

  1. 导⼊ java.util.Scanner。

  2. 声明 Scanner 类型的变量。

使⽤Scanner类中对应的⽅法(区分类型):

  • .nextInt(); //获得整数

  • .nextDouble(); //获得⼩数

  • .next(); //获得字符串

  • .next().charAt(0);//获得单个字符

  • 等等...

【注意】:控制台还有一些录入方式,比如通过System.in.read()等...

这里只介绍这一种,也是较为常见的一种

//如果有包的声明必须放在文件的首行
//1、引用JDK提供的扫描仪工具
import java.util.Scanner;
public class TestScanner1{
    public static void main(String[] args){
        //数据类型 变量名 = 值;
        //2、声明Scanner
        Scanner input = new Scannner(System.in);
        //3、基于Scanner变量名input.nextInt();获取整数
        int i = input.nexInt();
        System.out.println(i);
    }
}
//1、导包
import java.util.Scanner;
public class TestScanner2{
    public static void main(String[] args){
        //2、声明Scanner
        Scanner input = new Scanner(System.in);
        //3、input 接收字符串类型的值 input.next();
        System.out.println("请输入您的姓名:");
        String name = input.next();
        System.out.println("您的姓名是:"+name);
        //3、input 接收整型类型的值 input.nextInt();
        //如果录入不匹配数据,会产生java.util.InputMismatchEception,比如年龄输入18.1
        System.out.println("请输入您的年龄:");
        int age = input.nextInt();
        System.out.println("您的年龄是:"+age);
        //3、input 接收整型类型的值 input.next().charAt(0);
        System.out.println("请输入您的性别:");
        int sex = input.next().charAt(0);
        System.out.println("您的性别是:"+sex);
        //3、input 接收整型类型的值 input.nextDouble();
        System.out.println("请输入您的身高:");
        int height = input.nextDouble();
        System.out.println("您的身高是:"+height);
    }
}
posted @ 2021-07-15 23:16  CN_Darren  阅读(42)  评论(0)    收藏  举报