Java 程序:If Else

原文:https://www.studytonight.com/java-programs/java-if-else-program

在这个程序中,我们将使用 java 中的 if-else 语句执行各种程序。但是在继续之前,如果你不熟悉 java 中 if 语句的概念,那么一定要查看主题为Java 中条件语句的文章。

语法

if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}

在本教程中,我们将看到三种 if 语句的程序:

  1. if 语句

  2. if-else 语句

  3. 如果-否则-如果梯子

让我们分别看看这些程序。

程序 1:如果用 Java 编程

在这个程序中,我们将看到 if 语句在 java 中的实现。在这里,我们将根据一个人的年龄来决定他/她是否有资格投票。

算法:

  1. 开始吧。

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储人的年龄。

  4. 要求用户初始化变量。

  5. 使用 if 语句检查此人是否有资格投票。

  6. 如果输入的年龄大于等于 18 岁,则他有资格投票。

  7. 显示结果。

  8. 停下来。

下面是 if 条件程序的 Java 代码。

//Java Program for implementation of if statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the age: ");
         int age=sc.nextInt();
         //Determine whether the person is eligible to vote or not
          if(age>=18)
          {  
                System.out.println("The person is eligible to vote");  
          }     
     }
}

输入年龄:21 此人有资格投票

程序 2: Java If-Else 程序

在这个程序中,我们将看到 if-else 语句在 java 中的实现。这里,我们将使用 if-else 语句来确定输入的数字是正数还是负数。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储数字。

  4. 要求用户初始化数字。

  5. 使用 if-else 语句检查输入的数字是正数还是负数。

  6. 如果输入的数字大于 0,则将其打印为正数。

  7. 否则,如果输入的数字小于 0,则将其打印为负数。

  8. 显示结果。

  9. 停下来。

下面是 if-else 条件程序的 Java 代码。

//Java Program for implementation of if-else statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter a number: ");
         int num=sc.nextInt();
         if(num>0)
         {
             System.out.println("The entered number "+num+" is positive.");
         }
         else
         {
             System.out.println("The entered number "+num+" is negative.");
         }
     }
}

输入数字:-9 输入的数字-9 为负数。

程序 3: Java If-else-if 程序

在这个程序中,我们将看到 if-else-if 阶梯在 java 中的实现。在这里,我们将根据一个学生获得的分数来确定他的分数。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储获得的标记。

  4. 要求用户初始化变量。

  5. 使用 if-else-if 语句来确定从输入的分数中获得的分数。

  6. 如果标记小于 50,打印为失败。

  7. 否则,如果标记在 50 到 60 之间,打印为 D 级。

  8. 否则,如果标记在 60 到 70 之间,打印为 C 级。

  9. 否则,如果标记在 70 到 80 之间,打印为 B 级。

  10. 否则,如果分数在 80 到 90 之间,打印为 A 级。

  11. 否则,如果分数在 90 到 100 之间,打印为 A+级。

  12. 否则打印为无效。

  13. 显示结果。

  14. 停下来。

下面是 if-else 条件程序的 Java 代码。

//Java Program for implementation of if-else-if statement
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
         //Create an instance of the Scanner class
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the marks obtained: ");
         int num=sc.nextInt();
         //Determine the grades from the marks obtained
          if(num<50)
          {  
                System.out.println("fail");  
          }  
          else if(num>=50 && num<60)
          {  
                System.out.println("D grade");  
          }  
          else if(num>=60 && num<70)
          {  
               System.out.println("C grade");  
          }  
          else if(num>=70 && num<80)
          {  
               System.out.println("B grade");  
          }  
          else if(num>=80 && num<90)
          {  
              System.out.println("A grade");  
          }
          else if(num>=90 && num<100)
          {  
              System.out.println("A+ grade");  
          }
          else
          {  
             System.out.println("Invalid!");  
          }  
     }
}

输入获得的分数:78 B 级