“Java if语句的应用(二)”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
if...else if 多重判断
第1行: 第1行:
 
==问题引出==
 
==问题引出==
编程实现计算月份天数,用户输入年份和月份,如果是1月、3月、5月、7月等月,输出“该月有31天”,如果是4月、6月、9月等月,输出“该月有30天”,如果是2月,输出“该月有28(29)
+
例题:输入一个年份,判断它是不是闰年:
+
 
 +
<nowiki>闰年是公历中的名词。
 +
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
 +
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);</nowiki>
 +
 
 +
算法分析:
 +
 
 +
1.输入一个年份
 +
 
 +
2.看这个数能否被4整除,如果不能被4整除,则肯定不是闰年
 +
 
 +
3.如果这个数能被4整除,再看它是不是世纪年(能被100整除的年份)
 +
 
 +
*如果不是世纪年,则它肯定是闰年;
 +
*如果是世纪年,则再看它能否被400整除:
 +
 
 +
**如果能被400整除:说明是闰年;
 +
**如果不能被400整除,说明不是闰年。
  
 
==if语句的嵌套==
 
==if语句的嵌套==
第24行: 第41行:
 
}</nowiki>
 
}</nowiki>
  
*面馆例题:询问顾客吃什么面,并询问其是否加蛋、加油渣 ?将结果反馈给后台。
+
例题:输入一个年份,判断它是不是闰年:
  
 +
<nowiki>闰年是公历中的名词。
 +
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
 +
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);</nowiki>
  
练习:手机店“如果手机价格低于2000,并且能分期付款,那么我就买”怎么写:
 
  
  <nowiki>float price=1999f;boolean  fenqi=False;
+
  <nowiki>package task4;
if(price <2000){
+
 
  if(fenqi==True){
+
import java.util.Scanner;
    System.out.println("我购买。");
+
 
  }else{
+
public class RunNian1 {
    System.out.println("我不买。");
+
 
  }
+
public static void main(String[] args) {
}else {
+
// TODO Auto-generated method stub
    System.out.println("我不买。");
+
Scanner s= new Scanner(System.in);
 +
System.out.println("请输入年份(2001-2099):");
 +
int year=s.nextInt();
 +
if (year%4 !=0) {
 +
System.out.println("该年份不是闰年");
 +
}else {
 +
//再看它是不是世纪年
 +
if(year%100 !=0) {
 +
System.out.println("该年份不是闰年");
 +
}else {
 +
if(year%400 ==0) {
 +
System.out.println("该年份是闰年");
 +
}else {
 +
System.out.println("该年份不是闰年");
 +
}
 +
}
 +
}
 +
}
 +
 
 
}</nowiki>
 
}</nowiki>
  
 
==if语句的多条件==
 
==if语句的多条件==
  <nowiki>if (判断条件)  
+
  <nowiki>if (判断条件 op 判断条件 op ...)  
 
{  
 
{  
 
执行语句块1
 
执行语句块1
 
}</nowiki>
 
}</nowiki>
 +
 
if语句的判断条件里可以放置多个条件,彼此用逻辑运算符连接起来。
 
if语句的判断条件里可以放置多个条件,彼此用逻辑运算符连接起来。
  
判断闰年:
+
例:
 +
 +
<nowiki>package task4;
 +
 
 +
import java.util.Scanner;
 +
 
 +
public class Divide {
 +
 
 +
public static void main(String[] args) {
 +
// TODO Auto-generated method stub
 +
Scanner sc = new Scanner(System.in);
 +
System.out.println("请输入一个整数:");
 +
int t=sc.nextInt();
 +
if(t%2==0 && t%3 ==0){  
 +
      System.out.println("divisible by 2 and 3");
 +
 
 +
}
 +
 +
System.out.println("处理完毕");
 +
sc.close();
 +
}
 +
 
 +
}
 +
</nowiki>
 +
 
 +
例题:输入一个年份,判断它是不是闰年:
 +
 
 +
<nowiki>闰年是公历中的名词。
 +
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
 +
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);</nowiki>
 +
 
 +
<nowiki>
 +
import java.util.Scanner;
 +
 
 +
public class Main {
 +
 
 +
public static void main(String[] args) {
 +
// TODO Auto-generated method stub
 +
 +
Scanner in = new Scanner(System.in);
 +
        System.out.print("输入年份:");
 +
        int year = in.nextInt();        
 +
        String str;
 +
        // 闰年二月份
 +
        if ( (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0 ) ) {
 +
            str = "该年是闰年";
 +
        } else {
 +
        str = "该年不是闰年";
 +
        }
 +
 +
        System.out.println(str);
 +
        in.close();
 +
}
 +
   
