有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检测运算符的空白

我的代码有些问题

我想检测像" + "" +""+ ""+"这样的运算符的空白

我希望我的输出是

  • 运算符的空格是“A”

如何修改代码

我的密码在这里

Scanner input = new Scanner (new File(PATH to file));
int plus1;
int plus2;
int plus3;
int plus4;
String sPlus = "";
while (in.hasNext()) {
    String line = in.nextLine();
    in.hasNextLine();
    LOC++;
    if (line.length() > 0) {

        plus1 = -1;
        plus2 = -1;
        plus3 = -1;
        plus4 = -1;
        while (true) {
            plus1 = line.indexOf(" + ", plus1 + 1);
            plus2 = line.indexOf(" +", plus2 + 1);
            plus3 = line.indexOf("+ ", plus3 + 1);
            plus4 = line.indexOf("+", plus4 + 1);

            if (plus1 > 0) {
                sPlus = "A";
            }
            if (plus2 > 0) {
                sPlus = "B";
            }
            if (plus3 > 0) {
                sPlus = "C";
            }
            if(plus4 > 0){
                sPlus = "D";
            }

            if ((plus1 < 0) || (plus2 < 0) || (plus3 < 0) || (plus4 < 0)) break;
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    如果你能帮忙的话

    if (line.indexOf(" + ") != -1) sPlus = "A";
    else if (line.indexOf(" +") != -1) sPlus = "B";
    else if (line.indexOf("+ ") != -1) sPlus = "C";
    else if (line.indexOf("+") != -1) sPlus = "D";
    else break;