代码之家  ›  专栏  ›  技术社区  ›  Gege

Java Scanner nextDouble命令是否使用switch case跳过值?

  •  1
  • Gege  · 技术社区  · 7 年前

    我正在扫描一个带有开关盒的参数文件 Stack 它用一个 .nextDouble 命令

    这是我的代码片段:

    while (stackScanner.hasNextLine()) {        
    
        switch(stackScanner.next()) { 
    
        case"+": {
            operator= new operationNode("+");
            stack.push(operator);}
    
        case"-":{
            operator= new operationNode("-");
            stack.push(operator);}
    
        case"*":{
            operator= new operationNode("*");
            stack.push(operator);}
    
        case"/":{
            operator= new operationNode("/");
            stack.push(operator);}
    
        case"^":{
            operator= new operationNode("^");
            stack.push(operator);}
    
        while(stackScanner.hasNextDouble()) {
            stack.push(new numberNode(stackScanner.nextDouble()));
        }
    }
    

    问题在这里的最后一行,其中参数文件包含以下内容: ^ 2 - 3 / 2 6 * 8 + 2.5 3

    然而,扫描仪仅收集: ^ 2 - 3 / 6 * 8 + 3 .

    所以它跳过了这里成对出现的第一个数字(2和2.5)。

    问题是,当我添加 stackScanner.next(); 在while循环结束时,它保存的唯一数字是值2和2.5?

    2 回复  |  直到 7 年前
        1
  •  1
  •   DaveyDaveDave max    7 年前

    复制代码并稍微修改以使用 Stack<String> 而不是实施 operationNode numberNode 在课堂上,我发现以下内容(我认为)符合你的预期:

    public static void main(String... args) {
        Scanner stackScanner = new Scanner("^ 2 - 3 / 2 6 * 8 + 2.5 3");
    
        Stack<String> stack = new Stack<>();
    
        while (stackScanner.hasNextLine()) {
    
            switch (stackScanner.next()) {
                case "+": {
                    stack.push("+");
                    break;
                }
    
                case "-": {
                    stack.push("-");
                    break;
                }
    
                case "*": {
                    stack.push("*");
                    break;
                }
    
                case "/": {
                    stack.push("/");
                    break;
                }
    
                case "^": {
                    stack.push("^");
                    break;
                }
            }
    
            while (stackScanner.hasNextDouble()) {
                stack.push(Double.toString(stackScanner.nextDouble()));
            }
        }
    
        System.out.println(stack);
    }
    

    也就是说,我添加了 break; 语句,您似乎不需要这些语句(可能是某种JVM差异?)并移动了 while 回路外部 switch .

        2
  •  0
  •   Darshan Mehta    7 年前

    你需要包装 switch 进入 while 并移动处理 double default 块,例如:

    while (stackScanner.hasNextLine()) {
        String nextToken = stackScanner.next();
        switch(nextToken) { 
    
        case"+": {
            System.out.println("+");
            break;
            }
    
        case"-":{
            System.out.println("-");
            break;
        }
    
        case"*":{
            System.out.println("*");
            break;
        }
    
        case"/":{
            System.out.println("/");
            break;
        }
    
        case"^":{
            System.out.println("^");
            break;
        }
    
        default:
            if(isDouble(nextToken)){
                //Do something
            }
            break;
        }
    }
    

    您还需要编写一个方法来检查 . 它看起来像这样:

    private boolean isDouble(String number){
        try{
            Double.parseDouble(number);
            return true;
        }Catch(Exception e){
            return false;
        }
    }