我不确定这个特定代码哪里出了问题。有人可以给我一些指导吗?
这是我的问题以及我试图作为结果的结果。
修改 songVerse 以播放“The Name Game”(OxfordDictionaries.com),将“(Name)”替换为 userName 但没有首字母。
例如:如果 userName = "Katie" 和 songVerse = "Banana-fana fo-f(Name)!",程序打印:Banana-fana fo-fatie!例如:如果 userName = "Katie" 和 songVerse = "Fee fi mo-m(Name)",程序将打印: Fee fi mo-matie 注意:您可能认为 songVerse 将始终包含子字符串“(Name)”。
我上次尝试过的代码......无论我输入什么,我都会得到相同的结果。我尝试了“userName.substring()”的不同场景,但结果仍然相同。
import java.util.Scanner;
public class NameSong {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String userName;
String songVerse;
userName = scnr.nextLine();
userName = userName.substring(1); // Remove first character
songVerse = scnr.nextLine();
// Modify songVerse to replace (Name) with userName without first character
songVerse = songVerse + userName.substring(1 , userName.length()); // this is where my problem is.
System.out.println(songVerse);
}
}
哆啦的时光机
哔哔one
胡子哥哥
相关分类