 +
 +
 
 +
}
 +
</nowiki>
 +
 
 +
*姊妹题:输入一个年份,判断一年有多少天,
  
 
  <nowiki>int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
  <nowiki>int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
第73行: 第170行:
  
  
引子:如果手机价格低于2000,那么我就买
 
<nowiki>float price=1999f;
 
if(price<2000){
 
    System.out.println("我购买。");
 
}</nowiki>
 
例题:如果手机价格低于2000,或者能分期付款,那么我就买
 
<nowiki>float price=1999f;boolean  fenqi=False;
 
if(price<2000||fenqi=True){
 
    System.out.println("我购买。");
 
}</nowiki>
 
例题:如果手机价格低于2000,并且能分期付款,那么我就买
 
  
<nowiki>float price=1999f;boolean  fenqi=False;
 
if(price<2000&&fenqi==True){//注意这里的等号和赋值运算符是不一样的
 
    System.out.println("我购买。");
 
}</nowiki>
 
  
例题:如果手机价格低于2000,并且能分期付款,并且零首付,那么我就买
+
练习:如果手机价格低于2000,并且能分期付款;或者手机品牌是苹果的,那么我就买
 +
练习:如果手机价格低于于2000,或者能分期付款;满足这一条件的同时,并且手机品牌是苹果的,那么我就买
  
<nowiki>float price=1999f;boolean  fenqi=False;float firstPay=0f;
+
==if...else if 多重判断==
if(price<2000 && fenqi==True && firstPay==0){//注意这里的等号和赋值运算符是不一样的
+
语法格式:
    System.out.println("我购买。");
 
}</nowiki>
 
 
 
<nowiki>import java.util.Scanner;
 
public class happy09 {
 
 
 
public static void main(String[] args) {
 
// TODO Auto-generated method stub
 
        float price ;boolean fenqi;boolean firstpay;
 
        System.out.println("老板,请问手机多少钱?");
 
        Scanner sc = new Scanner(System.in);
 
        price = sc.nextFloat();
 
        System.out.println("老板,请问支不支持分期?");
 
        fenqi = sc.nextBoolean();
 
        System.out.println("老板,请问支不支持零首付?");
 
        firstpay = sc.nextBoolean();
 
       
 
        if(price <=1000 && fenqi == true && firstpay == true) {
 
        System.out.println("小Case!");
 
        }else {
 
        System.out.println("太贵了,买不起~");
 
        }
 
}
 
  
 +
<nowiki>if (判断条件1)
 +
{
 +
判断条件1为true时的执行语句块1
 +
} else  if(判断条件2){
 +
//判断条件1为false,判断条件2为true
 +
判断条件2为true时的执行语句块2
 +
}
 +
……  //可以有零个或多个else if语句
 +
else {  //最后的else语句也可以省略
 +
所有判断条件均为false时的执行语句块n+1
 
}</nowiki>
 
}</nowiki>
  
练习:如果手机价格低于2000,并且能分期付款;或者手机品牌是苹果的,那么我就买
 
练习:如果手机价格低于于2000,或者能分期付款;满足这一条件的同时,并且手机品牌是苹果的,那么我就买
 
  
==if...else if 多重判断==
 
 
*使用if...else 计算客户的BMI值,体质指数(BMI)=体重(kg)÷(身高(m)*身高(m)),根据用户输入的身高和体重进行计算,如果BMI值低于18.5,显示“您的体重过轻”;如果BMI值值在18.5-23.9之间,显示“您的体型完全正常,请继续保持”;如果BMI值值在24-27之间,显示“您的体型有些超重”;如果BMI值在27以上,显示“您的体重严重超重,请加强锻炼!”
 
*使用if...else 计算客户的BMI值,体质指数(BMI)=体重(kg)÷(身高(m)*身高(m)),根据用户输入的身高和体重进行计算,如果BMI值低于18.5,显示“您的体重过轻”;如果BMI值值在18.5-23.9之间,显示“您的体型完全正常,请继续保持”;如果BMI值值在24-27之间,显示“您的体型有些超重”;如果BMI值在27以上,显示“您的体重严重超重,请加强锻炼!”
  
第150行: 第217行:
 
}
 
}
 
}</nowiki>
 
}</nowiki>
 +
 +
  
 
*例2:当接收用户的输入为1时,输出“您选择了处理查询所有书籍的业务”;输入为2时,输出“您选择了处理按书籍编号查询”;输入为3时,输出“您选择了购买书籍”;否则输出“您的输入不正确,必须在1~6范围内!”。
 
