samedi 20 novembre 2021

I am doing a nested if else statement in eclipse and it only give output for the first 'if' statement [closed]

I only get output for the if (quantity >=0 && quantity <= 9 )

import java.util.Scanner; // Scanner is in the java.util package
public class Discount {

    public static void main(String[] args) {
    // Create a scanner object
    Scanner input = new Scanner(System.in);     
    
    //Prompt the user to enter number of items
    System.out.print("Enter number of items bought: ");
    int quantity = input.nextInt();
    
    //declare variables 
    double price=0;
    double discount=0;
    double disPrice=0;
    
    //if else statement for number of items
    if (quantity >= 0 && quantity <= 9) {
       System.out.print("Enter price for item bought");
       price = input.nextDouble();
       if (price >= 0.01 && price <= 10.0 ) {
       discount = 0.0;
       disPrice = price * quantity;
       System.out.println("You will get a discount " +discount+ " % and the discounted price 
       is " +disPrice);
       }
       else if (price >= 10.01 && price <= 100.0) {
       discount = 2.0;
       disPrice = price * quantity * 2.0/100.0 ;
       System.out.println("You will get a discount " +discount+ " % and the discounted price 
       is " +disPrice);
       }
       else if (price == 100.01) {
       discount = 5.0;
       disPrice = price * quantity * 5.0/100.0 ;
       System.out.println("You will get a discount " +discount+ " % and the discounted price 
       is " +disPrice);
       }
       
    else if (quantity >= 10 && quantity <= 99) {
        System.out.print("Enter price for item bought");
        price = input.nextDouble();
        if (price >= 0.01 && price <= 10.0 ) {
        discount = 5.0;
        disPrice = price * quantity * 5.0 / 100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price >= 10.01 && price <= 100.0) {
        discount = 7.0;
        disPrice = price * quantity * 7.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price == 100.01) {
        discount = 9.0;
        disPrice = price * quantity * 9.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
       
    else if (quantity >= 100 && quantity <= 499) {
        System.out.print("Enter price for item bought");
        price = input.nextDouble();
        if (price >= 0.01 && price <= 10.0 ) {
        discount = 9.0;
        disPrice = price * quantity * 9.0 / 100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price >= 10.01 && price <= 100.0) {
        discount = 15.0;
        disPrice = price * quantity * 15.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price == 100.01) {
        discount = 21.0;
        disPrice = price * quantity * 21.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
     
    else if (quantity >= 500 && quantity <= 999) {
        System.out.print("Enter price for item bought");
        price = input.nextDouble();
        if (price >= 0.01 && price <= 10.0 ) {
        discount = 14.0;
        disPrice = price * quantity * 14.0 / 100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price >= 10.01 && price <= 100.0) {
        discount = 23.0;
        disPrice = price * quantity * 23.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price == 100.01) {
        discount = 32.0;
        disPrice = price * quantity * 32.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }   

         
    else if (quantity >= 1000) {
        System.out.print("Enter price for item bought");
        price = input.nextDouble();  
        if (price >= 0.01 && price <= 10.0 ) {
        discount = 21.0;
        disPrice = price * quantity * 21.0 / 100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
         price is " +disPrice);
        }
        else if (price >= 10.01 && price <= 100.0) {
        discount = 32.0;
        disPrice = price * quantity * 32.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        else if (price == 100.01) {
        discount = 43.0;
        disPrice = price * quantity * 43.0/100.0 ;
        System.out.println("You will get a discount " +discount+ " % and the discounted 
        price is " +disPrice);
        }
        
     else System.out.println("Invalid!");
}
    }}}}}}

Aucun commentaire:

Enregistrer un commentaire