如何修复Java中非法启动表达式错误?

时间:2018-04-01 09:11:44

标签: java

我收到错误:

  

非法开始表达,否则没有if和非法开始声明。

以下是代码:

import java.util.Scanner;

public class Temperature_Converter {
    public static void main(String[] args) {
        System.out.println("Welcome!This program takes temperature as an input and an input" +
                " indicating wether we're converting from Celsius to Fahrenheit or Fahrenheit to Celsius");

        Scanner scan = new Scanner (System.in);

                System.out.println("Please enter \"C\" if we\'re converting from Fahrenheit");

        System.out.println("Please enter \"F\" if we're converting from Celsius");

        String temp_key = scan.nextLine();

        System.out.println("Please enter the numerical value of the temperature");

        double temp_value = scan.nextDouble();

        if (temp_key.equals("F"));
        {
            {
                double result = temp_value - 32 / 1.8;

                System.out.println("When converting " + temp_value + "Fahrenheit to Celsius,\n" +

                        " the result is " + result);
            }
             else if (temp_key.equals("C")
        }

             double result = temp_value * 1.8 + 32;

             System.out.println("When converting " + temp_value + "From Celsius to Fahrenheit,\n + ");
                                "the result is;"+ result;);

    }
    static {;
    System.out.println("Next time make sure you enter either F or C");
    }
}

1 个答案:

答案 0 :(得分:1)

丢失分号和双嵌套花括号块

此:

ParserXXX

应该是:

@FunctionalInterface // >Java 8
interface ParserXXX {
    Runnable get(Data data);
}

class Parser implements Runnable {
    ...
}

class Foo {
    private ParserXXX;

    public Foo() {
        xxx = new ParserXXX() { // anonymous class
            @Override
            public Runnable get(Data data) {
                return new Parser(data);
            }
        }
        xxx = Parser::new; // using method reference(>Java 8)
        xxx = data -> new Parser(data); // using lambda function(>Java 8)
    }

    private void parse(Data data) {
        execute(xxx.get(data));
    }
}

虽然我在这里......

本声明:

    if (temp_key.equals("F"));
    {
        {
            double result = temp_value - 32 / 1.8;

不会像你认为的那样从F转换为C.首先评估32 / 1.8。 (就像你在9年级代数中学到的那样)。你想说:

    if (temp_key.equals("F"))
    {
        double result = temp_value - 32 / 1.8;