-1

This is my code, here when i input the Product name i.e. Laptop/Mobile and then input the price. after that the program does not input eask and simply goes to the eask if-else case and prints the Invalid option as: enter image description here

import java.util.*;    
class Main {
          public static void main(String[] args) {
            int discount=0,ediscount=1;
            Scanner sc = new Scanner(System.in);
            String Product = sc.nextLine();
            int Price = sc.nextInt();
            String eask = sc.nextLine();
        
            if(Price>100){
              if(eask == "Yes" || eask == "No"){
                if(Product == "Mobile"){
                discount = 10;
                if(eask == "Yes")
                  ediscount=12;
              }
              else if(Product == "Laptop"){
                discount = 20;
                if(eask == "Yes")
                  ediscount=10;
              }
              else if(Product == "gadgets"){
                discount = 0;
                if(eask == "Yes")
                  ediscount=0;
              }
              else System.out.println("Invalid product");
              }
              else
                System.out.println("Invalid option");
            }
            else System.out.println("Invalid price");
        
            float TBA = discount * Price;
            System.out.println(TBA);
          }
       }
Aminator
  • 160
  • 1
  • 1
  • 11
  • 1
    Another issue in your code is [Scanner is skipping nextLine() after using next() or nextFoo()?](//stackoverflow.com/q/13102045) – Tom Dec 19 '20 at 06:40

1 Answers1

1

Try this:

        int discount=0,ediscount=1;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter product name : ");
        String Product = sc.next();
        System.out.print("Enter product price : ");
        int Price = sc.nextInt();
        System.out.print("Enter eask : ");
        String eask = sc.next();

        if(Price>100){
          if(eask.equals("Yes") || eask.equals("No") ){
              switch (Product) {
                  case "Mobile":
                  case "mobile":
                      discount = 10;
                      if("Yes".equals(eask))
                          ediscount=12;
                      break;
                  case "Laptop":
                  case "laptop":
                      discount = 20;
                      if("Yes".equals(eask))
                          ediscount=10;
                      break;
                  case "Gadgets":
                  case "gadgets":
                      discount = 0;
                      if("Yes".equals(eask))
                          ediscount=0;
                      break;
                  default:
                      System.out.println("Invalid product");
                      break;
              }
          }
          else
            System.out.println("Invalid option");
        }
        else System.out.println("Invalid price");

        float TBA = discount * Price;
        System.out.println("The price is : " + TBA);
Zach_Mose
  • 357
  • 3
  • 7