loopsif-statementstatements

Looping or printing a receipt


All right, so I have this program based where I want to print a receipt from a dentists office. As it is right now, you enter a number and the cost will print accordingly. However, I would like to able to enter multiple numbers in the program and when I type "-1" I want the program to stop and print the total cost. Take a look:

import java.util.Scanner;
public class DentistReception{
public static void main(String[] args) {
double cost = 0;
int treatment = 0;


final double checkUp = 60.00;
final double cleaning = 30.00;
final double cavity = 150.00;

Scanner input = new Scanner(System.in); 
System.out.println("What service(s) will be done?: ");
System.out.println("Checkup: 1");
System.out.println("Cleaning: 2");
System.out.println("Cavity: 3");
System.out.println("Exit: -1");
treatment = input.nextInt();

{

  if (treatment == 1) {
  cost = cost + checkUp;
}
else {
  if (treatment == 2) {
  cost = cost + cleaning;
}
else {
  if (treatment == 3) {
  cost = cost + cavity;
  }
  else {
    while (treatment < 0) break;
  
  }
}
}
}


System.out.println("Total cost it:"+cost);



  }
}

I want it to loop until i enter "-1", but the break doesn't seem like it wants to. Whenever I put the while or break somewhere else I get the message "break without loop" or something like that.


Solution

  • Use while and capture the option with a boolean variable

    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            double cost = 0;
            int treatment = 0;
    
            final double checkUp = 60.00;
            final double cleaning = 30.00;
            final double cavity = 150.00;
    
            boolean repeat = true;
            while (repeat) {
                Scanner input = new Scanner(System.in);
                System.out.println("What service(s) will be done?: ");
                System.out.println("Checkup: 1");
                System.out.println("Cleaning: 2");
                System.out.println("Cavity: 3");
                System.out.println("Exit: -1");
                treatment = input.nextInt();
                input.nextLine();
                switch (treatment) {
                    case 1:
                        cost = cost + checkUp;
                        break;
                    case 2:
                        cost = cost + cleaning;
                        break;
                    case 3:
                        cost = cost + cavity;
                        break;
                    default:
                        System.out.println("do you want to break out the loop?");
                        String ans = input.nextLine();
                        if (ans.equals("y")){
                            System.out.println("exiting...");
                            repeat = false;
                        }
    
    
                        break;
                }
                // if (treatment == 1) {
                // cost = cost + checkUp;
                // } else {
                // if (treatment == 2) {
                // cost = cost + cleaning;
                // } else {
                // if (treatment == 3) {
                // cost = cost + cavity;
                // } else {
                // while (treatment < 0)
                // break;
    
                // }
                // }
                // }
            }
    
            System.out.println("Total cost it:" + cost);
    
        }
    }