Java程序用于检查TPP学生面试资格
请考虑以下表格,了解不同公司的资格标准:
GPA |
合格公司 |
|---|---|
大于或等于8 |
谷歌、微软、亚马逊、戴尔、英特尔、威普罗 |
大于或等于7 |
教程点、埃森哲、印孚瑟斯、埃米康、瑞联 |
大于或等于6 |
rtCamp、赛博科技、Skybags、Killer、雷蒙德 |
大于或等于5 |
Patronics、Bata、诺贝克 |
让我们进入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数据类型。它不支持小数。它首先评估表达式,如果任何情况匹配,则执行该代码块。如果没有任何情况匹配,则执行默认情况。
语法
// 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
}
访问修饰符 - 用于设置方法的可访问性。它可以是public、protected、default和private。
非访问修饰符 - 显示方法的其他功能或行为,例如static和final。
返回类型 - 方法将返回的数据类型。当方法不返回任何内容时,我们使用void关键字。
方法名称 - 方法的名称。
参数 - 包含变量名称后跟数据类型。
示例
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”,并在主方法中使用两个参数调用了此方法。
结论
在本文中,我们讨论了三种方法来编写Java程序,以检查TPP学生的面试资格。我们已经了解了if else if条件和switch语句的使用。我们还为给定的问题创建了一个用户定义的方法。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP