Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int budget = Integer.parseInt(scanner.nextLine());
- String command = scanner.nextLine();
- while (!"Stop".equals(command)) {
- int price = 0;
- for (int i = 0; i < command.length(); i++) {
- price += command.charAt(i);
- }
- if (price <= budget) {
- budget -= price;
- System.out.println("Item successfully purchased!");
- } else {
- budget -= price;
- System.out.println("Not enough money!");
- break;
- }
- command = scanner.nextLine();
- }
- if ("Stop".equals(command)) { // other way if (budget >= 0) {
- System.out.println("Money left: " + budget);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment