gpt4 book ai didi

java - String.replace 结果被忽略?

转载 作者:行者123 更新时间:2023-12-01 01:24:43 24 4
gpt4 key购买 nike

所以我正在使用 IntelliJ 并突出显示替换流行语。大多数提示都在我的头上,所以我忽略了它们,但是我得到的结果是 string.replace 的结果被忽略了。为什么?

我需要类似 (string = string.replace(string.charAt(i));) 的东西吗?

import java.util.Scanner;

public class PhoneNumberDecipher {
public static String phoneNumber;
public static String Decipher(String string) {
string = phoneNumber;
for(int i =0; i<=phoneNumber.length(); i++) {
if(string.equalsIgnoreCase("A")
||string.equalsIgnoreCase("B")
||string.equalsIgnoreCase("C")) {
string.replace(string.charAt(i),'2')
}
else if(string.equalsIgnoreCase("D")
||string.equalsIgnoreCase("E")
||string.equalsIgnoreCase("F")) {
string.replace(string.charAt(i),'3');
}
else if(string.equalsIgnoreCase("G")
||string.equalsIgnoreCase("H")
||string.equalsIgnoreCase("I")) {
string.replace(string.charAt(i),'4');
}
else if(string.equalsIgnoreCase("J")
||string.equalsIgnoreCase("K")
||string.equalsIgnoreCase("L")) {
string.replace(string.charAt(i),'5');
}
else if(string.equalsIgnoreCase("M")
||string.equalsIgnoreCase("N")
||string.equalsIgnoreCase("O")) {
string.replace(string.charAt(i),'6');
}
else if(string.equalsIgnoreCase("P")
||string.equalsIgnoreCase("Q")
||string.equalsIgnoreCase("R")
|| string.equalsIgnoreCase("S")) {
string.replace(string.charAt(i),'7');
}
else if(string.equalsIgnoreCase("T")
||string.equalsIgnoreCase("U")
||string.equalsIgnoreCase("V")) {
string.replace(string.charAt(i),'8');
}
else if(string.equalsIgnoreCase("W")
||string.equalsIgnoreCase("X")
||string.equalsIgnoreCase("Y")
||string.equalsIgnoreCase("Z")) {
string.replace(string.charAt(i),'9');
}
}

return string;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Please Enter a Phone Number you Wish to Decipher...");
phoneNumber = input.nextLine();

System.out.print(Decipher(phoneNumber));
}
}

最佳答案

String对象是 immutable .
来自 docs :

public String replace(char oldChar,char newChar)

Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.


希望这可以帮助。

关于java - String.replace 结果被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024703/

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