首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我需要一些关于JAVA中子字符串函数的指导

我需要一些关于JAVA中子字符串函数的指导
EN

Stack Overflow用户
提问于 2018-10-08 02:17:56
回答 5查看 8.4K关注 0票数 0

我不确定我在这个特定代码中哪里出了问题。有人能给我一些指导吗?

这是我的问题,以及我试图得到的结果。

修改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()“的不同场景,但仍然有相同的结果。

代码语言:javascript
运行
复制
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测试通过

所有测试均已通过

代码语言:javascript
运行
复制
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-matie
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52691492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档