2016-09-16 20 views
0

我有這一點的代碼返回到程序的開始,如果沒有預期的答案。Java -if else顯示多個輸出

... 
else // returns to start for unsatisfactory 
{ 
    System.out.println(); 
    System.out.println(); 
    System.out.println("Check your spelling and try again"); 
    main (args);          
} 
... 

但是當我輸入不同的字,然後再次通過並進入預期字詞程序輸出兩個不同的結果


薪水:加薪$ 100.00

金額:$ 4.00

您的新薪水:$ 104.00


當前工資:$ 100.00

金額給自己加薪的:$ 0.00

你的新的工資:$ 100.00


我試圖用一個else if語句可能消除的一個原因,但它造成了同樣的事情。


import java.util.Scanner; 
import java.text.NumberFormat; 

public class Salary { 
    public static void main (String[] args) { 
     double currentSalary; // employee's current salary 
     double raise = 0.0;   // amount of the raise 
     double newSalary;  // new salary for the employee 
     String rating;   // performance rating 

     Scanner scan = new Scanner(System.in); 
     System.out.print ("Enter the current salary: "); 
     currentSalary = scan.nextDouble(); 

     System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); 

     rating = scan.next(); 

     // Computes raise with if-else 
     if ((rating.equals("Excellent")) || (rating.equals("excellent"))) { 
      // calculates raise for excellent 
      raise = .06 * currentSalary; 
     } 
     else if ((rating.equals("Good")) || (rating.equals("good"))) { 
      // calculates raise for good 
      raise = .04 * currentSalary; 
     } 
     else if ((rating.equals("Poor")) || (rating.equals("poor"))) { 
      // calculates raise for poor 
      raise = .015 * currentSalary; 
     } 
     else { 
      // returns to start for unsatisfactory 
      System.out.println(); 
      System.out.println(); 
      System.out.println("Check your spelling and try again"); 
      main (args); 
     }  

     newSalary = currentSalary + raise; 

     // Print the results 

     NumberFormat money = NumberFormat.getCurrencyInstance(); 
     System.out.println(); 
     System.out.println("Current Salary:  " + money.format(currentSalary)); 
     System.out.println("Amount of your raise: " + money.format(raise)); 
     System.out.println("Your new salary:  " + money.format(newSalary)); 
     System.out.println(); 
    } 
} 
+4

不要調用主遞歸,而是使用while/for循環 – ravthiru

+1

爲什麼不使用'equalsIgnoreCase'? – Li357

+0

你可能想要寫一個體面的解析函數(不是'main')來驗證輸入。如果輸入無效,它可以返回false,在這種情況下,您可以調用另一個請求新輸入的函數(或者您想如何處理它)。我也是那種認爲用錯誤的輸入參數調用的程序最好不是全部開始的人。在這種情況下,如果用戶再次啓動程序並將其正確設置,則會更好。這並不總是成立(例如,如果輸入來自大型配置文件,例如需要修復),但通常這是一個好方法。 – patrik

回答

1

那(第二)調用您對main()「完成」,回來了到由啓動程序調用的「第一」之一。

因此,第一批結果來自您明確致電main()。第二批來自何時該通話結束,您又回到您打電話的地方。

不推薦調用main()遞歸。你應該在main()中使用while循環。即保持要求輸入,直到你知道輸入有效,然後實際使用它。

+0

什麼是更好的選擇? – moose0306

1

在您致電main (args);之後,您不會回來,因此程序的每一次迭代都將繼續。

你應該後main (args);

{ 
     System.out.println(); 
     System.out.println(); 
     System.out.println("Check your spelling and try again"); 
     main (args); 
     return; 

} 

編輯添加return;:由John3136指出你不應該叫main (args)遞歸無論是。

+0

謝謝修復它 – moose0306

+0

我假設有其他方法來檢查輸入比再次實際調用'main' ... – patrik

2

那是因爲當你沒有得到預期的輸入時,遞歸地調用main(這不被認爲是好的練習)。輸入(第二次)預期輸入後,必須仍然執行初始main的剩餘部分,然後輸入0.026作爲輸入無效時使用raise

