Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取令牌“上的语法错误”;{";,此标记后应为SwitchLabels。正在尝试在switch语句中使用用户输入_Java_Switch Statement - Fatal编程技术网

Java 获取令牌“上的语法错误”;{";,此标记后应为SwitchLabels。正在尝试在switch语句中使用用户输入

Java 获取令牌“上的语法错误”;{";,此标记后应为SwitchLabels。正在尝试在switch语句中使用用户输入,java,switch-statement,Java,Switch Statement,我对Java非常陌生,正在尝试将用户输入实现到switch语句中。我得到了“token”{上的语法错误”,SwitchLabels应该在这个token“我的switch语句开头的错误”之后。任何帮助都将非常棒。谢谢。(顺便说一句,stackoverflow上的第一篇文章很抱歉,如果我弄错了格式) 该特定问题与这一行代码有关: System.out.println("Do You Want to Enter Fahrenheit or Celsius: "); 它应该放在开关之前。但是,我看到在

我对Java非常陌生,正在尝试将用户输入实现到switch语句中。我得到了“token”{上的语法错误”,SwitchLabels应该在这个token“我的switch语句开头的错误”之后。任何帮助都将非常棒。谢谢。(顺便说一句,stackoverflow上的第一篇文章很抱歉,如果我弄错了格式)


该特定问题与这一行代码有关:

System.out.println("Do You Want to Enter Fahrenheit or Celsius: ");
它应该放在开关之前。但是,我看到在这之后会出现另一个问题

您的case语句将比较float和int。您可以将其更改为case1:和case2:(或case0和case32)以消除此问题。不过,更好的方法是使用字符串或用户菜单

下面的固定代码

import java.util.*;

class FahrenheitToCelsiusBothInput {

public static void main(String[] args) {
float Fahrenheit = 32;
float Celsius = 0;

Scanner option = new Scanner(System.in);

System.out.println("Do You Want to Enter Fahrenheit or Celsius: ");
int userInput = option.nextInt();

switch(userInput) {                      //Getting "Syntax error on token "{", SwitchLabels expected after this token"
case 0:
        System.out.println("Enter temperature in Fahrenheit: ");
        Fahrenheit = option.nextInt();
        Fahrenheit = (float) ((Fahrenheit - 32) / 1.8000);
        System.out.println("Temperature in Celsius = " + Fahrenheit);  System.out.println("Thank You! ");
   break; 
case 1:
        System.out.println("Enter temperature in Celsius: ");
        Celsius = option.nextFloat();
        Celsius = (float) ((Celsius * 1.8000) + 32);
        System.out.println("Temperature in Fahrenheit = " + Celsius);  System.out.println("Thank You! ");
    break;  
default:
        System.out.println("Something went wrong! ");
    break;
        }
    }
}

编辑:调整代码使其实际工作。

嘿,如果您能帮助我@rctillotson,我还有另一个问题。我在上面的代码中遇到了这些错误。java.util.InputMismatchException位于java.util.Scanner.throwFor(未知源代码)位于java.util.Scanner.next的线程“main”中出现异常(未知源代码)在java.util.Scanner.nextInt(未知源代码)在java.util.Scanner.nextInt(未知源代码)在java.util.Scanner.nextInt(未知源代码)这是我的错误。你需要在int userInput=option.nextInt()之前输入“System.out.println(“你想输入华氏温度还是摄氏温度?”);”,我将更改上面的代码以反映这一点。
import java.util.*;

class FahrenheitToCelsiusBothInput {

public static void main(String[] args) {
float Fahrenheit = 32;
float Celsius = 0;

Scanner option = new Scanner(System.in);

System.out.println("Do You Want to Enter Fahrenheit or Celsius: ");
int userInput = option.nextInt();

switch(userInput) {                      //Getting "Syntax error on token "{", SwitchLabels expected after this token"
case 0:
        System.out.println("Enter temperature in Fahrenheit: ");
        Fahrenheit = option.nextInt();
        Fahrenheit = (float) ((Fahrenheit - 32) / 1.8000);
        System.out.println("Temperature in Celsius = " + Fahrenheit);  System.out.println("Thank You! ");
   break; 
case 1:
        System.out.println("Enter temperature in Celsius: ");
        Celsius = option.nextFloat();
        Celsius = (float) ((Celsius * 1.8000) + 32);
        System.out.println("Temperature in Fahrenheit = " + Celsius);  System.out.println("Thank You! ");
    break;  
default:
        System.out.println("Something went wrong! ");
    break;
        }
    }
}