【问题标题】:i compiled my java program and run in eclipse platform and also provide input but not got the output? There is any Logical Error in program [duplicate]我编译了我的java程序并在eclipse平台上运行并且还提供输入但没有得到输出?程序中有任何逻辑错误[重复]
【发布时间】:2021-06-28 20:31:53
【问题描述】:

这是基于选择的 Java 程序。因此,在这些程序中,用户必须将 Vegetarian 提供为 V,将 Non-Vegetarian 提供为 N,数量和距离将采用整数值。因此,当我保存并运行程序时,它采用用户参数的值但没有打印输出,我还在 eclipse 编辑器中检查了错误。

#程序

'''

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

'''

【问题讨论】:

    标签: java java.util.scanner selection


    【解决方案1】:

    使用下面的代码 sn-p。注意 if("V".equals(foodType)) 而不是 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");
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多