Respuesta :
Answer:
Fixed code is as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int sum = 0; int count = 0;
int total = 0;
Scanner console = new Scanner(System.in);
System.out.print("Is your money multiplied 1 or 2 times?");
int times = console.nextInt();
System.out.print("And how much are you contributing? ");
int donation = console.nextInt();
if (times == 1){
sum = sum + donation;
}
if (times == 2){
sum = sum + 2 * donation;
}
count++;
total = total + sum;
System.out.println("The total is " + total);
}
}
sample output:
Is your money multiplied 1 or 2 times?2
And how much are you contributing? 1
The total is 2
Explanation:
In the above code following changes are performed:
1. The statement
System.out.print("And how much are you contributing? ");
int donation = console.nextInt();
was repeating in both if blocks. So better to provide them before the if statement since these are only used to read the value of donation.
2. The statement
count++;
total = total + sum;
was also repeating in both if blocks. So better to provide them after the if block since both of these are trying to increment two independent variable count and total.
The code is better structured using indentation rules.