*例2:当接收用户的输入为1时,输出“您选择了处理查询所有书籍的业务”;输入为2时,输出“您选择了处理按书籍编号查询”;输入为3时,输出“您选择了购买书籍”;否则输出“您的输入不正确,必须在1~6范围内!”。

2020年3月19日 (四) 03:32的版本

问题引出

例题:输入一个年份,判断它是不是闰年:

闰年是公历中的名词。
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);

算法分析:

1.输入一个年份

2.看这个数能否被4整除,如果不能被4整除,则肯定不是闰年

3.如果这个数能被4整除,再看它是不是世纪年(能被100整除的年份)

  • 如果不是世纪年,则它肯定是闰年;
  • 如果是世纪年,则再看它能否被400整除:
    • 如果能被400整除:说明是闰年;
    • 如果不能被400整除,说明不是闰年。

if语句的嵌套

if语句的嵌套说的是,在已有的if语句里嵌套新的if语句。

例题:输入一个数,判断它是否是偶数和是否能被3整除

Scanner sc = new Scanner(System.in);
int t=sc.nextInt;
if(t%2==0){
   if(t%3==0){
       System.out.println("Divisible by 3 and 2");
   }else{
       System.out.println("divisible by 2 not divisible by 3");
   }
}else{
   if(t%3==0){
       System.out.println("divisible by 3 not divisible by 2")
   }else{
       System.out.println("not Divisible by 2 not divisible by 3");
   }
}

例题:输入一个年份,判断它是不是闰年:

闰年是公历中的名词。
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);


package task4;

import java.util.Scanner;

public class RunNian1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s= new Scanner(System.in);
		System.out.println("请输入年份(2001-2099):");
		int year=s.nextInt();
		if (year%4 !=0) {
			System.out.println("该年份不是闰年");
		}else {
			//再看它是不是世纪年
			if(year%100 !=0) {
				System.out.println("该年份不是闰年");
			}else {
				if(year%400 ==0) {
					System.out.println("该年份是闰年");
				}else {
					System.out.println("该年份不是闰年");
				}
			}
		}
	}

}

if语句的多条件

if (判断条件 op 判断条件 op ...) 
{ 
执行语句块1
}

if语句的判断条件里可以放置多个条件,彼此用逻辑运算符连接起来。

例:

package task4;

import java.util.Scanner;

public class Divide {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int t=sc.nextInt();
		if(t%2==0 && t%3 ==0){		   
		       System.out.println("divisible by 2 and 3");
		   
		}
		
		System.out.println("处理完毕");
		sc.close();
	}

}

例题:输入一个年份,判断它是不是闰年:

闰年是公历中的名词。
普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub	
		
		Scanner in = new Scanner(System.in);		 
	        System.out.print("输入年份:");
	        int year = in.nextInt();	        
	        String str;
	        // 闰年二月份
	        if ( (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0 ) ) {
	            str = "该年是闰年";
	        } else {
	        	str = "该年不是闰年";
	        }
	 
	        System.out.println(str);
	        in.close();
		}
	    
	

}

  • 姊妹题:输入一个年份,判断一年有多少天,
int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		
		 Scanner in = new Scanner(System.in);
		 
	        System.out.print("输入年份:");
	        int year = in.nextInt();
	        System.out.print("输入月份:");
	        int month = in.nextInt();
	 
	        String str = year + "-" + month + ":";
	 
	        // 闰年二月份
	        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) && month == 2) {
	            str += 29;
	        } else {
	            str += DAYS[month - 1];
	        }
	 
	        System.out.println(str);
	        in.close();
		}
	    



练习:如果手机价格低于2000,并且能分期付款;或者手机品牌是苹果的,那么我就买 练习:如果手机价格低于于2000,或者能分期付款;满足这一条件的同时,并且手机品牌是苹果的,那么我就买

if...else if 多重判断

语法格式:

if (判断条件1) 
{ 
判断条件1为true时的执行语句块1
} else  if(判断条件2){
//判断条件1为false,判断条件2为true
判断条件2为true时的执行语句块2
} 
……  //可以有零个或多个else if语句
else {  //最后的else语句也可以省略
所有判断条件均为false时的执行语句块n+1
}


  • 使用if...else 计算客户的BMI值,体质指数(BMI)=体重(kg)÷(身高(m)*身高(m)),根据用户输入的身高和体重进行计算,如果BMI值低于18.5,显示“您的体重过轻”;如果BMI值值在18.5-23.9之间,显示“您的体型完全正常,请继续保持”;如果BMI值值在24-27之间,显示“您的体型有些超重”;如果BMI值在27以上,显示“您的体重严重超重,请加强锻炼!”
