Nested If/Else Statements
很抱歉再次编辑此内容,但我不想再发表任何文章了,我想我会在这里提出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | public static void main(String[] args) { // TODO code application logic here char response = 0; double grossAnnualIncome = 0.0; double annualTuition = 0.0; double annualCharity = 0.0; double homeMortgage = 0.0; double healthCredit = 0.0; double annualAfterTaxes = 0.0; double monthlyAfterTaxes = 0.0; double taxAt17 = 0.0; double taxableIncome = 0.0; double ans1 = 0.0; Scanner input = new Scanner(System.in); System.out.printf("Please enter your gross annual income or 0 for none:" ); grossAnnualIncome = input.nextDouble(); if( grossAnnualIncome > 0 ) { System.out.printf("Please enter your annual tuition and expenses for higher education or 0 for none:"); annualTuition = input.nextDouble(); System.out.printf("Please enter your annual charitable contributions or 0 for none:"); annualCharity = input.nextDouble(); System.out.printf("Please enter the annual interest paid for your home mortgage or 0 for none:"); homeMortgage = input.nextDouble(); input.nextLine(); System.out.printf("Did 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.printf("Are you filing as a family? Enter 'Y' or 'N':"); response = input.nextLine().charAt(0); if ( Character.toUpperCase(response) == 'Y' ) { healthCredit = 3500; } else { if( Character.toUpperCase(response) == 'N' ) { System.out.printf("Are you filing as single? Enter 'Y' or 'N':"); response = input.nextLine().charAt(0); } if( Character.toUpperCase(response) == 'Y') { healthCredit = 2000; } } } else { healthCredit = 0; } taxableIncome = grossAnnualIncome - homeMortgage - annualTuition - annualCharity - healthCredit; taxAt17 = taxableIncome * .17; annualAfterTaxes = grossAnnualIncome - taxAt17; monthlyAfterTaxes = annualAfterTaxes / 12; ans1 = homeMortgage + annualTuition + annualCharity + healthCredit; System.out.printf("\ YOUR TAXES\ \ " +"Gross Annual Income: %,.0f\ \ " +"Deductions: \ " +"\\tHigher Education: %,.0f\ " +"\\tCharitable Contributions: %,.0f\ " +"\\tHome Mortgage Interest: %,.0f\ " +"\\tHealth Insurance Tax Credit: %,.0f\ \ " +"Tax at 17%%: %,.0f\ " +"Annual Income After Taxes: %,.0f\ " +"Monthly Income After Taxes: %,.0f", grossAnnualIncome, annualTuition, annualCharity, homeMortgage, healthCredit, taxAt17, annualAfterTaxes, monthlyAfterTaxes); } if( grossAnnualIncome <= 0 ) { System.out.printf("You earned no income so you owe no taxes!" ); } System.exit(0); } } |
我的两个输出都是正确的,但是我在最后一个输出上遇到了麻烦。
我的最后一个输出需要读为"您欠税$ 0.00!"
当我编写代码时:
1 2 3 4 |
它显示了GrossAnnualIncome> 0输出中的所有内容。
Java:printf语句中的文字百分号
这可能与17%的百分比有关。需要转义它,可以使用另一个%
完成
1 | 17%% |