Java 用于检查TPP学生是否有资格参加面试
考虑以下表格以确定不同公司的资格标准-
CGPA | 合格公司 |
---|---|
大于或等于8 | 谷歌、微软、亚马逊、戴尔、英特尔、威普罗 |
大于或等于7 | 教程点、埃森哲、印孚瑟斯、Emicon、Rellins |
大于或等于6 | rtCamp、Cybertech、Skybags、Killer、Raymond |
大于或等于5 | Patronics、Bata、Nobroker |
让我们进入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值,它是真的。因此,它执行了第一个if块内的代码。
方法2:使用switch语句
switch语句仅适用于int、short、byte和char数据类型。它不支持小数值。它首先评估表达式,如果有任何一个case匹配,就会执行相应的代码块。如果没有一个case匹配,则执行default情况。
语法
// 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’值,case 6与表达式匹配。因此,编译器执行了case 6的代码。
方法3:使用用户定义的方法
方法是一段可以多次重复使用以执行单个操作的代码块。它节省了我们的时间,也减小了代码的大小。
语法
accessSpecifier nonAccessModifier return_Type method_Name(Parameters){
//Body of the method
}
accessSpecifier - 用于设置方法的可访问性。它可以是public、protected、default和private。
nonAccessModifier - 显示方法的其他功能或行为,例如static和final。
return_Type - 方法将返回的数据类型。当方法不返回任何内容时,使用void关键字。
method_Name - 方法的名称。
parameters - 包含变量名称,后面是数据类型。
示例
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语句的使用。我们还为给定的问题创建了一个用户定义的方法。