首页 > 代码库 > 多重和嵌套if

多重和嵌套if

多重if实例:

看例子,内容不解释了!

隐藏行号 复制代码 多重if
  1. import java.util.Scanner;
  2. public class 多重if{
  3.     public static void main(String[] args){
  4.         Scanner input= new Scanner(System.in);
  5.         System.out.print("请输入成绩");
  6.         int i=input.nextInt();
  7.         if(i>=90){
  8.             System.out.print("优秀");
  9.         }else if(i>=80){
  10.             System.out.print("良好");
  11.         }else if(i>=60){
  12.             System.out.print("一般");
  13.         }else{
  14.             System.out.print("差");
  15.         }
  16.     }
  17. }
<style type="text/css">.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}</style>

嵌套if实例:

隐藏行号 复制代码 嵌套if
  1. import java.util.*;
  2. public class 嵌套if{
  3.     public static void main(String[] args){
  4.         Scanner i=new Scanner(System.in);
  5.         System.out.print("请输入你的姓名:");
  6.         String name=i.next();
  7.         System.out.print("您的性别:");
  8.         String sex=i.next();
  9.         //System.out.print("请输入你的年龄:");
  10.         //int age=i.nextInt();
  11.         if(sex.equals("男")){
  12.             System.out.print("请输入你的年龄:");
  13.             int age=i.nextInt();
  14.             if(age>=18){
  15.                 System.out.print("欢迎"+name+"先生!");    
  16.             }else{
  17.                 System.out.print("对不起,"+name+"朋友,你还未成年,请自觉退出!");
  18.             }
  19.         }else{
  20.             System.out.print("尊敬的"+name+"女士,这里是男士VIP");
  21.         }
  22.     }
  23. }
<style type="text/css">.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}</style>

以下为输出结果,很有趣吧!

image