zoukankan      html  css  js  c++  java
  • 流程控制

    一、If

    1、if ( condition ) { ... }
    2、if ( condition ) { ... } else { .... }
    3、if ( condition1 )...else if ( condition2 ) ... else

    public class Statement1 {
    
        public static void main(String[] args) {
            
            // 在 Java 语言中凡是使用 "" 引起来的内容 都是 字符串 ( 对应 String 类型 )
            String name = "张三丰" ; 
            int age = 22 ;
            char gender = '' ;
    
            if( age >= 22 && gender == '' ) {
                System.out.println( name + " , 你可以结婚了" ); // 注意,这里的 + 是字符串连接符,不是算术运算符
            } else {
                System.out.println( name + " , 你还未到结婚年龄" );
            }
    
            System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
    
            Random rand = new Random() ; // 创建一个用于产生随机数的对象
    
            int score = rand.nextInt( 100 ) ; // 随机产生一个 [ 0 , 100 ) 之间的整数
    
            System.out.println( "score : " + score );
    
            if( score >= 90 ) {
                System.out.println( "优秀" );
            } else if( score >= 80 ){ // 为嘛不写 score >= 80 && score < 90 呢? 
                System.out.println( "优良" );
            } else if( score >= 70 ){
                System.out.println( "良好" );
            } else if( score >= 60 ){
                System.out.println( "及格" );
            } else if( score >= 0 ){
                System.out.println( "进步空间巨大" );
            } else {
                System.out.println( "不是有效的成绩" );
            }
    
        }
    
    }

    二、switch

    在 switch 关键字之后的 ( ) 中可以使用那些类型:
    1、JDK 1.5 之前只能使用 与 int 兼容的类型,比如 int 、char 、short 、byte
    2、从 JDK 1.5 开始允许在 switch 语句中使用 枚举类型 ( enum ) 【 以后讲 enum 】
    3、从 JDK 1.7 开始允许在 switch 语句中使用 String 类型

    import java.util.Random ;
    
    public class Statement2 {
    
        public static void main(String[] args) {
    
            Random rand = new Random() ;
    
            int month = rand.nextInt( 12 ) ; // 随机产生一个 [ 0 , 12 ) 之间的整数
            month += 1 ; // month = month + 1 ; // 在中国,月份是从 1 开始的
            System.out.println( "month : " + month ); // [ 1 , 12 ]
    
            /*
            if( month == 2  ) {
                System.out.println( "平年28天、闰年29天" ) ;
            } else if ( month == 4 || month == 6 || month == 9 || month == 11 ){
                System.out.println( "30天" ) ;
            } else if ( month == 1 || month == 3 || month == 5 || month == 7  || month == 8 || month == 10 || month == 12 ){
                System.out.println( "30天" ) ;
            } else {
                System.out.println( "31天" ) ;
            }
            */
    
            char m = (char) month ;
            switch (  m ) { // switch 语句块开始
                    case 2:
                        System.out.println( "平年28天、闰年29天" ) ;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        System.out.println( "30天" ) ;
                        break; // bread 会跳出当前的 switch 语句块
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        System.out.println( "31天" ) ;
                        break; // bread 会跳出当前的 switch 语句块
                    default:
                        System.out.println( "无效的月份" ) ;
                        break;
            } // switch 语句块结束
    
            // 月份:1 、2、3、4、5、6、7、8、9、10、11、12
            // 星期: 一、二、三、四、五、六、日
    
    
    
        }
    
    }
    import java.util.Scanner ;
    
    public class Statement3 {
    
        public static void main(String[] args) {
    
            // 创建一个可以读取用户在控制台中输入内容的扫描器
            Scanner sc = new Scanner( System.in ); // 暂时只使用不详细研究
    
            System.out.println( "请输入一个月份名称,比如 一月、二月、正月、腊月" );
            String month = sc.nextLine(); // 读取用户在控制台中输入的内容(读取到换行符为止)
    
            switch( month ) {
                default :
                    System.out.println( "这个月份我判断不了" ); 
                    break ;
                case "一月" :
                case "正月" :
                case "十二月" :
                case "腊月" :
                    System.out.println( "三十一天" );  // 注意: 这里不考虑农历
                    break ;
                case "二月" :
                    System.out.println( "平年二十八天、闰年二十九天" ); // 注意: 这里不考虑农历
                    break ;
            }
    
    
             // 关闭扫描器
            sc.close();
        }
    
    }
  • 相关阅读:
    前端基础之BOM和DOM
    前端基础之JavaScript
    前端基础之CSS
    Black And White HDU
    Robot Motion HDU
    A Knight's Journey POJ
    Find a way HDU
    排序 HDU
    Dungeon Master POJ
    Network Saboteur POJ
  • 原文地址:https://www.cnblogs.com/helloyuankun/p/12864113.html
Copyright © 2011-2022 走看看