笔记02

逻辑运算符基本使用

1.&&与&的区别

(1)最终结果都一样;

(2)&&具有短路效果,左边是false,右边不执行;

(3)&无论左边是false还是true,右边都会执行。

2.||和|区别

(1)最终结果一样;

(2)||具有短路效果,左边是true,右边不执行;

(3)|无论左边是false还是true,右边都会执行。

练习:输入两个数比较是否相等。输出三个数中的最大数

    import java.util.Scanner;
    public class operater5 {
    public static void main(String[] args){
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入数x:  y:");
	int x=sc.nextInt();
	int y=sc.nextInt();
	
	boolean flag=(x==y)?true:false;
	System.out.println(flag);
	System.out.println("请输入数a:  b:  c:");
	int a=sc.nextInt();
	int b=sc.nextInt();
	int c=sc.nextInt();
	System.out.println("三个数中的最大数为:");
	System.out.println(a>b?(a>c?a:c):(b>c?b:c));
	}
 }

运行结果
在这里插入图片描述

自增自减操作

A: ++和- -操作:

  • a:单独操作的时候,++和–不管放在前面还是后面,结果是一样的,
    b:如果参与了运算操作的时候:
    如果++和–在变量的后面的时候,先那变量参与运算操作,后变量进行++和–操作;
    如果++和–在变量的前面的时候,先变量做++和–操作,后拿变量参与运算操作。
* 例句:输出一个数,利用自增自减操作进行运算:
  运行代码:
                public class Until6 {
                 public static void main(String[] args){
	             int x=5;
	             System.out.println(x);
	             x++;
	             ++x;
	            System.out.println(x);
	            int y=++x;
	            System.out.println("x:"+x);
	             System.out.println("y:"+y);		
          }
       }

二、流程控制语句

(1)顺序语句
(2)if语句的基本使用
格式:if(关系表达式){
语句块;
}else if(关系表达式){
语句块;
}

else{
语句块;
}

  •      import java.util.Scanner;
     /**王者荣耀段位
    
    • 程序考虑数据
  •  安全性数据
    
  •   边界数据
    
  •   错误数据
    
  • @author Administrator
    

*/
public class operater10 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入数x: ");
int x=sc.nextInt();
if(x>=90&&x<=100){
System.out.println(“最强王者”);

	   }else if(x>=80&&x<90){
		System.out.println("星耀");
		
	     }else  if(x>=70&&x<80){
		
		System.out.println("钻石");
		
	   }else  if(x>=60&&x<70){
		
		  System.out.println("黄金");
	   }else  if(x<60){
		 System.out.println("黑铁");
	   }else{
	      System.out.println("最强强强王者");
	   }
       }
}

运行结果
在这里插入图片描述
(3)switch语句
A:格式:
switch(变量/表达式){
case 字面值1:语句块1;break;
case 字面值2:语句块2;break;

default:语句块n ;
}
B:switch 语句:
1.表达式的值,byte,short,int, char
2.jdk 1.5之后,加入枚举类型;
3.JDK 1.7之后,加入String类型;
4.break: 能够中断switch语句的执行。
5. default:所有的情况都不匹配的时候,就执行该处的语句块。
掌握switch语句的执行顺序
*
例句:使用输入一个年份每个月都有几天
运行代码:
import java.util.Scanner;
public class operater14{

      public static void main(String[] args){
	
     Scanner sc=new Scanner(System.in);
     int year =sc.nextInt();
    int month =sc.nextInt();
    int days=31;
     switch(month){
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
    	  break;
      case 4:
	  case 6:
	  case 9:
	  case 11:
	days=30;break;
	  case 2:
    	  days=(year % 4 == 0 && year % 100 !=0||year % 400 ==0)?29:28;
    
    		  break;
    default:
    	break;
    	
     }System.out.println(year+"年"+month+"月有"+days+"天");
sc.close();
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值