java入门之if判断语句

JAVA入门之if判断语句

1、基本语法

class IFDemo1{
	public static void main(String[] args) {
	
	/*	if(结果是布尔类型的值或者说是表达式){
				执行的语句体
		}
		*/
		
		int a=1;
		if(a++>1){ //()里面的结果为true 那就执行{}里面的语句体
			System.out.println("Hello World!");
		}
			System.out.println("Hello World!下面的代码");
	}
}

2、if…else比较数的大小

class IFDemo2 {
	public static void main(String[] args) {
		if(5>2){
				System.out.println("Hello World!真");

		}else{
				System.out.println("Hello World!假");
		}
		System.out.println("Hello World!下面的代码");
	}
}

3、求两个数的最大值

class IFDemo3 {
	public static void main(String[] args) {
		// if else 来求出两个数的最大值
		int a=20;
		int b=100;
		
		//定义一个变量来保存这个最大值
		int max=0;
		if(a>b){
			max=a;
		}else{
			max=b;
		}
		//输出最大值
		System.out.println("最大值是"+max);
	}
}

4、if…else 嵌套求三个数的最大值

class  IFDemo4{
	public static void main(String[] args) {
		int a=20;
		int b=200;
		int c=20000;
		int max=0;
		
		//if else 嵌套
		if(a>b){
			if(a>c){
				max=a;
			}else{
				max=c;
			}
		}else{
			if(b>c){
				max=b;
			}else{
				max=c;
			}
		}
		System.out.println("最大值是"+max);
	}
}

5、三个数求最大值的两种方法

class IFDemo5 {
	public static void main(String[] args) {
		int a=20;
		int b=10;
		int c=5;

		int max=0;
		int max2=0;
		/*
		if(a>b){
			max=a;
		}else{
			max=b;
		}
		if(max>c){
			max2=max;
		}else{
		   max2=c;
		}*/
		if(a>b&&a>c){
			max=a;
		}
		if(b>a&&b>c){
			max=b;
		}
		if(c>a&&c>b){
			max=c;
		}
		System.out.println("最大值是"+max);
	}
}

6、else…if 录入成绩,对成绩进行判断,分出一个优良中差

import java.util.Scanner;
class IFDemo6 {
	public static void main(String[] args) {

		//输入成绩
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入你的成绩 0---100");
		int score=sc.nextInt();
		
		//对成绩进行判断,分出一个优良中差
		if(score>=0&&score<60){
			System.out.println("成绩不及格");

		}else if(score>=60&&score<75){
			System.out.println("成绩差");

		}else if(score>=75&&score<85){
			System.out.println("成绩中等");

		}else if(score>=85&&score<95){
			System.out.println("成绩良好");

		}else if(score>=95&&score<100){
			System.out.println("成绩有效");

		}else if(score==100){
			System.out.println("满分");

		}else{
		  System.out.println("成绩不合法");
		}
	}
}

发布了5 篇原创文章 · 获赞 0 · 访问量 42

猜你喜欢

转载自blog.csdn.net/weixin_42401546/article/details/103902767
今日推荐