Only allow true to value answers from Y/N response

1560 views java
1

I have this code that creates invoice amounts. I am trying to make it so I can have a Y/N (Yes or No) question in order to continue, however, if I give it anything other than a 'Y' it quits. I want it to only accept 'Y' and 'N' as answers.

and if answered 'N' still prints string 'message'.

import java.util.Scanner;

public class InvoiceApp {

    public static void main(String[] args) {
        // welcome the user to the program
        System.out.println("Welcome to the Invoice Total Calculator");
        System.out.println();  // print a blank line

        // create a Scanner object named sc
        Scanner sc = new Scanner(System.in);

        // perform invoice calculations until choice isn't equal to "y" or "Y"
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            if (choice.equalsIgnoreCase("n")) {  // trying to make a Y/N question

            }
            // get the invoice subtotal from the user
            System.out.print("Enter subtotal:   ");
            double subtotal = sc.nextDouble();

            // calculate the discount amount and total
            double discountPercent = 0;
            if (subtotal <= 100) {
                discountPercent = .1;
            } else if (subtotal <= 200) {
                discountPercent = .2;
            } else if (subtotal >= 500) {
                discountPercent = 0.25;
            }
            double discountAmount = subtotal * discountPercent;
            double total = subtotal - discountAmount;

            // display the discount amount and total
            String message = "Discount percent: " + discountPercent + "\n"
                           + "Discount amount:  " + discountAmount + "\n"
                           + "Invoice total:    " + total + "\n";            
            System.out.println(message);

            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }
    }
}

Thanks for any help.

answered question

if (choice.equalsIgnoreCase("n")) break;?

@GBlodgett I guess that would work but at the moment any answer besides 'Y' ends the program. Example: I enter the letter 'Z' it ends.

1 Answer

1

You can do this:

 while (true){
  if(choice.equalsIgnoreCase("y"){
     System.out.print("Enter subtotal:   ");
            double subtotal = sc.nextDouble();

            // calculate the discount amount and total
            double discountPercent = 0;
            if (subtotal <= 100) {
                discountPercent = .1;
            } else if (subtotal <= 200) {
                discountPercent = .2;
            } else if (subtotal >= 500) {
                discountPercent = 0.25;
            }
            double discountAmount = subtotal * discountPercent;
            double total = subtotal - discountAmount;

            // display the discount amount and total
            String message = "Discount percent: " + discountPercent + "\n"
                           + "Discount amount:  " + discountAmount + "\n"
                           + "Invoice total:    " + total + "\n";            
            System.out.println(message);

            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
    }
    else if(choice.equalsIgnoreCase("n")){
        //....
        break;
   }
    else{
     System.out.print("Please enter (y/n): ");
            choice = sc.next();
            System.out.println();
}
}

OR:

 while (!choice.equalsIgnoreCase("n"){
  if(choice.equalsIgnoreCase("y"){
     System.out.print("Enter subtotal:   ");
            double subtotal = sc.nextDouble();

            // calculate the discount amount and total
            double discountPercent = 0;
            if (subtotal <= 100) {
                discountPercent = .1;
            } else if (subtotal <= 200) {
                discountPercent = .2;
            } else if (subtotal >= 500) {
                discountPercent = 0.25;
            }
            double discountAmount = subtotal * discountPercent;
            double total = subtotal - discountAmount;

            // display the discount amount and total
            String message = "Discount percent: " + discountPercent + "\n"
                           + "Discount amount:  " + discountAmount + "\n"
                           + "Invoice total:    " + total + "\n";            
            System.out.println(message);

            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
    }
    else{
     System.out.print("Please enter (y/n): ");
            choice = sc.next();
            System.out.println();
}
}

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories