2012-03-23 32 views
0

我的程序中有一個輸出出現問題。這個問題源於我的一個輸出繞過了另一個do/while循環內的嵌套do/while循環。我可以把第一個輸出語句放在嵌套的do/while循環中。這樣做的問題是它會給我兩個輸出消息,而不是基於我的計算。不幸的是,由於一切都是主要的,因此該計劃有點長。Do/While循環輸出時出現問題

import java.util.Scanner; //CAPTURES USERS INPUT VIA THE KEYBOARD 

public class practice 
{ 

public practice() 
{ 

} 

public static void main(String[] args) 
{ 

    char response = 0; //STORES USERS RESPONSE 
    int attempt = 1; //LOOP CONTROL VARIABLE FOR OUTER DO WHILE LOOP 
    int attempts = 1; //LOOP CONTROL VARIABLE FOR INNER DO WHILE LOOP 
    double grossAnnualIncome = 0.0; //STORES USERS GROSS ANNUAL INCOME 
    double annualTuition = 0.0; //STORES USERS ANNUAL TUITION PAYMENTS 
    double annualCharity = 0.0; //STORES USERS ANNUAL DONATIONS TO CHARITY 
    double homeMortgage = 0.0; //STORES USERS ANNUAL HOME MORTGAGE PAYMENTS 
    double healthCredit = 0.0; //STORES USERS HEALTH INSURANCE CREDIT 
    double annualAfterTaxes = 0.0; //STORES USERS ANNUAL INCOME AFTER TAXES 
    double monthlyAfterTaxes = 0.0; //STORES USERS MONTHLY INCOME AFTER TAXES 
    double taxOwed = 0.0; //STORES USERS TAX RATE AT A FIXED RATE OF 17% 
    double taxableIncome = 0.0; //STORES USERS TAXABLE INCOME 
    double taxCredits = 0.0; //STORES ALL OF THE USERS TAX CREDITS ADDED TOGETHER 

    Scanner input = new Scanner(System.in); //ALLOWS THE USER TO ENTER THEIR CURRENT TAX INFORMATION 

    do 
    { 
     do 
     { 

     System.out.print("\nPlease enter your gross annual income or 0 for none: "); //PROMPT 1 
     grossAnnualIncome = input.nextDouble(); 

      if(grossAnnualIncome > 0) 
      { 

      System.out.print("\nPlease enter your annual tuition and expenses for higher education or 0 for none: "); 
      annualTuition = input.nextDouble(); 

     System.out.print("\nPlease enter your annual charitable contributions or 0 for none: "); 
      annualCharity = input.nextDouble(); 

      System.out.print("\nPlease enter the annual interest paid for your home mortgage or 0 for none: "); 
      homeMortgage = input.nextDouble(); 
      input.nextLine(); 

      System.out.print("\nDid you purchase health insurance through your employer or outside the workplace?" 
       + " Enter 'Y' or 'N': "); 
     response = input.nextLine().charAt(0); 


       if(Character.toUpperCase(response) == 'Y') 
       { 

       System.out.print("\nAre you filing as a family? Enter 'Y' or 'N': "); //PROMPT 6 
       response = input.nextLine().charAt(0); 

        if (Character.toUpperCase(response) == 'Y') 
        { 

        healthCredit = 3500; 
        } 

       else 
       { 

        if(Character.toUpperCase(response) == 'N') 
         { 

         System.out.print("\nAre you filing as single? Enter 'Y' or 'N': "); //PROMPT 7 
         response = input.nextLine().charAt(0); 

         } 

          if(Character.toUpperCase(response) == 'Y') 
          { 

          healthCredit = 2000; 
          } 

       } 

       } 
       else 
       { 

       healthCredit = 0; 
       } 

       System.out.printf("\nAre the following entries correct?\n\n" 
          + "Gross Annual Income: $%,.0f\n\n" 
          + "Deductions: \n" 
          + "\tHigher Education: %,.0f\n" 
          + "\tCharitable Contributions: %,.0f\n" 
          + "\tHome Mortgage Interest: %,.0f\n" 
          + "\tHealth Insurance Tax Credit: %,.0f\n\n", grossAnnualIncome, annualTuition,         annualCharity, homeMortgage, healthCredit); 

       System.out.print("\nEnter 'Y' or 'N': "); 
       response = input.nextLine().charAt(0); 
      } 

      if(Character.toUpperCase(response) == 'Y') 
     { 

      --attempts; 

     } 

     }while(attempts == 1); 


       //CALCULATIONS 
    taxableIncome = grossAnnualIncome - taxCredits; 
     taxOwed = taxableIncome * .17; //TAKES TAXABLE INCOME AND MULTIPLIES IT BY A 17% FLAT TAX RATE 
     annualAfterTaxes = grossAnnualIncome - taxOwed; //TAKES USERS GROSS ANNUAL INCOME SUBTRACTED BY 17% TAX BRACKET 
     monthlyAfterTaxes = annualAfterTaxes/12; //DIVIDES THE USERS ANNUAL GROSS AFTER TAXES BY 12 FOR MONTHLY GROSS 
     taxCredits = annualTuition + annualCharity + homeMortgage + healthCredit; //ADDS UP THE USERS TOTAL ANNUAL TAX 

    if(grossAnnualIncome == 0) 
    { 

    System.out.print("\nYou earned no income so you owe no taxes!"); //GROSS ANNUAL INCOME EQUALS ZERO OUTPUT 
    input.nextLine(); 
    } 

    else if(grossAnnualIncome <= taxCredits) 
    { 

    System.out.print("\nYOU OWE $0.00 IN TAXES!"); //GROSS ANNUAL INCOME LESS THAN OR EQUAL TO TAX CREDITS OUTPUT 
    } 

    else 
    { 


      //GROSS ANNUAL INCOME GREATER THAN ZERO OUTPUT 
    System.out.printf("\n\nYOUR TAXES\n\n" 
        + "Gross Annual Income: $%,.0f\n\n" 
        + "Deductions: \n" 
        + "\tHigher Education: %,.0f\n" 
        + "\tCharitable Contributions: %,.0f\n" 
        + "\tHome Mortgage Interest: %,.0f\n" 
        + "\tHealth Insurance Tax Credit: %,.0f\n\n" 
        + "Tax at 17%%: $%,.0f\n" 
        + "Annual Income After Tax: $%,.0f\n" 
        + "Monthly Income After Tax: $%,.0f", grossAnnualIncome, annualTuition, annualCharity, 
         homeMortgage, healthCredit, taxOwed, annualAfterTaxes, monthlyAfterTaxes); 

    } 

System.out.print("\n\nDo you want to calculate taxes for someone else? Enter 'Y' or 'N' "); 
    response = input.nextLine().charAt(0); 

    if(Character.toUpperCase(response) == 'N') 
    { 

     --attempt; 
    } 

}while(attempt == 1); 

System.exit(0); 

} 

} 

