zoukankan      html  css  js  c++  java
  • 条件结构

    一、if...else语句

     1 //导入包使用Scanner
     2 import java.util.Scanner;
     3 public class IfDemo{
     4     public static void main(String [] args){
     5         Scanner input=new Scanner(System.in);
     6         System.out.println("请输入假期天数");
     7         int holiday=input.nextInt();//获取键盘输入的整数 next表示字符串 nextDouble表示double类型
     8         if(holiday>6)
     9         {
    10             System.out.println("去海南");
    11         }
    12         else if(holiday>3)
    13         {
    14             System.out.println("去海边");
    15         }
    16         else
    17         {
    18             System.out.println("在家");
    19         }
    20     }
    21 }
    View Code

    二、switch

     1 import java.util.Scanner;
     2 public class SwitchDemo{
     3     public static void main(String []args)
     4     {
     5         Scanner input=new Scanner(System.in);
     6         System.out.println("请输入1-5之间的数字");
     7         int number=input.nextInt();
     8         switch(number)
     9         {
    10             case 1:System.out.println("1");break;
    11             case 2:System.out.println("2");break;
    12             case 3:System.out.println("3");break;
    13             case 4:System.out.println("4");break;
    14             case 5:System.out.println("5");break;
    15             default:System.out.println("Error");break;
    16         }
    17     }
    18 }
    View Code

    小结

    1.case后面的常量不能重复
    2.break可以省略,一旦省略,程序就会一直往下执行,直到遇见break或switch结束
    3.case的顺序可以颠倒,default可以放在任何位置,一般放在最后,可以省略
    4.switc用于匹配常量,能匹配的类型有byte,short,int,char,String(jdk1.7及以上),enum(jdk1.5)
    5.switch和多重if...else语句比较
    switch适合做等值判断,不适合做区间判断,做等值判断的时候语法更简洁,直观
    多重if语句功能比switch更全面

  • 相关阅读:
    jQuery基础知识
    在nginx上部署django项目--------Gunicorn+Django+nginx+mysql
    pycharm激活码------2017.11.之前有效
    nginx安装及优化
    Python sh模块--------替换subprocess的利器
    Django1.10官方文档翻译
    神秘的.user.ini文件
    Thinkphp5 分页带参数
    javascript 点击按钮实现隐藏显示切换效果
    Centos 7 安装 Supervisor 及使用
  • 原文地址:https://www.cnblogs.com/wzy330782/p/5256224.html
Copyright © 2011-2022 走看看