Need help making a loop process

215 views Asked by At

so we have an assignment about a check-out counter. I want to loop back to a process if you typed an amount that is not enough for your price. I can't seem to do that because when I try to do the "do" loop, the scanned cash won't be read by "while ()". Thats all thank you.

this is the whole code, the process where my problem start is in "//amount of buyers cash"

package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
    public static void main(String[] args) {
        //choosing of product
        System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
        System.out.println("\nEnter 1 for Milk");

        //scanners and booleans for product type
        Scanner scanner = new Scanner(System.in);
        String product = scanner.nextLine();
        boolean milk = true;

        //if statements for milk
        if(milk){
            System.out.println("\nThis will cost P15 each, how much would you like to buy?");

        //scanners and booleans for product price
        Scanner scanner_price = new Scanner(System.in);
        int amount = scanner_price.nextInt();
        int price = 15 * amount;
        System.out.println("\nYour product costs P" + price);

        //amount of buyers cash
        
        System.out.println("\nPlease enter how much will you pay");
        Scanner money = new Scanner(System.in);
        int cash = money.nextInt();
        
        
        
        //if statement for not enough cash
        if(cash < price){
            System.out.println("Sorry, your cash is not enough. Please enter amount again");
            // this is the part where I want to go back to "amount of buyers cash"
        }
            

        if(cash >= price){
            int change = cash - price;
            System.out.println("\nYour change is P" + change);
            System.out.println("\nThank you! Come again next time.");
            System.exit(0);
        }
        
    }
}}
2

There are 2 answers

0
kiran On BEST ANSWER

I think you just need to change the if condition to while condition while checking if the price greater than the cash.

package prelim.activities;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
    //choosing of product
    System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
    System.out.println("\nEnter 1 for Milk");

    //scanners and booleans for product type
    Scanner scanner = new Scanner(System.in);
    String product = scanner.nextLine();
    boolean milk = true;

    //if statements for milk
    if(milk){
        System.out.println("\nThis will cost P15 each, how much would you like to buy?");

    //scanners and booleans for product price
    Scanner scanner_price = new Scanner(System.in);
    int amount = scanner_price.nextInt();
    int price = 15 * amount;
    System.out.println("\nYour product costs P" + price);

    //amount of buyers cash
    
    System.out.println("\nPlease enter how much will you pay");
    Scanner money = new Scanner(System.in);
    int cash = money.nextInt();
    
    
    
    //if statement for not enough cash
    while(cash < price){
        System.out.println("Sorry, your cash is not enough. Please enter amount again");
        // this is the part where I want to go back to "amount of buyers cash"
        Scanner money1 = new Scanner(System.in);
         cash = money1.nextInt();
    }
        

    if(cash >= price){
        int change = cash - price;
        System.out.println("\nYour change is P" + change);
        System.out.println("\nThank you! Come again next time.");
        System.exit(0);
    }
    
}
}}
0
saif On

If i'm following correctly you want to update the price each time for that you have to change the scope of the price

package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
    public static void main(String[] args) {
        //choosing of product
        System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
        System.out.println("\nEnter 1 for Milk");

        //scanners and booleans for product type
        Scanner scanner = new Scanner(System.in);
        String product = scanner.nextLine();
        boolean milk = true;

// price moved here so it will be accessible through out the method.
        int price = 0;

        //if statements for milk
        if(milk){
            System.out.println("\nThis will cost P15 each, how much would you like to buy?");

        //scanners and booleans for product price
        Scanner scanner_price = new Scanner(System.in);
        int amount = scanner_price.nextInt();
        price = 15 * amount;
        System.out.println("\nYour product costs P" + price);

        //amount of buyers cash
        
        System.out.println("\nPlease enter how much will you pay");
        Scanner money = new Scanner(System.in);
        int cash = money.nextInt();
        
        
        
        //if statement for not enough cash
        if(cash < price){
            System.out.println("Sorry, your cash is not enough. Please enter amount again");
            // this is the part where I want to go back to "amount of buyers cash"
        }
            

        if(cash >= price){
            int change = cash - price;
            System.out.println("\nYour change is P" + change);
            System.out.println("\nThank you! Come again next time.");
            System.exit(0);
        }
        
    }
}}