【问题标题】:2 or more digit numbers with delimiters in javajava中带有分隔符的2位或更多位数字
【发布时间】:2012-12-14 07:23:12
【问题描述】:

我有这个基本程序,在输入个位数时效果很好。但是当计算一个包含多个数字的表达式时,比如 1337 - 456 + 32,程序不会继续运行......它就像我什么都没做一样。 它不会冻结或输出错误消息,它只是停止。

代码如下:

import java.io.*;
import java.util.*;
public class Tester {
    public static void main(String args[]) {
        Scanner kb = new Scanner(System.in);
        System.out.print("Enter number: ");
        String s = kb.nextLine();
        Scanner sc = new Scanner(s);
        //Set delimiters to a plus sign surrounded by any amount of white space...or...
        // a minus sign surrounded by any amount of white space.
        sc.useDelimiter("\\s*");
        int sum = 0;
        int temp = 0;
        int intbefore = 0;
        
        if (sc.hasNext("\\-")) {
            sc.next();
            if (sc.hasNextInt()) {
                intbefore = sc.nextInt();
                int temper = intbefore * 2;
                intbefore = intbefore - temper; 
            }
        }
        if (sc.hasNextInt()) {
            intbefore = sc.nextInt(); //now its at the sign (intbefore = 5)
        }
        sum = intbefore;
        
        while (sc.hasNext()) {
            if(sc.hasNext("\\+")) { //does it have a plus sign?
                sc.next(); //if yes, move on (now at the number)
                System.out.println("got to the next();");
                
                if(sc.hasNextInt()) { //if there's a number
                    temp = sc.nextInt();
                    sum = sum + temp; //add it by the sum (0) and the sum of (5) and (4)
                    System.out.println("added " + sum);
                }
            }
            if(sc.hasNext("\\-")) {
                sc.next();
                System.out.println("got to the next();");
                
                if (sc.hasNextInt()) {
                    temp = sc.nextInt();
                    sum = sum - temp; //intbefore - temp == 11
                    System.out.println("intbefore: " + intbefore + "  temp: " + temp);
                    System.out.println("subtracted " + sum); // subtracted by 11
                }
            }
        }
        System.out.println("Sum is: " + sum);
    }
}

帮助了解为什么会发生这种情况以及如何解决它? (如果有帮助,我正在使用 netbeans)

另外,我假设输入在每个数字之间都有一个空格,例如:123 + -23 - 5

【问题讨论】:

  • 在调试器中单步执行代码会显示什么?您是在某处陷入无限循环(尝试在while 之后立即添加println 调用),还是函数调用未返回?
  • 在您的while(sc.hasNext()) 块中,如果字符串的其余部分没有空格,sc.Next(); 语句将返回所有剩余的字符串。
  • 你们是对的,它陷入了无限循环。

标签: java numbers delimiter arithmetic-expressions


【解决方案1】:

我试图执行你的代码,这就是我发现的。

假设您的输入是 12+1。

不是 sc.hasNextInt() 会给你1 你分配给intbefore

接下来,代码中的 while 循环将进入无限循环,因为 sc.hasNext() 将始终为真(在这种情况下返回 2),因此它与 while 循环内的两个 if 条件不匹配使您的代码在无限循环中运行。现在继续努力。一切顺利。

【讨论】:

  • 你是对的,但我忘了说假设输入总是有一个空格 Ex: 12 + 1。我会把它添加到问题中
【解决方案2】:

首先尝试将测光仪更改为"\\s+",或者直接使用默认值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多