【问题标题】:Write a method that returns the even indexes of a string编写一个返回字符串偶数索引的方法
【发布时间】:2014-10-16 05:03:45
【问题描述】:

所以我应该编写一个只打印出字符串的偶数索引的方法。因此,例如,如果我在控制台/扫描仪中输入 “Hiejlzl3ow”,我想返回 “Hello”。我通过使用 for 循环和 charAt(i) 得到了解决方案,但是我无法将结果作为字符串返回(因为它是一个字符)。我尝试使用 String.valueOf 转换 charAt(i),但它只打印出第一个(或者更确切地说是 0)索引值(在本例中为 H)。有没有人可以快速解决这个问题?有没有更简单的解决方案? (注意:这是 java 的开头,所以只允许使用方法、for 循环、字符串方法和扫描器。)

//This method prints out the even indexes of a string
public static String decrypt(String question, Scanner console)
{
  System.out.print(question + " ");
  String s = console.nextLine();
  for (int i = 1; i < s.length()-1; i=i+2)
  {
     char x = (s.charAt(i));
     s = String.valueOf(x);

  }
return s;
}

【问题讨论】:

    标签: java string methods


    【解决方案1】:

    首先,字符串索引从 0 开始。所以在 for 循环中,您需要将 i 值设置为 0。代码中的第二个错误是您在 for 循环中修改了字符串 's' 的值。这就是为什么你得到 'i' 作为输出。

    试试下面的代码,你会得到预期的输出

       //This method prints out the even indexes of a string
    public static String decrypt(String question, Scanner console)
    {
      String str = "";
      System.out.print(question + " ");
      String s = console.nextLine();
      for (int i = 0; i < s.length(); i=i+2)
      {
         char x = (s.charAt(i));
         str  = str + String.valueOf(x);
    
      }
    return str;
    }
    

    【讨论】:

    • 非常感谢大家。带有 if 语句/字符串构建器的那些也可以工作,但我们还没有学会这些,所以这可能是下一个最好的。
    【解决方案2】:

    您可以使用StringBuilder:

    StringBuilder sb = new StringBuilder();
    
    // ...
    
    sb.append(x);
    
    // ...
    
    String result = sb.toString();
    

    【讨论】:

      【解决方案3】:

      试试下面的代码:

      public static String decrypt(String question, Scanner console)
          {
            System.out.print(question + " ");
            String s = console.nextLine();
            StringBuilder builder=new StringBuilder("");
            for (int i = 0; i < s.length(); i++)
            {
                if(i%2==0){
                    builder.append(s.charAt(i));
                }
            }
          return builder.toString();
          }
      

      输入:

      Hello World
      

      输出:

      HloWrd
      

      有关StringBuilder 的更多信息,请访问this 链接。

      【讨论】:

        【解决方案4】:

        java 中的数组是从零开始的。所以你应该从 0 开始索引:

        for (int i = 0; i < s.length()-1; i=i+2)
                     ^^
        

        【讨论】:

          【解决方案5】:

          您应该使用i=o 来初始化数组,并使用i &lt; s.lenght() 作为条件来遍历完整的字符串。

          【讨论】:

            【解决方案6】:
            String blah = "hbellalho";
            String evenIndices;
            for (int i = 0; i < blah.length; i++) {
                if (i % 2 != 0) {
                    evenIndices += blah.getCharAt(i);
                }
            }
            return evenIndices;
            

            您的主要问题是您正在修改原始字符串。你需要另一个(甚至更好,一个 StringBuilder,虽然为了简单起见我没有包括它)来只保存偶数值。

            当您在 Java 中对 String 执行 += 时,请记住它每次都会创建一个新的 String 对象,因此在具有大量迭代的循环中这样做并不好。

            【讨论】:

              【解决方案7】:

              您的问题很明显,因为您是初学者。让我给你解释一下

              • 在 java 中,字符串起始索引从 ZERO(0) 开始,但您在 for 循环中使用了 int i=1。
              • 使用i &lt; s.length() 或i &lt;= s.length()
              • 第三,当您从输入字符串中取出一个字符时,您将其转换为字符串并将其分配给它的变量,因为自Strings are immutable in Java 以来只有最后一个处理的字符保留在字符串中。所以你必须连接到目前为止处理的字符串中的每个新字符。
              • 这个只是为了优化(初学者可以跳过),您可以使用 StringBUffer 或 StringBuilder 类,这样每次将字符连接到字符串时都不会创建新对象。

              You can read more about StringBuffer and StringBuilder and how to use them in Java

              试试这个代码 -

              public static String decrypt(String question, Scanner console)
              {
                System.out.print(question + " ");
                String s = console.nextLine();
                String result ="":
               for (int i = 0; i < s.length(); i=i+2)
                {
                   result  += s.charAt(i);
                }
              return result;
              }
              

              Follow this link to read other interesting Java String stuff

              【讨论】:

                【解决方案8】:

                由于这是 Java 的开始,你可以试试这个:

                //This method prints out the even indexes of a string
                public static String decrypt(String question, Scanner console)
                {
                  System.out.print(question + " ");
                  String s = console.nextLine();
                  for (int i = 0; i < s.length(); i+=2)
                    {
                         System.out.print(s.charAt(i));
                    }
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-07-22
                  • 1970-01-01
                  • 2012-04-03
                  相关资源
                  最近更新 更多