veronikaaa86

05. Account Balance

Mar 26th, 2022
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. package whileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05AccountBalance {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String input = scanner.nextLine();
  10.  
  11. double sum = 0;
  12. boolean isNegative = false;
  13. while (!input.equals("NoMoreMoney")) {
  14. double amount = Double.parseDouble(input);
  15.  
  16. if (amount < 0) {
  17. isNegative = true;
  18. break;
  19. }
  20.  
  21. sum = sum + amount;
  22. System.out.printf("Increase: %.2f%n", amount);
  23.  
  24. input = scanner.nextLine();
  25. }
  26.  
  27. if (isNegative) {
  28. System.out.println("Invalid operation!");
  29. }
  30.  
  31. System.out.printf("Total: %.2f", sum);
  32. }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment