gpt4 book ai didi

java分割字符串函数

转载 作者:行者123 更新时间:2023-12-01 17:31:22 25 4
gpt4 key购买 nike

我正在尝试构建一个程序,在使用 Split 函数后读取某个字符串

import java.util.Scanner;

public class Lexa2 {

public void doit() {
String str = "( 5 + 4 ) * 2";
String [] temp = null;
temp = str.split(" ");
dump(temp);
}
public void dump(String []s) {
for (int i = 0 ; i < s.length ; i++) {
if (s[i] == "(") {
System.out.println("This is the left paren");
} else if (s[i] == ")"){
System.out.println("This is the right paren");
}else if (s[i] == "+"){
System.out.println("This is the add");
}else if (s[i] == "-"){
System.out.println("This is the sub");
}else if (s[i] == "*"){
System.out.println("This is the mult");
}else if (s[i] == "/"){
System.out.println("This is the div");
}else
System.out.println("This is a number");
}
}

public static void main(String args[]) throws Exception{
Lexa2 ss = new Lexa2();
ss.doit();
}
}

输出应该是这样的:

This is the left paren
this is a number
this is the add
this is the right paren
this is a number

最佳答案

你已经很接近了,只需将 (s[i] == "?") 替换为 (s[i].equals("?"))

关于java分割字符串函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549419/

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