Question: The following code is poorly structured: Rewrite it so that it has a better structure and avoids redundancy. To simplify things, you may assume that

The following code is poorly structured:

int sum = 1000; Scanner console new Scanner (System.in); !! System.out.print (

Rewrite it so that it has a better structure and avoids redundancy. To simplify things, you may assume that the user always types 1 or 2. (How would the code need to be modified to handle any number that the user might type?)

int sum = 1000; Scanner console new Scanner (System.in); !! System.out.print ("Is your money multiplied 1 or 2 times? "); int times = console.nextInt (): if (times 1) { System.out.print ("And how much are you contributing? "); int donation = console.nextInt (); sum = sum + donation; countl++; total = total + donation; if (times 2) { %3D System.out.print ("And how much are you contributing? "); int donation = console.nextInt (); sum = sum + 2 * donation; count2++; total = total + donation;

Step by Step Solution

3.38 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Refactored code to reduce redundancy If the user could typ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!