再一次,我很抱歉代碼是多久。我有確切的問題是,當用戶在第一次提示進入0(不進入嵌套的do/while循環),它只是循環:

Please enter your gross annual income or 0 for none: 0 

Please enter your gross annual income or 0 for none: 0 

Please enter your gross annual income or 0 for none: 0 

現在,如果我動議:

if(grossAnnualIncome == 0) 
{ 

System.out.print("\nYou earned no income so you owe no taxes!"); 
input.nextLine(); 
break; 
} 

進入嵌套do/while循環我得到:

Please enter your gross annual income or 0 for none: 0 

You earned no income so you owe no taxes! 
YOU OWE $0.00 IN TAXES! 

它應該只讀取第一個輸出。

正確的輸出應該是這樣的:

Please enter your gross annual income or 0 for none: 0 

You earned no income so you owe no taxes! 

預先感謝您。

+1

我現在沒有機會將其插入編譯器,但嘗試將'attempt'和'attempts'的默認值更改爲'0'而不是'1'。 – edthethird 2012-03-23 03:11:20

+0

嗯,它修復了一件事,搞砸了另一件事。我認爲這是正確的道路。可能需要稍微調整一下邏輯。 – Abweichung 2012-03-23 03:25:55

回答

1

如果用戶輸入'0',則沒有任何內部if語句被擊中,嘗試不會改變,因此您繼續運行相同的循環。

