FOR循环中的StringIndexOutOfBoundsException import java.util.Scanner; 公开课考试{ 公共静态void main(字符串[]args){ String word=“ErFLwWSa$F?ytWtmVRLWmDj#4STs!@VRa!12OW”; 字符串word2=“绿色”; 字符串结果=”; 对于(inti=0;i

FOR循环中的StringIndexOutOfBoundsException import java.util.Scanner; 公开课考试{ 公共静态void main(字符串[]args){ String word=“ErFLwWSa$F?ytWtmVRLWmDj#4STs!@VRa!12OW”; 字符串word2=“绿色”; 字符串结果=”; 对于(inti=0;i,java,if-statement,for-loop,replace,character,Java,If Statement,For Loop,Replace,Character,,假设您有一个长度为10的字符串 for(int i=0;iI将其重新表述为“位置10中的元素”。事实上,更好:)因此基本上尝试读取不存在的字符11?现在如何进行反击?如果我处于循环的最后一个周期,则添加if语句?如果数组的长度为10,则它的索引范围为0到9(因此10超出范围).是的,你提出的解决方案很好。@hayonj:我不需要检查每次迭代,而是调整循环条件…I

,假设您有一个长度为10的字符串


for(int i=0;iI将其重新表述为“位置10中的元素”。事实上,更好:)因此基本上尝试读取不存在的字符11?现在如何进行反击?如果我处于循环的最后一个周期,则添加if语句?如果数组的长度为10,则它的索引范围为0到9(因此10超出范围).是的,你提出的解决方案很好。@hayonj:我不需要检查每次迭代,而是调整循环条件…
I
import java.util.Scanner;

public class Test {

public static void main (String [] args){

    String word = "ErFLwWSa$ F?ytWtmVRLWmDj #4STs!@  VRa!12OW";
    String word2 = "GREEN";
    String result ="";
    for(int i = 0; i<=word2.length()-1; ++i){
        result += word.replace(word.charAt(i),word2.charAt(i+1));
    }
}
}