キーボードで月を入力し、対応するシーズンを出力します(他の場合またはスイッチの場合)。

パッケージren.redface.demo; 

import java.util.Scanner;

/ *
*需要:キーボードは月に入り、その月の対応する季節を出力します。
*年に4つの季節があります
* 3,4,5春
* 6,7,8夏
* 9,10,11秋
* 12,1,2冬
*
*分析:
* A:キーボードで月を入力し、スキャナーを使用して
* B :月が月であるかを判別し、月に応じて対応する季節を出力します
* if
*スイッチ
* /

public  class Test {
     public  static  void main(String [] args){
         // キーボード入力を1か月、スキャナーを使用してスキャナーを実装 
        sc = new Scanner(System.in); 

        // データを受信 
        System.out.println( "1つ入力してください月(1-12): " );
         int month = sc.nextInt(); 

        // 月が数か月か
        どうかを判別し、(月== 1 ||月== 2 ||月= = 12 ){ 
            System.out.println( "冬" ); 
        } else  if(月== 3 ||月== 4 ||月== 5 ){ 
            System.out.println( "春"); 
        } else  if(month == 6 || month == 7 || month == 8 ){ 
            System.out.println( "Summer" ); 
        } else  if(month == 9 || month == 10 | | month == 11 ){ 
            System.out.println( "Autumn" ); 
        } else { 
            System.out.println( "入力した月が間違っています" ); 
        } 
    } 
}

 

package ren.redface.demo; 

import java.util.Scanner; 

/ * 
 *要件:キーボードで月を入力し、その月に対応する季節を出力します。
 *年に4つの季節があります
 * 3,4,5春
 * 6,7,8夏
 * 9,10,11秋
 * 12,1,2冬
 * 
 *分析:
 * A:キーボードで月を入力し、スキャナーを使用して
 * B :その月がどの月であるかを判別し、対応するシーズンを出力します
 * if 
 * switch 
 * 
 * 月ごとのケース浸透。
 * / 
public  class Test2 {
     public  static  void main(String [] args){
         // 1か月のキーボード入力、スキャナーを使用してスキャナーを実装 
        sc = newScanner(System.in); 

        // データを受信します 
        System.out.println( "月を入力してください(1-12):" );
         int month = sc.nextInt(); 

        // ケース貫通
        スイッチを使用してコードを改善します(月){
         ケース 1 ケース 2 ケース 12 
            System.out.println( "Winter" );
             ブレーク;
         ケース 3 ケース 4 ケース 5 
            System.out.println( "Spring" );
             ブレーク;
         ケース 6 ケース 7 ケース 8 
            System.out.println( "Summer" );
             break ;
         ケース 9 ケース 10 ケース 11 
            System.out.println( "Autumn" );
             break ;
         デフォルト
                System.out .println( "入力した月が間違っています" );
                 break ; 
        } 
    } 
}

 

おすすめ

転載: www.cnblogs.com/ooo888ooo/p/12689238.html