import java.util.Scanner;


public class mly {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		//获取用户对于菜单的选择
		String bmi = scanner.next();
		int value =  Integer.parseInt(bmi);
		if(value <18.5) {
		    System.out.println("您的体重过轻");
		}else if (value <23.9){//18.5<=value <23.9
		    System.out.println("您的体形完全正常");
		}else if (value <27){//24<=value <27
		    System.out.println("您的体重有些超重");
		}else{//value >=27
		    System.out.println("您的体重完全超重,请加强锻炼。");
		   }
		scanner.close();
		}
	}


  • 例2:当接收用户的输入为1时,输出“您选择了处理查询所有书籍的业务”;输入为2时,输出“您选择了处理按书籍编号查询”;输入为3时,输出“您选择了购买书籍”;否则输出“您的输入不正确,必须在1~6范围内!”。
Scanner scanner = new Scanner(System.in);
//获取用户对于菜单的选择
String value = scanner.next();
int choise = Integer.valueOf(value);
if(choice == 1) {
  System.out.println("您选择了处理查询所有书籍的业务");
} else if(choice == 2) {
  System.out.println("您选择了处理按书籍编号查询");
} else if(choice == 3) {
  System.out.println("您选择了购买书籍");
} else {
  System.out.println("您的输入不正确,必须在1~6范围内!");
}

Java练习题

1.完成‘猜数字’游戏的改进版,使玩家能连续多次输入

2.用if+逻辑运算符完成以下语句:

如果手机价格低于2000,那么我就买
如果手机价格低于2000,或者能分期付款,那么我就买
如果手机价格低于2000,并且能分期付款,那么我就买
如果手机价格低于2000,并且能分期付款,并且零首付,那么我就买
如果手机价格低于2000,并且能分期付款;或者手机品牌是苹果的,那么我就买
如果手机价格低于2000,或者能分期付款;满足这一条件的同时,并且手机品牌是苹果的,那么我就买

3.练习:应用if嵌套结构计算上一节中的例题:

当接收用户的输入时,如果监测指数小于100,就输出“空气质量很好,请自由活动!否则的话如果监测指数小于150,输出“空气质量轻度污染,请加强防护”,否则的话如果监测指数小于200,输出“空气中度污染,敏感人群留在室内”,否则的话就输出,“空气严重污染,所有人群留在室内”

练习题答案

计1 徐洁和苗璐瑶同学提供。

1.猜数字:

package lianxi;
import java.util.Scanner; 

public class Text { 

       public static void main(String[] args) {

              // TODO Auto-generated method stub   

             int num = 50;

              while(true){

                     Scanner scan = new Scanner(System.in);

                     System.out.println("输入数字:");

                     int t = scan.nextInt();

                     String str1 = (t > num) ?"你输入的数值偏大" :"你输入的数值偏小";

                     System.out.println("输入的数据为:" + str1);  
            }     

      }             
}

2. 手机购买:

package lianxi;

public class Text3 { 

       public static void main(String[] args) {

              // TODO Auto-generated method stub
              float price = 1999f;
              String name = "apple";
              boolean fenqi = true;
              int firstpay = 0;

              if(price<2000){
                     System.out.println("我就买");
              }else {
                     System.out.println("我bu买");
              }

              if (price<2000 && fenqi==true){
                     System.out.println("我就买");

              }else {
                     System.out.println("我bu买");
              }

              if (price<2000 && fenqi==true && firstpay==0){
                    System.out.println("我就买");

              }else {
                    System.out.println("我bu买");
              }

              if ((price<2000 && fenqi==true) || name=="apple"){
                    System.out.println("我就买");
              }else {
                    System.out.println("我bu买");
              }

              if ((price<2000 ||fenqi==true )&& name=="apple")){
                     System.out.println("我就买");
              }else {
                     System.out.println("我bu买");

              }

       }

}

3.空气质量:由计2 周浩同学提供

package lian;

import java.util.Scanner;

public class san {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Scanner scan = new Scanner(System.in);

                 System.out.println("请输入监测指数:");

                 int t=scan.nextInt();

 

                 if(t<150){

                        if(t<100){

                             System.out.println("空气质量很好,请自由活动!");

                       }else{

                             System.out.println("空气质量轻度污染,请加强防护");

                       }

                 }else{

                       if(t<200){

                             System.out.println("空气中度污染,敏感人群留在室内");

                       }else{

                             System.out.println("空气严重污染,所有人群留在室内");

                       }

                 }

                 scan.close();

               }

              }

结果:

请输入监测指数:

99

空气质量很好,请自由活动!

返回 Java程序设计