java中的条件判断语句

2018/08/07 19:41
阅读数 107

一、java中的条件语句:

1、if语句:

      格式:if(判断的条件){

                           语句1;

                  }

      解读:当条件符合的时,程序则执行语句1.不符合条件接收程序。

      例子:package com.oldboy;

                import java.util.Scanner;

                 public class Test {

                      public static void main(String[] args) {   

                            @SuppressWarnings("resource")   

                            Scanner scanner=new Scanner(System.in);//从控制台输入数据   

                             @SuppressWarnings("unused")   

                             int x=scanner.nextInt();//定义一个整型从变量接收从控制台传入的数据  

                            if(x>5){    

                                System.out.println("x="+x);  

                  }

          }

}

2、if(){}else{}语句:

      格式:

             if(判断条件){

                语句块1;

               }else{

                语句块2;

             }

       解读:if(){}else{}语句是分为两种情况,当条件成立时,执行语句块1,当条件为假时,执行语句块2.

       例子:

               package com.oldboy;
                import java.util.Scanner;
                  public class Test {
                       public static void main(String[] args) {
                            @SuppressWarnings("resource")
                            Scanner scanner=new Scanner(System.in);//从控制台输入数据
                             @SuppressWarnings("unused")
                          int x=scanner.nextInt();//定义一个整型从变量接收从控制台传入的数据
                              if(x>90){
                                  System.out.println("我的成绩是优秀,我要继续加油");
                             }else{
                                  System.out.println("我要继续努力.......");
                             }
                       }
               }

3、if(){}else if{}else{};

      格式:if(判断语句){

                     语句块1;

               }else if(){

                     语句块2;

              }

              ...{

           } else{

                    语句块n;

            }

          解读:这个条件判断语句使用与多种的条件的场合。

          例如:package com.oldboy;
                       import java.util.Scanner;
                          public class Test {
                              public static void main(String[] args) {
                                   @SuppressWarnings("resource")
                                   Scanner scanner=new Scanner(System.in);//从控制台输入数据
                                 @SuppressWarnings("unused")
                                   int x=scanner.nextInt();//定义一个整型从变量接收从控制台传入的数据
                                       if(x>90 && x<100){
                                              System.out.println("我的成绩是优秀,我要继续加油");
                                         }else if(x>80 && x<89){
                                              System.out.println("我要继续努力是良好");
                                         }else if(x>=60 && x<79){
                                              System.out.println("及格");
                                         }else{
                                                System.out.println("同学好好的学习");
                                            }
                            }
                 }

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部