我試圖做一個程序,它可以找到迴文數(它必須是兩個3位數的產品,我希望它包含6位數字,但它並不重要)。這裏是我的代碼:java字符串 - 字符串索引超出範圍,charAt
public class palindromicNumber {
public static void getPalindromicNumber() {
boolean podminka = false;
int test;
String s;
for (int a = 999; podminka == false && a > 100; a--) {
for (int b = 999; podminka == false && b > 100; b--) {
test = a * b;
s = Integer.toString(test);
int c = 0;
int d = s.length();
while (c != d && podminka == false) {
if (s.charAt(c) == s.charAt(d)) { // I think that problem is here but I can't see what
System.out.println(s);
podminka = true;
}
c++;
d--;
}
}
}
}
}
,如果我想編譯:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:695)
at faktorizace.palindromicNumber.getPalindromicNumber(palindromicNumber.java:24)
at faktorizace.Faktorizace.main(Faktorizace.java:19)
Java結果:1
嘎,捱打。 XD – 2012-07-25 16:35:55
當您更改'd'更新與此變量相關的代碼時。比如比較和條件運算符涉及這個變量。 – 2012-07-25 16:37:10
Ouuu,那是我正在尋找的錯誤。當我知道我必須看的地方時,很容易找出它。 :D – United121 2012-07-25 18:30:40