简体   繁体   English

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

[英]I need some guidance on the substring function in JAVA

I'm not sure where I am going wrong with this particular code.我不确定这个特定代码哪里出了问题。 Could someone please lend me some guidance to this?有人可以给我一些指导吗?

Here is my question as well as what I have attempted to have as an outcome.这是我的问题以及我试图作为结果的结果。

Modify songVerse to play "The Name Game" (OxfordDictionaries.com), by replacing "(Name)" with userName but without the first letter.修改 songVerse 以播放“The Name Game”(OxfordDictionaries.com),将“(Name)”替换为 userName 但没有首字母。

Ex: If userName = "Katie" and songVerse = "Banana-fana fo-f(Name)!", the program prints: Banana-fana fo-fatie!例如:如果 userName = "Katie" 和 songVerse = "Banana-fana fo-f(Name)!",程序打印:Banana-fana fo-fatie! Ex: If userName = "Katie" and songVerse = "Fee fi mo-m(Name)", the program prints: Fee fi mo-matie Note: You may assume songVerse will always contain the substring "(Name)".例如:如果 userName = "Katie" 和 songVerse = "Fee fi mo-m(Name)",程序将打印: Fee fi mo-matie 注意:您可能认为 songVerse 将始终包含子字符串“(Name)”。

Code that I tried this last time...and no matter what I put in I keep getting the same results.我上次尝试过的代码......无论我输入什么,我都会得到相同的结果。 I've tried different scenarios of the "userName.substring()" and still have the same outcome.我尝试了“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 test passed 1 次测试通过

All tests passed所有测试通过

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

Here you go.干得好。

  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.replace("(Name)", userName.substring(0));

  System.out.println(songVerse);

} } } }

here you removed first character already from userName, so at the second last line you again don't need to remove it.在这里,您已经从 userName 中删除了第一个字符,因此在倒数第二行,您再次不需要删除它。 and for the song Verse, you need to remove "(NAME)" from it, so here you can use对于歌曲 Verse,您需要从中删除“(NAME)”,因此您可以在这里使用

songVerse = songVerse.replace("(NAME)","");
songVerse = songVerse+userName;

The method substring(int begin, int end) let hoose/create a substring from the initial String indicating the numbers of chars from which the substring should begin and end or begin only.方法 substring(int begin, int end) 允许从初始 String 提升/创建一个子字符串,指示子字符串应该开始和结束或仅开始的字符数。 There are no other variants to edit a substring, while it will not become a part of a freshly made string (“String songVerse” in your case).没有其他变体可以编辑子字符串,而它不会成为新制作的字符串的一部分(在您的情况下为“String songVerse”)。 The object.replace() method should change the indicated “Text” (in your case it's a “(Name)”) onto anything that you'd like to be inserted instead of it independently on the quantity or type of the chars before or after the “Text”. object.replace() 方法应该将指示的“文本”(在您的情况下是“(名称)”)更改为您想要插入的任何内容,而不是独立于之前或之前的字符的数量或类型在“文本”之后。 The variant proposed by Nicholas K is correct and should work or you can try its shorter version, however the result will be the same: Nicholas K 提出的变体是正确的,应该可以工作,或者您可以尝试其较短的版本,但结果是相同的:

public class NameSong  {
    public static void main (String [] args) {
        Scanner scnr = new Scanner(System.in);
        String userName;
        String songVerse;

        userName = scnr.nextLine();
        songVerse = scnr.nextLine();
        songVerse = songVerse.replace("(Name)", userName.substring(1));

      System.out.println(songVerse);
   }

}

The problem with your code is that you are not attempting to solve the problem that you described in your question.您的代码的问题在于您没有尝试解决您在问题中描述的问题。

Try following these steps:尝试执行以下步骤:

  1. Devise a list of steps written in English to solve the problem;设计一份用英文写的解决问题的步骤清单; pay attention to details.注意细节。
  2. Run the list of steps in step 1 by hand.手动运行步骤 1 中的步骤列表。
  3. Convert the steps in step 1 to code.将步骤 1 中的步骤转换为代码。

Here are some hints:这里有一些提示:

  • You will be reading the lyrics one line at a time.您将一次阅读一行歌词。
  • Some lines have a replacement and others do not.有些线路有替代品,有些则没有。
  • You will receive the Name as input one time;您将收到 Name 作为输入一次; generate the name replacement value one time and use it each time you perform a replacement.生成名称替换值一次并在每次执行替换时使用它。

Your code is terrible.你的代码很糟糕。 Here is some more about "Pay attention to details"这里有更多关于“注意细节”的内容

  • You do not have a loop in your code;您的代码中没有循环; this will read one line of lyrics and perform one substitution.这将读取一行歌词并执行一次替换。 Count the number of lines in the lyrics.计算歌词中的行数。 If the number of lines is greater than one, then your technique is guaranteed to fail.如果行数大于 1,那么您的技术肯定会失败。

  • If you have a loop in your code but decided not to include it in your code, stop lying in your questions.如果您的代码中有一个循环但决定不将其包含在您的代码中,请停止在您的问题中撒谎。 We can not help you fix code that you pretend does not exist.我们无法帮助您修复您假装不存在的代码。

  • In a sane world, the name to use for the substitutions will appear exactly one time.在一个理智的世界中,用于替换的名称只会出现一次。 Read it one time.读一遍。

  • In order to replace (Name) in a string, you must first find (Name) in a string.为了替换字符串中的 (Name),您必须首先在字符串中找到 (Name)。

This is pretty easy这很容易

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.replace("(Name)", userName);
  /* Your solution goes here  */

  System.out.println(songVerse);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM