jeudi 19 octobre 2017

Beginner Java! "Else if" wont work even tho I dont have semicolons with the "if" statements

Why won't my else if work? I read other threads and it says that there shouldn't be semicolons at the end of "if" statements and I double checked that.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Sales {

  public static void main(String[] args) {
    int quantity, input;
    double discount, total_Price;
    final double TOTAL_COST;
    String inputString, status = null;

    Scanner keyboard = new Scanner(System.in);

    inputString =
      JOptionPane.showInputDialog("Amount of packages bought: ");

    quantity = Integer.parseInt(inputString);
    discount = Integer.parseInt(inputString); {

      {
        if (quantity >= 10)
          if (quantity <= 19)
            status = "20% discount!";
        discount = .20;
      }

      {
        if (quantity < 10)
          status = "no discount!";
      }

      {
        if (quantity >= 20)
          if (quantity <= 49)
            status = "30% discount!";
        discount = .30;
      }

      {
        if (quantity >= 50)
          if (quantity <= 99)
            status = "40% discount!";
        discount = .40;
      } 

      else if (quantity > 99)
        status = "50% discount!";
      discount = .50;
    }

    total_Price = quantity * 99;

    input = quantity * 99;

    TOTAL_COST = total_Price - ((input) * discount);

    DecimalFormat formatter = new DecimalFormat("$###0.00");

    JOptionPane.showMessageDialog(null, "You get " + status + " Your total with the discount is " + formatter.format(TOTAL_COST) +
      "!");
  }

}

If there's anything else I should change please let me know. Remember I am a beginner, sorry if the layout is super confusing for you.

Aucun commentaire:

Enregistrer un commentaire