Java 这是将字符串输入if语句的正确语法,还是完全关闭了?

Java 这是将字符串输入if语句的正确语法,还是完全关闭了?,java,oop,for-loop,Java,Oop,For Loop,成功 else if(type.equals(“gas”))这有4个问题: 行}else if(type.equals(“gas:”)需要另一个)在末尾 在“gas”情况下,您使用的是变量距离,但您没有给它一个值 虽然if(type.equals(“elec”))是正确的语法(回答您的问题),但通常最好编写if(“elec.equals(type)),因为这样不会引发NullPointerExceptioniftype==null 它应该是“gas”,而不是“gas: 正如Paul提到的,if语

成功
else if(type.equals(“gas”))
这有4个问题:

  • }else if(type.equals(“gas:”)
    需要另一个
    在末尾
  • “gas”
    情况下,您使用的是变量
    距离
    ,但您没有给它一个值
  • 虽然
    if(type.equals(“elec”))
    是正确的语法(回答您的问题),但通常最好编写
    if(“elec.equals(type))
    ,因为这样不会引发
    NullPointerException
    if
    type==null
  • 它应该是
    “gas”
    ,而不是
    “gas:

  • 正如Paul提到的,if语句语法是正确的,但最好从硬编码字符串(“elec”和“gas”)开始,以避免NullPointerException。如其他答案所述,if-else应该使用“gas”而不是“gas:”。为了避免此类错误,您可以考虑将“EURC”和“GAS”放入静态最终字符串常量中。如果您使用常量,您将知道它们在整个程序中都是相同的。如果用户以大写形式输入响应,您可能还需要调用type.toLowerCase()。

    在Paul提到的更正之后,下面是完整的代码:

    travelCost.java

    package travelCost;
    
    import java.util.Scanner;
    
    public class travelCost {
    
        public static void main(String[] args) {
            //Scanner function
            Scanner in = new Scanner(System.in);
            //define problem variables
            //first
            double distance;
            double mpg;
            double pricePerGallon;
            double milesPerKwh;
            double pricePerKwh;
            double totalCostGas;
            double totalCostElec;
            String type;
    
    
    
    
    //Here i want the user to input a string and then based upon the answer //section into the for loop
            System.out.println("Enter whether the car is 'elec' or 'gas': ");
    
    
            type = in.next();
            if (type.equals("elec"))
            {
    
                System.out.println("Enter the Total Distance in Miles: ");
                distance = in.nextDouble();
                System.out.println("Enter the total Miles per Kwh: ");
                milesPerKwh = in.nextDouble();
                System.out.println("Enter the Total Price per Kwh: ");
                pricePerKwh = in.nextDouble();
                totalCostElec = (distance/milesPerKwh) * pricePerKwh;
                System.out.printf("The trip is going to cost $%5.2f: ", totalCostElec);
    
            } else if (type.equals("gas: ")
            {
                System.out.println("Enter the Miles per Gallon: ");
                mpg = in.nextDouble();
                System.out.println("Enter the total Price per Gallon of Gasoline: ");
                pricePerGallon = in.nextDouble();
                System.out.println("Enter the total Price per Gallon of Gasoline: ");
                pricePerGallon = in.nextDouble();
                totalCostGas = (distance/mpg) * pricePerGallon;
                System.out.printf("The trip is going to cost $%5.2f", totalCostGas);
    
            }else
            {
                System.out.println("Please resubmit entry");
            }
    
            System.out.println();
    
    
    
    
    
    
        }
    }
    
    输入:

    电气100 102

    输出:

    这次旅行将花费20美元:


    type.equals(“gas:”)
    永远不会为真,应该使用
    “gas”
    作为字符串-查看它将返回什么。现在就认识到这一点。谢谢ton@AdrianStubbs,如果你成功地解决了问题,请标记答案或关闭问题。否则,ppl将需要再次进行故障排除:)Thx这不提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。@1l13v:Hi,这是成功运行程序的答案。因为他/她犯了语法错误,即缺少
    。也冗余
    。因此,如果块
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            double distance;
            double mpg;
            double pricePerGallon;
            double milesPerKwh;
            double pricePerKwh;
            double totalCostGas;
            double totalCostElec;
            String type;
    
            System.out.println("Enter whether the car is 'elec' or 'gas': ");
    
            type = in.next();
            if (type.equals("elec")) {
                System.out.println("Enter the Total Distance in Miles: ");
                distance = in.nextDouble();
                System.out.println("Enter the total Miles per Kwh: ");
                milesPerKwh = in.nextDouble();
                System.out.println("Enter the Total Price per Kwh: ");
                pricePerKwh = in.nextDouble();
                totalCostElec = (distance / milesPerKwh) * pricePerKwh;
                System.out.printf("The trip is going to cost $%5.2f: ",
                        totalCostElec);
    
            } else if (type.equals("gas")) {
                System.out.println("Enter the Total Distance in Miles: ");
                distance = in.nextDouble();
                System.out.println("Enter the Miles per Gallon: ");
                mpg = in.nextDouble();
                System.out
                        .println("Enter the total Price per Gallon of Gasoline: ");
                pricePerGallon = in.nextDouble();
                System.out
                        .println("Enter the total Price per Gallon of Gasoline: ");
                pricePerGallon = in.nextDouble();
                totalCostGas = (distance / mpg) * pricePerGallon;
                System.out.printf("The trip is going to cost $%5.2f", totalCostGas);
    
            } else {
                System.out.println("Please resubmit entry");
            }
    
            System.out.println();
        }