0

This is Selection Based Java Program. So in these program user have to provide the Vegetarian as V and Non-Vegetarian as N and it will take integer value for quantity and distance. So, when I saved and run the program it takes the value of the user parameter but didn't print the output I also check the errors in the eclipse editor.

#Program

'''

package demo;
import java.util.Scanner;

public class FoodCorner {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int vegCombo = 12;
        int nonvegCombo = 15;
        int totalCost = 0;
        int charge = 0;
        
        System.out.println("Enter the type of Food Item as Vegeterian 'V' and for Non-Vegeterian as 'N'");
        String foodType = scan.nextLine();
        System.out.println("Enter the Quantity of food Item");
        int quantity = scan.nextInt();
        System.out.println("Enter the Distance for delivery");
        float distance = scan.nextFloat();
        
        while(distance > 3) {
            charge++;
            distance = distance - 3;
        }
        
        if(distance > 0 && quantity >= 1) {         
            if(foodType == "V") {
                totalCost = (vegCombo * quantity) + charge;
                System.out.println("The total cost of your order is: "+totalCost);
            }
            else if(foodType == "N") {
                totalCost = (nonvegCombo * quantity) + charge;
                System.out.println("The total cost of your order is: "+totalCost);
            }
        }
        
        else {
            System.out.println("the bill amount is -1");
        }
        
    }
}

''' Output of the java program

Giriraj Soni
  • 23
  • 1
  • 5

1 Answers1

0

Use below code snippet. Notice the line if("V".equals(foodType)) instead of if(foodType == "V").

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int vegCombo = 12;
        int nonvegCombo = 15;
        int totalCost = 0;
        int charge = 0;

        System.out.println("Enter the type of Food Item as Vegeterian 'V' and for Non-Vegeterian as 'N'");
        String foodType = scan.nextLine();
        System.out.println("Enter the Quantity of food Item");
        int quantity = scan.nextInt();
        System.out.println("Enter the Distance for delivery");
        float distance = scan.nextFloat();

        while(distance > 3) {
            charge++;
            distance = distance - 3;
        }

        if(distance > 0 && quantity >= 1) {
            if("V".equals(foodType)) {
                totalCost = (vegCombo * quantity) + charge;
                System.out.println("The total cost of your order is: "+totalCost);
            }
            else if("N".equals(foodType)) {
                totalCost = (nonvegCombo * quantity) + charge;
                System.out.println("The total cost of your order is: "+totalCost);
            }
        }

        else {
            System.out.println("the bill amount is -1");
        }

    }
Manish Kumar
  • 595
  • 2
  • 15