我的程序中有一个输出问题。问题源于我的一个输出绕过另一个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!
提前谢谢。
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
这个功能很难阅读。你应该做的第一件事是将它重构为方法。我确信你可以实现它,而不需要像这样的嵌套循环。你花在尝试调试丑陋循环上的时间将超过仅仅将其分解为更易于管理的块,这可以更容易理解。