【问题标题】:String index out of range error - how do I print this string vertically in java?字符串索引超出范围错误 - 如何在 java 中垂直打印此字符串?
【发布时间】:2020-11-11 06:00:37
【问题描述】:

我正在尝试垂直打印一个语句,然后用两个班级垂直向后打印,这样我就可以练习多种技能。然而,令人沮丧的是,我无法让我的程序运行并不断收到“字符串索引超出范围错误”。我不确定我是否因为我是 Java 新手而误称了我的函数。

class Main {
  public static void main(String[] args) {

    MyString.verPrint("treasure");
    MyString.backPrint("treasure");

  }
}



public class MyString {
    
    public static String verPrint(String x){
      int i = x.length();
    while(0 < i){
      x = x.charAt(i) + "\n";
      i++;
    }
      return(x);
    }
      
    public static String backPrint(String x){
        int i = x.length() - 1;
      while(i >= 0){
          i--;
          x = x.charAt(i) + "\n";
        }
      return(x);
    }
}

【问题讨论】:

  • 你看看你在verPrint中的逻辑怎么样,跟着它,写下i要取什么值。
  • 另外,您的print 方法都没有进行任何打印,因此如果它们正常工作,您将看不到任何输出。

标签: java methods char vertical-text


【解决方案1】:

您的解决方案的问题是您在第一次迭代中通过为其分配一个字符和一个新行来更新输入字符串。因此,它不适用于后面的迭代。我对您的 sn-p 进行了一些更改。你可以关注它-

public class Main {
  public static void main(String[] args) {

    System.out.println(MyString.verPrint("treasure"));
    System.out.println(MyString.backPrint("treasure"));

  }
}

class MyString {

  String x;
    
    public static String verPrint(String x){
      int i = x.length();
      String y = "";
      int j = 0;
      
    while(j < i){
      y += x.charAt(j) + "\n";
      j++;
    }
      return(y);
    }
      
    public static String backPrint(String x){
      int i = x.length() - 1;
      String y = "";
      
    while(i >= 0){
      y += x.charAt(i) + "\n";
      i--;
    }
      return(y);
    }
}

【讨论】:

  • 非常感谢!我现在看到了打印的问题。我总是忘记它返回并且不打印的静态方法。我明白为什么必须放入一个新变量,因为我认为我这样做的方式会在最后一个不起作用的字母上开始它。再次感谢您!
  • 随时!如果它对您有用,请将其标记为答案。谢谢!
【解决方案2】:
String s="HELLO";
        char[] y=s.toCharArray();
        for (char x : y) {
            System.out.println(x);
        }

输出
H
电子
L
L

String S="Hello";
        S=new StringBuffer(S).reverse().toString();
        System.out.println(S);
char[] y=S.toCharArray();
            for (char x : y) {
                System.out.println(x);
            }

你知道输出

【讨论】:

    【解决方案3】:

    您的条件有问题,同时检查 x 字符串参数的长度和索引:

    public class Main {
        public static void main(String[] args) {
    
            System.out.println(verPrint("treasure"));
            System.out.println("-----------");
            System.out.println(backPrint("treasure"));
        }
    
        public static String verPrint(String x) {
            int i = 0;
            String result = "";
            while (i < x.length()) {
                result += x.charAt(i++) + "\n";
            }
            return result;
        }
        
        public static String backPrint(String x) {
            int i = x.length() - 1;
            String result = "";
            while (i >= 0) {
                result += x.charAt(i--) + "\n";
            }
            return result;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多