【问题标题】:Problems with Output in JavaJava 中的输出问题
【发布时间】:2019-02-26 20:40:21
【问题描述】:

我不知道为什么我的输出不正确。例如,如果输入是“跑步很有趣”,那么输出应该是“跑步很有趣”。但是,我得到的输出是“Iunning”。

  import java.util.Scanner;
  public class Problem1 {
  public static void main( String [] args ) {

 Scanner sc = new Scanner(System.in);

 System.out.println("Enter text: ");
 String sentence = sc.nextLine();

  int space = sentence.indexOf(" ");
 String firstWord = sentence.substring(0, space + 1);
 String removedWord = sentence.replaceFirst(firstWord, "");

String newSentence = removedWord.substring(0,1).toUpperCase() + 
firstWord.substring(1).toLowerCase();

System.out.println(""); 
System.out.println( newSentence );

  }
}

【问题讨论】:

  • 您的代码first在什么时候无法按您预期的方式工作?
  • @GBlodgett 分析得很好;为了让你朝着正确的方向前进,也许看看String.split 并在“”上拆分。

标签: java string uppercase lowercase


【解决方案1】:

这可以正常工作:

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

    System.out.println("Enter text: ");
    String sentence = sc.nextLine();

    int space1 = sentence.indexOf(' ');
    int space2 = sentence.indexOf(' ', space1 + 1);

    if (space1 != -1 &&  space2 != -1) {
        String firstWord = sentence.substring(0, space1 + 1);
        String secondWord = sentence.substring(space1 + 1, space2 + 1);

        StringBuilder newSentence = new StringBuilder(sentence);
        newSentence.replace(0, secondWord.length(), secondWord);
        newSentence.replace(secondWord.length(), secondWord.length()+ firstWord.length(), firstWord);
        newSentence.setCharAt(0, Character.toUpperCase(newSentence.charAt(0)));
        newSentence.setCharAt(secondWord.length(), Character.toLowerCase(newSentence.charAt(secondWord.length())));

        System.out.println(newSentence);
    }
}

【讨论】:

    【解决方案2】:

    removedWord.substring(0,1).toUpperCase() 这行添加了句子中第二个单词的大写首字母。 (I)

    firstWord.substring(1).toLowerCase(); 将第一个单词的每个字母添加到句子的末尾。 (unning)

    因此这将创建Iunning 的输出。您需要将removedWord 的其余部分添加到String,以及一个空格和firstWord 的第一个字母,作为removedWord 中的空格处的小写字母。您可以通过使用indexOf 查找空间,然后使用substring() 在空间索引之后添加firstWord.toLowerCase() 来执行更多操作:

    removedWord = removedWord.substring(0, removedWord.indexOf(" ")) + " " + 
                  firstWord.toLowerCase() + 
                  removedWord.substring(removedWord.indexOf(" ") + 1, 
                  removedWord.length());
    String newSentence = removedWord.substring(0,1).toUpperCase() + 
                     removedWord.substring(1, removedWord.length());
    

    输出:

    Is running fun
    

    【讨论】:

    • 根据 OP,所需的输出是“Is running fun”
    【解决方案3】:

    问题出在String newSentence。您没有正确组合 firstWordremovedWord

    你的情况应该这样:

    String newSentence = removedWord.substring(0, 1).toUpperCase()  // I
                    + removedWord.substring(1,2) + " "      // s
                    + firstWord.toLowerCase().trim() + " "  // running
                    + removedWord.substring(2).trim();      // fun
    

    编辑(添加新的解决方案。学分@andy):

    String[] words = sentence.split(" ");
    
    words[1] = words[1].substring(0, 1).toUpperCase() + words[1].substring(1);
    
    String newSentence = words[1] + " " 
                    + words[0].toLowerCase() + " " 
                    + words[2].toLowerCase();
    

    【讨论】:

      【解决方案4】:

      你的问题是

      firstWord.substring(1).toLowerCase()
      

      没有像您预期的那样工作。

      鉴于firstWord“Running“,如您的示例所示,那么

      ”Running“.substring(1)
      

      返回“unning”

      ”unning“.toLowerCase()
      

      显然返回“unning”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-01
        • 2013-12-15
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 2014-12-04
        相关资源
        最近更新 更多