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語句的使用。我們還為給定的問題建立了一個使用者定義的方法。

更新時間: 2023年4月25日

233 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告