Java学习DAY2

第二章 运算符和选择语句

1.运算符

定义:对变量和常量进行操作的符号
在这里插入图片描述
算术运算符:+,-,*,/,%

/*
    整数相除只能得到整数,想要小数,需要浮点数参与运算
     */
public static  void  main(String[] args){
        int a=10;
        int b=3;
        System.out.println(a/b);//商
        System.out.println(a%b);//余数
        System.out.println(10.0/3);
    }

结果:

3
1
3.3333333333333335

Process finished with exit code 0

自增自减运算符:++,–
++或者–可以放在变量前,也可以放在变量后面
++在变量前:先++,再做操作
++在变量后:先做操作,再++(–同理)

——————————————————————
赋值运算符:=,+=,-=…
关系运算符:==,!=,>…(操作结果是布尔型)

public static  void  main(String[] args){
        int a=10;
        int b=5;
        System.out.println(a==b);
        System.out.println(a=b);//把b的值赋给a,再输出a的值
    }

结果:
false
5

Process finished with exit code 0

逻辑运算符:&&,||,!
&&:有false则 false
||:有true 则true
——————————————————————
三元运算符:
关系表达式? 表达式1:表达式2;

2字符与字符串

字符与字符串的加法其实是做拼接,记住几个特殊字符的数值:
‘a’ :97 —— ‘A’: 65——‘0’:48

 public static  void  main(String[] args){
        int a=10;
        char b='a';
        System.out.println(a+b);//商
        System.out.println("hello"+"world");
        System.out.println("hello"+10+20);//注意和下面式子的区别
        System.out.println(10+20+"hello");
    }



结果:
107
helloworld
hello1020
30hello

Process finished with exit code 0

3.Scanner

从键盘输入
在这里插入图片描述

package heima;
import java.util.Scanner;

public class inputnumber {
    public static  void  main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int i = sc.nextInt();
        System.out.println("i:" + i);
    }
}



结果:
请输入一个整数:
10
i:10

Process finished with exit code 0
4.流程控制语句

顺序结构:从上到下,依次执行
选择结构:if语句和switch语句

public class keyword {
    //给定x,计算y的值
    public static  void  main(String[] args) {
        int x= 5;
        int y=0 ;
        if(x>=3){
            y=2*x+1;
        }else if(x>=-1 && x<3){
            y=2*x;
        }else if(x<-1){
            y=0;
        }
        System.out.println("y:"+y);
    }
}



switch语句

package heima;
import java.util.Scanner;

public class keyword {
    //输出星期几
    public static  void  main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out .println("请输入一个整数(1-7):");
        int weekDay= sc.nextInt();
        switch (weekDay){
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("输入错误");
                break;
        }

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值