JavaWeb開發實戰指南--條件結構-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("真的是清哥");
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章