Concept
How can a Java program read income and children inputs while rejecting negative values?
ComputerScienceOne / Life & Taxes
"27.3.2. Life & Taxes Let’s adapt the conditional statements we developed in Section 3.6.4 into a full Java program. The first thing we need to do is establish the variables we’ll need and read them in from the user. At the same time we can check for bad input (negative values) for both the inputs.\n\n1 Scanner s = new Scanner(System.in);\n\n2 double income, baseTax, numChildren, credit, totalTax;\n\n3\n\n4 System.out.println(\"Please enter your Adjusted Gross Income: \");\n\n5 income = s.nextDouble();\n\n6\n\n7 System.out.println(\"How many children do you have?\");\n\n8 numChildren = s.nextDouble();\n\n9\n\n10 if(income < 0 || numChildren < 0) {\n\n11 System.out.println(\"Invalid inputs\");\n\n12 System.exit(1);\n\n13 }"
Related Ideas
- How can conditional statements be used to build a life and taxes program?ComputerScienceOne · Life & Taxes
- How can PHP conditionals validate income and calculate life and taxes?ComputerScienceOne · Life & Taxes
- How is the child tax credit calculated and limited by total tax liability?ComputerScienceOne · Life & Taxes
- How can conditionals calculate tax brackets, credits, and total tax?ComputerScienceOne · Quadratic Roots Revisited
- How can conditionals prevent a calculated tax liability from becoming negative?ComputerScienceOne · Quadratic Roots Revisited
- How do you read numeric input from a user in Java with Scanner?ComputerScienceOne · Converting Units
- What are examples of if, if-else, and if-else-if statements in Java?ComputerScienceOne · If, If-Else, If-Else-If Statements
- How do you catch and handle an exception in Java?ComputerScienceOne · Catching Exceptions