分享

条件结构-if

 清哥好课堂 2022-06-01 发布于湖南


清哥好课堂

《最受欢迎的精准提升平台》

条件结构-if
/***@Title: ${filename}*@Package: ${package_name}*@Description: ${todo}*从上往下依次执行,按顺序执行的,这一种结构,我们把它称为顺序结构*条件结构选择结构if结构语法if(条件表达式){  //条件成立时,执行的语句}if ---》如果表达式是由运算符,变量,常量所组成的式子条件表达式,表示条件的式子,结果,要么是真,要么是假是真,表示条件成立是假,表示条件不成立
比较运算符逻辑运算符
*@author: 源代码资料尽在"清哥好课堂"公众号:qghktit*@date: ${date}${time}*@version: 1.0*/import java.util.Scanner;public class IFDemo { public static void main(String[] args) { //对3个数进行排序,从小到大进行排列 //1、从控制台接收三个数 int a, b,c; Scanner sc = new Scanner(System.in); System.out.println("please input first number:"); a = sc.nextInt(); System.out.println("please input second number:"); b = sc.nextInt(); System.out.println("please input three number:"); c = sc.nextInt(); //2、比较 //2.1、a,b比较 完成之后 a是a、b中最小 if (a > b) { int t = a; a = b; b = t; } //2.2、a,c比较,得到,a、b、c中最小的a if (a > c) { int t = a; a = c; c = t; } //2.3、b,c比较, 得到,b是三个数中的第二小的值 if (b > c) { int t = b; b = c; c = t; } //3、输出结果 System.out.println("从小到大的顺序是:"+a+","+b+","+c); } public static void main5(String[] args) { //对2个数进行排序,从小到大进行排列 // a, b b a //1、准备两个数 int a, b; a = 5; b = 6; //2、比较 小到大 if(a > b) {//交换a, b的位置 int t = a; a = b; b = t; }
//3、按顺序输出 System.out.println("从小到大的顺序是:"+a+"," +b); } public static void main4(String[] args) { //3个数的最大值 //1、从控制台接收三个数 int a, b,c; Scanner sc = new Scanner(System.in); System.out.println("please input first number:"); a = sc.nextInt(); System.out.println("please input second number:"); b = sc.nextInt(); System.out.println("please input three number:"); c = sc.nextInt();
//2、比较,求出三个数中最大值 int max = a; //2.1 求a,b中的最大值 if (max < b) { max = b; } //2.2 求a,b,c中的最大值 if (max < c) { max = c; } //3、输出 System.out.println(a+","+b+","+c+"中的最大值是"+max); } public static void main3(String[] args) { //求2个数中最大的那个数,最大值 //1、要准备两个数 int a, b; a = 7; b = 6; //2、比较,得到最大值 int max = a; if (max < b) { max = b; } //3、输出最大值 System.out.println(a+","+b+"最大值是:"+max); } public static void main2(String[] args) { //输入一个数,判断这个数是否在(0-100)之间 //1、从控制台接收一个数 Scanner sc = new Scanner(System.in); System.out.println("please input a number:"); int n = sc.nextInt(); //2、要判断它是在否(0, 100)区间内 // 0<n && n<100 if (n>0 && n < 100) { System.out.println(n+"是在(0, 100)的区间内"); } } public static void main1(String[] args) {// System.out.println("Hello World!");// System.out.println("Hello World!");// System.out.println("Hello World!"); int a = 5; int b = 5;// if (a == b) //条件// { //大括号中可以有多条语句// System.out.println("Hello World!");// System.out.println("Hello World!");// } if (a==b) //{ //如果条件的执行语句,只有一条,大括号可以省略 System.out.println("Hello World!"); //} System.out.println("大家好!");
//对于字符串来说,判断相等,一般使用 equals() String //判断字符串的内容,是否相等 // == 来判断,不仅内容相等,还要求指向相同 String str = "清哥";// if ("清哥".equals(str))// {// System.out.println("真的是清哥");// } if (str.equals("清哥")) { System.out.println("真的是清哥"); } }}

清哥好课堂公众号

微信号 : qghktit

新浪微博:清哥好课堂

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约