Java Scanner nextDouble命令是否使用开关大小写跳过值?

Java Scanner nextDouble命令是否使用开关大小写跳过值?,java,switch-statement,arguments,java.util.scanner,next,Java,Switch Statement,Arguments,Java.util.scanner,Next,我正在将带有开关大小写的参数文件扫描到堆栈中,它正在使用.nextDouble命令跳过值 这是我的代码片段: while (stackScanner.hasNextLine()) { switch(stackScanner.next()) { case"+": { operator= new operationNode("+"); stack.push(operator);} case"-":{ op

我正在将带有开关大小写的参数文件扫描到
堆栈中,它正在使用
.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?

复制代码并稍微修改以使用
堆栈
,而不是实现
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);
}
publicstaticvoidmain(字符串…参数){
扫描仪stackScanner=新扫描仪(^2-3/2 6*8+2.5 3”);
堆栈=新堆栈();
while(stackScanner.hasNextLine()){
开关(stackScanner.next()){
格“+”:{
stack.push(“+”);
打破
}
案例“-”:{
堆栈。推送(“-”);
打破
}
案例“*”:{
stack.push(“*”);
打破
}
案例“/:{
stack.push(“/”);
打破
}
案例“^”:{
堆栈。推送(“^”);
打破
}
}
while(stackScanner.hasNextDouble()){
stack.push(Double.toString(stackScanner.nextDouble());
}
}
System.out.println(堆栈);
}

也就是说,我添加了
中断语句,您似乎不需要这些语句(可能是某种JVM差异?),并将
while
循环移到
开关

之外。您需要将
开关
包装到
while
中,并将
double
的处理移到
默认
块中,例如:

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;
    }
}
您还需要编写一个方法来检查
double
。它看起来像这样:

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

您是否注意到您的案例中没有中断,并且您的while循环在switch语句中?@MauricePerry我将其作为默认值:但它没有读取某些值。此外,中断似乎不会影响我的结果(?)你确定你发布了真实的代码吗?当我复制并粘贴它时,我没有看到你所说的结果。特别是,我的堆栈看起来像:
[^,2.0,-,*,/,^,3.0,/,^,2.0,6.0,*,/,^,8.0,+,-,*,/,^,2.5,3.0]
,这与@MauricePerry的观察一致,即您缺少
break
语句。@DaveyDaveDave哦,我完全意识到我做错了什么——没有正确地阅读堆栈。。谢谢你的帮助对不起,我认为这实际上是不对的。每行只处理一个令牌。如果输入是单行-“^2-3/2 6*8+2.5 3”,它将处理“^”,然后停止。