Dusty
Dusty

Reputation: 11

Java, ending a applet based on choice

I'm just starting out in java and I'm trying to make my applet only stop when I type "n" or "N". If I type "y or "Y" I want it to run the other code.

Here is the code:

    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"))

    {
        // 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.0;
        if (subtotal >= 200)
            discountPercent = .2;
        else if (subtotal >= 100)
            discountPercent = .1;
        else
            discountPercent = 0.0;

        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();
    }
}

}

Upvotes: 1

Views: 46

Answers (1)

Martin Frank
Martin Frank

Reputation: 3474

if you're trying to implement an applet then you should subclass from Applet... but if you implement an Application (looks like you're doing it so right now) then you can quit your application by simply calling System.exit(0);

your programm will quit also when you simply leave your while(...)-loop, you can do it by calling

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

if("n".equalsIgnoreCase(choice){
    break;//this will make you jump out of the while loop
}

Upvotes: 1

Related Questions