如何更改数组中的特定值,然后在Java中将数组转换为字符串?
我已经在下面解释了我想要它做什么。我很难找到答案,我真的很感谢你的帮助如何更改数组中的特定值,然后在Java中将数组转换为字符串?,java,arrays,string,Java,Arrays,String,我已经在下面解释了我想要它做什么。我很难找到答案,我真的很感谢你的帮助 import java.util.*; import java.lang.*; class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); System.out.println("Input a palindrome"); String str =
import java.util.*;
import java.lang.*;
class Main {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Input a palindrome");
String str = s.nextLine();
String strL = str.toLowerCase();
String[] strSplit = strL.split("");
// After this I need to alter the array and make all 4's become a's, all 3's become e's, and all 0's become o's. then I need to make that a string named "strSplitRev". I have no idea how to do that.
}
}
您需要做的是使用一个类似下面这样的
for循环来循环数组strSplit
for(int i=0;i
在for循环中,您将检查strSplit[i]
是否等于4,3等,并将其替换为a、e等
if (strSplit[i].equals("4"){
strSplit[i] = "a"
}
这或您可以使用switch/case语句。在同一循环中,您可以将所有单个字符附加到
StringBuilder
对象中,希望这会有所帮助。请随意询问是否有不清楚的地方
String strSplitRev="";
for(int i=0;i<strSplit.length;i++){
System.out.println(strSplit[i]);
if(Integer.parseInt(strSplit[i])==0){
strSplitRev+="o";
}
else if(Integer.parseInt(strSplit[i])== 3){
strSplitRev+="e";
}
else if(Integer.parseInt(strSplit[i])==4){
strSplitRev+="a";
}
else strSplitRev+=strSplit[i];
}
字符串strSplitRev=”“;
对于(int i=0;我能请你在问题本身加上你的“解释”吗?评论已经超出了右边的空白,很难阅读。