更新: 讓我先說這是可怕的,可怕的錯誤,但這應該工作。我將你的循環控制改爲布爾值,並且移動了一些東西。除了在這裏完全沒有發生oop(實體類會更好),如果用戶選擇輸入,則您的掃描器輸入將表現不可預測。 這絕不是防彈的。

import java.util.Scanner; //CAPTURES USERS INPUT VIA THE KEYBOARD 

public class PracticeFromStackOverFlow 
{ 

public PracticeFromStackOverFlow() 
{ 

} 

public static void main(String[] args) 
{ 

    char response = 0; //STORES USERS RESPONSE 
    boolean outerLoop = true; //LOOP CONTROL VARIABLE FOR OUTER DO WHILE LOOP 
    boolean innerLoop = true; //LOOP CONTROL VARIABLE FOR INNER DO WHILE LOOP 
    double grossAnnualIncome = 0.0; //STORES USERS GROSS ANNUAL INCOME 
    double annualTuition = 0.0; //STORES USERS ANNUAL TUITION PAYMENTS 
    double annualCharity = 0.0; //STORES USERS ANNUAL DONATIONS TO CHARITY 
    double homeMortgage = 0.0; //STORES USERS ANNUAL HOME MORTGAGE PAYMENTS 
    double healthCredit = 0.0; //STORES USERS HEALTH INSURANCE CREDIT 
    double annualAfterTaxes = 0.0; //STORES USERS ANNUAL INCOME AFTER TAXES 
    double monthlyAfterTaxes = 0.0; //STORES USERS MONTHLY INCOME AFTER TAXES 
    double taxOwed = 0.0; //STORES USERS TAX RATE AT A FIXED RATE OF 17% 
    double taxableIncome = 0.0; //STORES USERS TAXABLE INCOME 
    double taxCredits = 0.0; //STORES ALL OF THE USERS TAX CREDITS ADDED TOGETHER 

    Scanner input = new Scanner(System.in); //ALLOWS THE USER TO ENTER THEIR CURRENT TAX INFORMATION 

    while(outerLoop) 
    { 
     innerLoop = true; // reset this every time you 
     while(innerLoop) { 

      System.out.print("\nPlease enter your gross annual income or 0 for none: "); //PROMPT 1 
      grossAnnualIncome = input.nextDouble(); 

      if(grossAnnualIncome > 0) { 

       System.out.print("\nPlease enter your annual tuition and expenses for higher education or 0 for none: "); 
       annualTuition = input.nextDouble(); 

       System.out.print("\nPlease enter your annual charitable contributions or 0 for none: "); 
       annualCharity = input.nextDouble(); 

       System.out.print("\nPlease enter the annual interest paid for your home mortgage or 0 for none: "); 
       homeMortgage = input.nextDouble(); 
       input.nextLine(); 

       System.out.print("\nDid you purchase health insurance through your employer or outside the workplace?" 
         + " Enter 'Y' or 'N': "); 
       response = input.nextLine().charAt(0); 
       healthCredit = 0; 

       if(Character.toUpperCase(response) == 'Y') { 

        System.out.print("\nAre you filing as a family? Enter 'Y' or 'N': "); //PROMPT 6 
        response = input.nextLine().charAt(0); 


        if (Character.toUpperCase(response) == 'Y') { 

         healthCredit = 3500; 
        } else { // do you really need this? 

         if(Character.toUpperCase(response) == 'N') { 

          System.out.print("\nAre you filing as single? Enter 'Y' or 'N': "); //PROMPT 7 
          response = input.nextLine().charAt(0); 
         } 

         if(Character.toUpperCase(response) == 'Y') { 

          healthCredit = 2000; 
         } 

        } 

       } 

       System.out.printf("\nAre the following entries correct?\n\n" 
         + "Gross Annual Income: $%,.0f\n\n" 
         + "Deductions: \n" 
         + "\tHigher Education: %,.0f\n" 
         + "\tCharitable Contributions: %,.0f\n" 
         + "\tHome Mortgage Interest: %,.0f\n" 
         + "\tHealth Insurance Tax Credit: %,.0f\n\n", grossAnnualIncome, annualTuition,         annualCharity, homeMortgage, healthCredit); 

       System.out.print("\nEnter 'Y' or 'N': "); 
       response = input.nextLine().charAt(0); 

       if(Character.toUpperCase(response) == 'Y'){ 
        innerLoop = false; 

       } 
      } else { 
       innerLoop = false; 
      } 
     } 


       //CALCULATIONS 
    taxableIncome = grossAnnualIncome - taxCredits; 
     taxOwed = taxableIncome * .17; //TAKES TAXABLE INCOME AND MULTIPLIES IT BY A 17% FLAT TAX RATE 
     annualAfterTaxes = grossAnnualIncome - taxOwed; //TAKES USERS GROSS ANNUAL INCOME SUBTRACTED BY 17% TAX BRACKET 
     monthlyAfterTaxes = annualAfterTaxes/12; //DIVIDES THE USERS ANNUAL GROSS AFTER TAXES BY 12 FOR MONTHLY GROSS 
     taxCredits = annualTuition + annualCharity + homeMortgage + healthCredit; //ADDS UP THE USERS TOTAL ANNUAL TAX 

    if(grossAnnualIncome == 0) { 

     System.out.print("\nYou earned no income so you owe no taxes!"); //GROSS ANNUAL INCOME EQUALS ZERO OUTPUT 
     input.nextLine(); 
    } 

    else if(grossAnnualIncome <= taxCredits) { 
     System.out.print("\nYOU OWE $0.00 IN TAXES!"); //GROSS ANNUAL INCOME LESS THAN OR EQUAL TO TAX CREDITS OUTPUT 
    } 

    else { 


      //GROSS ANNUAL INCOME GREATER THAN ZERO OUTPUT 
    System.out.printf("\n\nYOUR TAXES\n\n" 
        + "Gross Annual Income: $%,.0f\n\n" 
        + "Deductions: \n" 
        + "\tHigher Education: %,.0f\n" 
        + "\tCharitable Contributions: %,.0f\n" 
        + "\tHome Mortgage Interest: %,.0f\n" 
        + "\tHealth Insurance Tax Credit: %,.0f\n\n" 
        + "Tax at 17%%: $%,.0f\n" 
        + "Annual Income After Tax: $%,.0f\n" 
        + "Monthly Income After Tax: $%,.0f", grossAnnualIncome, annualTuition, annualCharity, 
         homeMortgage, healthCredit, taxOwed, annualAfterTaxes, monthlyAfterTaxes); 

    } 

    System.out.print("\n\nDo you want to calculate taxes for someone else? Enter 'Y' or 'N' "); 
    response = input.nextLine().charAt(0); 

    if(Character.toUpperCase(response) == 'N') { 
     System.out.print("Laters"); 
     outerLoop = false; 
    } 

} 

System.exit(0); 

} 

} 
+0

