mardi 27 octobre 2020

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.

Aucun commentaire:

Enregistrer un commentaire