Java 使用switchcase检查userinput是字符串还是int或float

Java 使用switchcase检查userinput是字符串还是int或float,java,switch-statement,Java,Switch Statement,基本上,如果检查用户输入是整数、浮点还是字符串,我想构建一个switchcase语句。我想使用hasCheckInt()、hasCheckDouble()等方法来检查相应的数据类型 有些代码丢失了是的,因为我不知道该输入什么。 原谅java noob import java.util.Scanner; public class helloWorld { public static void main(String[] args) { Scanner userInput = new S

基本上,如果检查用户输入是整数、浮点还是字符串,我想构建一个switchcase语句。我想使用hasCheckInt()、hasCheckDouble()等方法来检查相应的数据类型

有些代码丢失了是的,因为我不知道该输入什么。 原谅java noob

import java.util.Scanner;
public class helloWorld {
public static void main(String[] args) {
      Scanner userInput = new Scanner(System.in);
      switch()) {
      case 1: 

      if(userInput.hasNextInt()) {
      System.out.println("Int");
      }
      break;



     case 2: 
     if(userInput.hasNextLine()) {
     System.out.println("String");
     }
     break;
     }
     }
     }
基本上,如果检查用户输入是整数、浮点还是字符串,我想构建一个switchcase语句。我想使用hasCheckInt()、hasCheckDouble()等方法来检查相应的数据类型

不能,开关的
大小写必须是常量,而不是表达式(包括函数调用)的结果

相反,您需要
if/else if/else

if (hasCheckInt(argumentHere)) {
    // `hasCheckInt` returned `true`
    // ...
}
else if (hasCheckDouble(argumentHere)) {
    // `hasCheckDouble` returned `true`
    // ...
}
else {
    // None of the above matched
}

我使用if语句来做这件事。只是我想用开关来做。不可能吗?正确,不可能。在Java中,
开关的
大小写必须是常量:
大小写42:
它不能是表达式:
大小写hasCheckInt(args):
。(大多数(但不是所有)具有
开关的语言都是如此。JavaScript是我所知道的唯一例外,但我相信还有其他例外。)