我不确定我在这个特定代码中哪里出了问题。有人能给我一些指导吗?
这是我的问题,以及我试图得到的结果。
修改songVerse来玩“名字游戏”(OxfordDictionaries.com),将"( Name )“替换为userName,但不带第一个字母。
例如:如果userName = "Katie“和songVerse = "Banana-fana fo-f(Name)!",程序会打印: Banana-fana fo-fo-fo!例如:如果userName = "Katie“和songVerse = "Fee fi mo-m(Name)",程序输出: Fee fi mo-matie注意:您可以假设songVerse总是包含子字符串"(Name)”。
我最后一次尝试的代码,time...and,不管我放入什么,我总是得到相同的结果。我尝试了"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);
}
}1测试通过
所有测试均已通过
Run
Testing Katie and Banana-fana fo-f(Name)!
Output differs. See highlights below.
Your output
Banana-fana fo-f(Name)!tie
Expected output
Banana-fana fo-fatie!
Testing Walter and Banana-fana fo-f(Name)!
Output differs. See highlights below.
Your output
Banana-fana fo-f(Name)!lter
Expected output
Banana-fana fo-falter!
Testing Katie and Fee fi mo-m(Name)
Output differs. See highlights below.
Your output
Fee fi mo-m(Name)tie
Expected output
Fee fi mo-matiehttps://stackoverflow.com/questions/52691492
复制相似问题