【发布时间】: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