MY OUTPUT Enter the number of cash: 5 1 2 3 4 5 Total: 15 EXPECTED OUTPUT Enter the number of cash: 5 1 2 3 4 5 Total: 1+2+3+4+5 15
Please fix this java code so that it will meet the expected output (see attached photo)
import java.util.*;
public class Main {
public static void main(String args[]) {
int numOfCash;
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("Enter the number of cash: ");
numOfCash = sc.nextInt();
sc.nextLine();
if (numOfCash <= 1) {
System.out.println("Invalid");
} else {
break;
}
}
int val, total = 0;
for (int i = 0; i<numOfCash; i++) {
val = sc.nextInt();
sc.nextLine();
if (val < 1) {
System.out.println("Invalid");
i--;
continue;
}
total += val;
}
System.out.println("Total:\n" + total);
}
}
Step by step
Solved in 3 steps with 2 images