gpt4 book ai didi

java - 打印出在字符串中找到字符的行号

转载 作者:行者123 更新时间:2023-12-02 09:17:54 26 4
gpt4 key购买 nike

我正在编写一个程序来扫描给定字符串中的数学运算符,当程序找到匹配项时,我想返回该运算符以及找到它的行,下面是我的尝试。

public class Testing {

public enum TokenType {

OP_MULTIPLY,
OP_DIVIDE,
OP_MOD,
OP_ADD,
OP_SUBTRACT,
OP_LESS,
OP_LESSEQUAL,
OP_GREATER,
OP_GREATEREQUAL,
OP_EQUAL,
OP_NOTEQUAL,
OP_NOT,
OP_ASSIGN,
OP_AND,
OP_OR,
OP_DOT

}

public static String inputString = "public class HelloWorld {\n"
+ " public static void-*//-- main(String[] args) {\n" + " // to print out+8+*+ hello world\n"
+ " System.out.println(\"Hello World!\");\n" + " }\n" + "}\n";

public static void lineNumber(String lineNumber) {
int count = 1;

String[] lines = inputString.split("\\r?\\n");
for (String line : lines) {
System.out.println("line " + count++);
}

int n = inputString.length();
for (int i = 0; i < n; i++) {
char ch = inputString.charAt(i);
getOP(ch);

}
}

public static TokenType getOP(char ch) {

switch (ch) {
case '+':
System.out.println(TokenType.OP_ADD + ", " + ch);
return TokenType.OP_ADD;

case '-':
System.out.println(TokenType.OP_SUBTRACT + ", " + ch);
return TokenType.OP_SUBTRACT;

case '/':
System.out.println(TokenType.OP_DIVIDE + ", " + ch);
return TokenType.OP_DIVIDE;

case '*':
System.out.println(TokenType.OP_MULTIPLY + ", " + ch);
return TokenType.OP_MULTIPLY;
}

return null;

}

public static void main(String[] args) {
lineNumber(inputString);
}
}

这是我当前的输出:

line 1
line 2
line 3
line 4
line 5
line 6
OP_SUBTRACT, -
OP_MULTIPLY, *
OP_DIVIDE, /
OP_DIVIDE, /
OP_SUBTRACT, -
OP_SUBTRACT, -
OP_DIVIDE, /
OP_DIVIDE, /
OP_ADD, +
OP_ADD, +
OP_MULTIPLY, *
OP_ADD, +

我想要实现什么:

Line 1: OP_ADD, +
Line 3: OP_MULTIPLY, *
etc.

谢谢

最佳答案

您的代码可能如下所示:

public class Testing {

enum TokenType {

OP_MULTIPLY,
OP_DIVIDE,
OP_MOD,
OP_ADD,
OP_SUBTRACT,
OP_LESS,
OP_LESSEQUAL,
OP_GREATER,
OP_GREATEREQUAL,
OP_EQUAL,
OP_NOTEQUAL,
OP_NOT,
OP_ASSIGN,
OP_AND,
OP_OR,
OP_DOT

}

public static void main(String[] args) {
lineNumber();
}

public static final String inputString = "public class HelloWorld {\n" +
" public static void-*//-- main(String[] args) {\n" +
" // to print out+8+*+ hello world\n" +
" System.out.println(\"Hello World!\");\n" +
" }\n" +
"}\n";

private static void lineNumber() {
String[] lines = inputString.split("\\r?\\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
for (int j = 0; j < line.length(); j++) {
TokenType tokenType = getOP(line.charAt(j));
if (tokenType != null) {
System.out.printf("Line %d: %s, %s\n", i + 1, tokenType.name(), line.charAt(j));
}
}
}
}

private static TokenType getOP(char ch) {
switch (ch) {
case '+':
return TokenType.OP_ADD;
case '-':
return TokenType.OP_SUBTRACT;
case '/':
return TokenType.OP_DIVIDE;
case '*':
return TokenType.OP_MULTIPLY;
// more
}
return null;
}
}

嵌套循环允许保留当前行索引。此外,您的 getOP 方法应该只负责根据字符值确定数学运算。它不应该打印任何内容,这是 lineNumber 的责任。

输出:

Line 2: OP_SUBTRACT, -
Line 2: OP_MULTIPLY, *
Line 2: OP_DIVIDE, /
Line 2: OP_DIVIDE, /
Line 2: OP_SUBTRACT, -
Line 2: OP_SUBTRACT, -
Line 3: OP_DIVIDE, /
Line 3: OP_DIVIDE, /
Line 3: OP_ADD, +
Line 3: OP_ADD, +
Line 3: OP_MULTIPLY, *
Line 3: OP_ADD, +

关于java - 打印出在字符串中找到字符的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com