我想让它循环到每个方法/部分,但我得到错误。当我发表评论时,他们的工作完美,但总的来说,我得到了:
线程中的异常" main" java.lang.StringIndexOutOfBoundsException: 字符串索引超出范围:java.lang.String.charAt为0(未知 来自RecursionTester.main(RecursionTester.java:25)
我的代码:
import java.util.Scanner;
public class RecursionTester {
public static void main(String[] args) {
int power;
int base;
String sentence;
int digit;
int num;
char answer;
Scanner sc = new Scanner(System.in);
do {
System.out.print("What base would you like? ");
base = sc.nextInt();
System.out.print("What power would you like? ");
power = sc.nextInt();
System.out.println(base + " to the " + power + " power is " + raiseToPower(base, power));
System.out.print("\nWould you like raise another number to a power (y or n)? ");
answer = sc.nextLine().charAt(0);
} while (answer == 'Y' || answer == 'y');
do {
System.out.print("Type a sentence and check if it's a palindrome: ");
sentence = sc.nextLine().trim();
if (palindrome(sentence))
System.out.println(sentence + " is a palindrome");
else
System.out.println(sentence + " is not a palindrome");
System.out.print("\nWould you like to reverse another text string (y or n)? ");
answer = sc.nextLine().charAt(0);
} while (answer == 'Y' || answer == 'y');
sc.close();
}
//private methods here
}