首页  >  文章  >  Java  >  Java程序用于检查TPP学生是否有资格参加面试

Java程序用于检查TPP学生是否有资格参加面试

王林
王林转载
2023-09-06 22:33:07888浏览

Java程序用于检查TPP学生是否有资格参加面试

请考虑下表了解不同公司的资格标准 -

的中文翻译为:

CGPA

绩点平均成绩

符合条件的公司

大于或等于8

谷歌、微软、亚马逊、戴尔、英特尔、Wipro

大于或等于7

教程点、accenture、Infosys、Emicon、Rellins

大于或等于6

rtCamp、Cyber​​tech、Skybags、Killer、Raymond

大于或等于5

Patronics、鞋子、NoBrokers

让我们进入 java 程序来检查 tpp 学生参加面试的资格。

方法1:使用if else if条件

通常,当我们必须检查多个条件时,我们会使用 if else if 语句。它遵循自上而下的方法。

语法

if(condition 1) {
   // code will be executed only when condition 1 is true
} else if(condition 2) {
   // code will be executed only when condition 2 is true
} else {
   // code will be executed when all of the above condition is false
}

示例

public class Eligible {
   public static void main(String[] args) {
      int regd = 12109659;
      double CGPA = 8.08;
      if( CGPA >= 8 ) {
         System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro");
      } else if(CGPA >= 7) {
         System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins");
      } else if(CGPA >= 6) {
         System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond");
      } else if( CGPA >= 5 ) {
         System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker");
      } else {
         System.out.println("Improve yourself!");
      }
   }
}

输出

12109659 is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wiproe

在上面的代码中,我们声明并初始化了两个名为“regd”和“CGPA”的变量。当我们运行此代码时,编译器将检查第一个 if 条件,并且对于给定的“CGPA”值,它是 true。因此,它执行了第一个 if 块内的代码。

使用Switch语句的方法

switch 语句仅适用于 int、short、byte 和 char 数据类型。它不支持十进制值。它首先评估表达式,如果有任何情况匹配,它将执行该代码块。如果没有任何情况匹配,则执行默认情况。

语法

// expression and value must be of same datatype
switch(expression) {
   case value: 
   // code will be executed only when the expression and case value matched
   break;
   case value:
   // code will be executed only when the expression and case value matched
   break; 
   .
   .
   .
   case value n: // n is required number of value
   default: 
   //  If none of the case matched then it will be executed
}

示例

public class Main {
   public static void main(String[] args){
      int regd = 12109659;
      double CGPA = 6.55;
      int GPA = (int) CGPA; 
      // typecasting double to integer type
      switch(GPA){ 
         // here GPA = 6
         case 10:
         case 9:
         case 8:
            System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro");
            break;
         case 7:
            System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins");
            break;
         case 6:
            System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond");
            break;
         case 5:
            System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker");
            break;
         default:
            System.out.println("Improve yourself!");
      }
   } 
}

输出

12109659 is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond

在上面的代码中,我们再次采用了相同的变量。由于 switch 与 double 变量不兼容,我们将其类型转换为名为“GPA”的整数类型变量。对于给定的“GPA”值,情况 6 与表达式匹配。因此,编译器执行了 case 6 代码。

方法三:使用用户自定义方法

方法是可以多次重用以执行单个操作的代码块。它节省了我们的时间,也减少了代码的大小。

语法

accessSpecifier nonAccessModifier return_Type method_Name(Parameters){
   //Body of the method
}

accessSpecifier - 用于设置方法的可访问性。它可以是公共的、受保护的、默认的和私有的。

nonAccessModifier - 它显示方法的附加功能或行为,例如静态和最终。

return_Type − 方法将返回的数据类型。当方法不返回任何内容时,我们使用void关键字。

method_Name - 方法的名称。

参数 - 它包含变量名称,后跟数据类型。

示例

public class Main {
   public static void eligible(int regd, double CGPA){
      if(CGPA >= 8){
         System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro");
      } else if(CGPA >= 7){
         System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins");
      } else if(CGPA >= 6){
         System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond");
      } else if(CGPA >= 5){
         System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker");
      } else {
         System.out.println("Improve yourself!");
      }
   }
   public static void main(String[] args){
      eligible(12109659, 7.89);
   }
}

输出

12109659 is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins

上述程序的逻辑与我们在本文中讨论的第一个程序相同。主要区别在于我们创建了一个名为“eligible()”的用户定义方法,带有两个参数“regd”和“CGPA”,并且我们在主方法中使用两个参数调用了该方法。

结论

在本文中,我们讨论了三种用于检查tpp学生是否有资格参加面试的java程序方法。我们看到了使用if else if条件和switch语句的用法。我们还为给定的问题创建了一个用户定义的方法。

以上是Java程序用于检查TPP学生是否有资格参加面试的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除