如何解决我编译了我的java程序并在eclipse平台上运行并提供了输入但没有得到输出?程序中存在任何逻辑错误
这是基于选择的 Java 程序。所以在这些程序中,用户必须提供素食者为 V,非素食者为 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");
}
}
}
''' this
解决方法
使用下面的代码片段。注意 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");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。