那麼,我應該移動,如果== 0語句嵌套的do/while?我唯一的問題是它打印兩條語句而不是一條,如上所示。 – Abweichung 2012-03-23 03:34:40

+0

我複製了上面的代碼,初始化嘗試到0並運行它,我沒有得到額外的味精。這不會是完全正確的,你必須修復嘗試邏輯 - 如果你使用它來控制,爲什麼不使用布爾值? – gebuh 2012-03-23 03:59:43

+0

任何想法如何讓我的最後一個提示在我的外部do/while循環再次工作?輸入0後,它會顯示消息,然後應詢問您是否想要執行更多稅收 – Abweichung 2012-03-23 04:22:59

0

該功能非常難以閱讀。你應該做的第一件事是重構它的方法。我相信你可以在不需要嵌套循環的情況下實現它。你花費在調試這樣的醜陋循環上的時間不僅僅是把它分解成更容易理解的更易於管理的塊。

+0

這似乎是應該發表評論,而不是回答,不是? – ulmangt 2012-03-23 03:16:19

+0

是的,我知道代碼是非常不可取的。這是一項任務,我的老師暫時想要一切。我已經在JCreator中正確地編碼和隔開了一切,但是當我把它粘貼在這裏時會變得很難看。 – Abweichung 2012-03-23 03:16:37