zoukankan      html  css  js  c++  java
  • Java基础06-条件选择语句

    1.switch多分支结构

     1 import java.util.Scanner;
     2 public class Test1{
     3     public static void main(String[] args){
     4         Scanner in=new Scanner(System.in);
     5         System.out.println("请输入你的名次:");
     6         int num1=in.nextInt();
     7         switch(num1){
     8             case 1:
     9             System.out.println("武林盟主");
    10             break;//跳出
    11             case 2:
    12             System.out.println("峨眉掌门");
    13             break;
    14             case 3:
    15             System.out.println("武当掌门");
    16             break;
    17             default:
    18             System.out.println("逐出师门");
    19         }
    20                 
    21     }
    22 }

     2.if嵌套结构

    例:输入一个整数,判断是偶数还是奇数

     1 import java.util.Scanner;
     2 public class Test1{
     3     public static void main(String[] args){
     4         Scanner in=new Scanner(System.in);
     5         System.out.println("请输入一个整数");
     6         int num=in.nextInt();
     7         if(num==0){
     8             System.out.println("请输入正整数");
     9         }else{
    10             if(num%2==0){
    11                 System.out.println("是偶数");
    12             }else{
    13                 System.out.println("是奇数");
    14             }
    15             
    16         }
    17                 
    18     }
    19 }

     3.输入一个年份判断是瑞年还是平年

     1 //年能被4整除但不能被100整除或者能被400整除的为瑞年
     2 import java.util.Scanner;
     3 public class Test1{
     4     public static void main(String[] args){
     5         Scanner in=new Scanner(System.in);
     6         System.out.println("请输入一个年份");
     7         int year=in.nextInt();
     8         if(year%4==0&&year%100!=0||year%400==0){
     9             System.out.println("是瑞年");
    10         }else{
    11             System.out.println("是平年");    
    12         }
    13                 
    14     }
    15 }
  • 相关阅读:
    立一个flag。
    详解 fcntl 记录上锁。
    [转] 虚拟机中生成与物理机的共享文档。
    Shell实现1.0
    深剖malloc、new
    由命名管道的实现联想到 read 和 fread 的区别。
    僵尸进程与孤儿进程。
    Vim引申以及Linux下彩色进度条实现
    各大排序八方齐聚!
    Docker三剑客:Compose、Machine和Swarm
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/9947904.html
Copyright © 2011-2022 走看看