您的問題的實用解決方案可能會避免遞歸調用main幷包裹例如在一個循環中輸入驗證像這樣

... 
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); 
while (true) { 
    rating = scan.next(); 
    if ((rating.equals("Excellent")) || (rating.equals("excellent"))) 
    { 
     raise = .06 * currentSalary; break; 
    } 
    else if ((rating.equals("Good")) || (rating.equals("good"))) 
    { 
     raise = .04 * currentSalary; break; 
    } 
    else if ((rating.equals("Poor")) || (rating.equals("poor"))) 
    { 
     raise = .015 * currentSalary; break; 
    } 
    else 
    { 
     System.out.println(); 
     System.out.println(); 
     System.out.println("Check your spelling and try again");  
    } 
} 
... 
0

你不應該叫主recursively.you應該使用do while循環,因爲我更新的代碼和它的正常工作。

import java.util.Scanner; 

    import java.text.NumberFormat; 



    public class Salary { 

    public static void main (String[] args) { 

    double currentSalary; // employee's current salary 

    double raise = 0.0;   // amount of the raise 

    double newSalary;  // new salary for the employee 

    String rating;   // performance rating 

    boolean flag=false; // to check input 


    Scanner scan = new Scanner(System.in); 


do{ 

     System.out.print ("Enter the current salary: "); 

     currentSalary = scan.nextDouble(); 

     System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); 

     rating = scan.next(); 


     // Computes raise with if-else 
     if ((rating.equals("Excellent")) || (rating.equals("excellent"))) // calculates raise for excellent 
      { 
        raise = .06 * currentSalary; 
        flag=true; 

      } 
      else if ((rating.equals("Good")) || (rating.equals("good"))) // calculates raise for good 
       { 
        raise = .04 * currentSalary; 
        flag=true; 

       } 
      else if ((rating.equals("Poor")) || (rating.equals("poor"))) // calculates raise for poor 
       { 
        raise = .015 * currentSalary; 
        flag=true; 

       } 
     else              // returns to start for unsatisfactory 
      { 
        System.out.println(); 
        System.out.println(); 
        System.out.println("Check your spelling and try again"); 
        flag=false; 

      }  

}while(!flag); 


    newSalary = currentSalary + raise; 



    // Print the results 

    NumberFormat money = NumberFormat.getCurrencyInstance(); 

    System.out.println(); 

    System.out.println("Current Salary:  " + money.format(currentSalary)); 

    System.out.println("Amount of your raise: " + money.format(raise)); 

    System.out.println("Your new salary:  " + money.format(newSalary)); 

    System.out.println(); 






     } 

    } 
0

您可能想要考慮另一種方法。如果輸入無效或嘗試修復,請終止該問題。下面的例子是「修復」的做法,

public class Salary { 
    public static void main(String[] args) { 
     IPM ipm; 
     if (verify(args)) { 
      ipm = new IPM(args); 
     } else { 
      Scanner scan = new Scanner(System.in); 
      String[] update; 
      do { 
       update = repair(scan); 
      } while (!verify(update)); // You may want a loop count as well... 
      ipm = new IPM(update); 
     } 
     ipm.print(); 
    } 

    public static boolean verify(String[] s) { 
     return IPM.verify(s); 
    } 

    public static String[] repair (Scanner s) { 
     // Request new input and store it in an array of strings. 
     // This method does not validate the input. 
    } 
} 

public class IPM { 
    double currentSalary; 
    double raise = 0.0; 
    double newSalary; 
    String rating; 

    IPM(String[] input) { 
     // Set attributes of IPM. 
    } 

    public static boolean verify(String[] s) { 
     //Determine if the input is valid. 
    } 

    public void print() { 
     // Print IPM object. 
    } 
} 

注意,從工資調用IPM.verify()。這應該是IPM責任的一部分,因爲Salary不需要知道任何關於main的內容。此外,類IPM可能會更改,並且IPM.verify()的調用將不會要求所有驗證IPM的類都被更改。