2016-03-08 102 views
0

我創建了一個加班計算器,如果用戶輸入的時間超過49小時並回到用戶輸入使用小時的位置,則會向用戶發出警告有效的。如何將用戶循環回到具有兩個循環的程序開始

如果有人輸入正確的小時數並想要發出另一個查詢,我該如何在循環內發出循環?因爲它目前已經設置在一個循環中。

特異性:

System.out.println ("Would you like to view another account? Y/N: "); 
inputLine.next().charAt(0); 

下面是完整的代碼診斷:

import java.util.Scanner; 

public class Overtime 
{ 
    public static void main() 
    { 
     Scanner inputLine = new Scanner(System.in); 

     String staffName; 
     int i = 1; 

     double hoursWorked = 0, hourlyRate = 15, totalPay = 0, nationalInsurance = 0, tax = 0, netPay = 0, overtime = 0; 

     System.out.print("Enter employee name: "); 

     staffName = inputLine.nextLine(); 
     do 
     { 
      System.out.print("Enter number of hours worked: "); 

      hoursWorked = inputLine.nextFloat(); 
      if(hoursWorked <= 36) 
      { 
       totalPay = (hourlyRate * hoursWorked); 
      } 
      else if(hoursWorked >= 37 && hoursWorked <= 40) 
      { 
       totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5); 
      } 
      else //(hoursWorked > 41) 
      { 
       totalPay = (hourlyRate * 36) + (41 - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2); 
      } 

      if(hoursWorked >= 37 && hoursWorked <= 40) 
      { 
       overtime = (hoursWorked - 36) * (hourlyRate * 1.5); 
      } 
      else if(hoursWorked > 41) 
      { 
       overtime = (41 - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2); 
      } 

      if(totalPay > 155) 
      { 
       nationalInsurance = (totalPay * 0.12); 
      } 


      tax = (totalPay * 0.20); 
      netPay = (totalPay - tax - nationalInsurance); 

      if(hoursWorked >= 49) 
      { 
       System.out.println("You are not legally allowed to work over 48 hours! "); 
      } 

      else 
      { 
       System.out.println("***********************"); 
       System.out.println("Employee: " + staffName); 
       System.out.println("Total Hours Worked: " + hoursWorked); 
       System.out.println("Overtime Pay: " + overtime); 
       System.out.println("Net Pay: " + totalPay); 
       System.out.println("Tax: " + tax); 
       System.out.println(
         "National insurance: " + (nationalInsurance = Math.round(nationalInsurance * 100.00)/100.00)); 
       System.out.println("Net Pay" + netPay); 
       i++; 
       System.out.println("Would you like to view another account? Y/N: "); 
       inputLine.next().charAt(0); 

       //System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2)); 
       //System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100)))); 
      } 
     } 
     while(i == 1); 
    } 

} 

回答

0

或許你也應該看調用inputLine.next()的結果的charAt(0);

您可以將所有內容放在另一個循環中,您可以根據該調用的輸出制定條件。

但是,我強烈建議您重構此代碼,以便在閱讀代碼時使其符合邏輯意義,而不是在一次大的方法調用中完成。

0

我建議你看看下面的代碼,並將它與你自己的代碼進行比較。從這裏你可以看到差異,循環更有意義。如果用戶不再想要輸入更多帳戶,則按N將退出循環。

public static void main(String args[]) 
{ 
    Scanner inputLine = new Scanner(System.in); 
    String staffName, quit; 
    double hoursWorked = 0, hourlyRate = 15, totalPay = 0, nationalInsurance = 0, tax = 0, netPay = 0, overtime = 0; 

    do 
    { 
     System.out.print("Enter employee name: "); 
     staffName = inputLine.next(); 

     System.out.print("Enter number of hours worked: "); 
     hoursWorked = inputLine.nextFloat(); 

     /** 
     * All other logic. 
     */ 

     if(hoursWorked >= 49) 
     { 
      System.out.println("You are not legally allowed to work over 48 hours! "); 
     } 
     else 
     { 
      System.out.println("The output"); 
     } 

     System.out.println ("Would you like to create another account? Y/N: "); 
     quit = inputLine.next(); 
    } 
    while(!quit.equalsIgnoreCase("N")); 
} 
0

您可以更改列表行你做塊到:

if(inputLine.nextLine().charAt(0)!='Y') i=0; 

所以,如果用戶進入任何(或別的東西),那麼我的價值將得到改變爲0,循環將打破。

甲替代性和良好的方法是使用我作爲char變量和初始設定其值設定爲「Y」,然後使用

i=inputLine.nextLine().charAt(0); 

while(i=='Y'||i=='y'); 

此外,代碼當不可不要做過多和不必要的事情,那將是一個糟糕的設計! 如果小時數大於48​​,則無需計算任何數據,並且應該循環回到用戶將再次輸入小時的位置。所以,在輸入之後檢查是否超過了48小時。如果它小於49,則在一個塊中進行所有計算並打印並詢問用戶是否想輸入另一個輸入。如果小時> 48,則打印該信息無效並再次詢問用戶輸入。

下面的代碼給它

import java.util.*; 

public class Overtime 
{ 
public static void main() 
{ 
    Scanner inputLine = new Scanner(System.in); 

    String staffName; 
    char i = 'Y'; 

    double hoursWorked = 0, hourlyRate = 15, totalPay=0, nationalInsurance=0, tax=0, netPay=0, overtime=0; 

    System.out.print ("Enter employee name: "); 

    staffName = inputLine.nextLine(); 
    do{ 
    System.out.print ("Enter number of hours worked: "); 

    hoursWorked = inputLine.nextFloat(); 

    if (hoursWorked >= 49) 
    { 
    System.out.println ("You are not legally allowed to work over 48 hours! "); 
    } 

    else 
    { 


    if (hoursWorked <= 36) 
    { 
     totalPay = (hourlyRate * hoursWorked); 
    } 
    else if (hoursWorked >= 37 && hoursWorked <= 40) 
    { 
     totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5); 
    } 
    else //(hoursWorked > 41) 
    { 
     totalPay = (hourlyRate * 36) + (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2); 
    } 

    if (hoursWorked >= 37 && hoursWorked <= 40) 
    { 
     overtime = (hoursWorked - 36) * (hourlyRate * 1.5); 
    } 
    else if (hoursWorked > 41) 
    { 
     overtime = (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate *2); 
    } 

    if (totalPay > 155) 
    { 
     nationalInsurance = (totalPay * 0.12); 
    } 


    tax = (totalPay * 0.20); 
    netPay = (totalPay - tax - nationalInsurance); 

    System.out.println ("***********************"); 
    System.out.println ("Employee: " + staffName); 
    System.out.println ("Total Hours Worked: " + hoursWorked); 
    System.out.println ("Overtime Pay: " + overtime); 
    System.out.println ("Net Pay: " + totalPay); 
    System.out.println ("Tax: " + tax); 
    System.out.println ("National insurance: " + (nationalInsurance = Math.round(nationalInsurance * 100.00)/ 100.00)); 
    System.out.println ("Net Pay" + netPay); 
    i++; 
    System.out.println ("Would you like to view another account? Y/N: "); 
    i=inputLine.nextLine().charAt(0); 

    //System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2)); 
    //System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100)))); 
    } 
    } 
    while(i == 'Y'||i=='y'); 
}