這是項目歐拉的問題#4當我增加for循環開始和結束的值時,我的代碼出了什麼問題?
我能夠得到他們提供的解決方案,當他們第一次提出問題。 (兩個2位數字的乘積是9009 = 91×99)。但是,一旦我嘗試使用三位數字進行操作,控制檯中就不會顯示任何內容。
所以,當我用for(i = 10; i < 99; i++)
和for(j = 10; j < 99; j++)
啓動循環時,我得到了解決方案9009.但是,當我改變它時,情況並非如此。
這裏是我的代碼:
public class LargestPalindromeProduct {
public static String reverseNumber(String x) {
String number = x;
String numberReversed = new StringBuffer(number).reverse().toString();
return numberReversed;
}
public static void main(String[] args) {
//A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
//Find the largest palindrome made from the product of two 3-digit numbers.
int x = 0;
for(int i = 100; i < 1000; i++) {
for(int j = 100; j < 1000; j++) {
x = i*j;
}
if((String.valueOf(x)).equals(reverseNumber(String.valueOf(x)))) {
System.out.println(x);
}
}
} // close main
} // close class
的到底是不是有可能,有沒有迴文數誰是兩個3產品數字? – 2015-01-04 14:26:54
(順便說一句:你把if語句放在inner'for'之外)。 – 2015-01-04 14:28:00
@GáborBakos雖然它對2位數的數字仍然有效。 (儘管它列出了該範圍內的所有迴文數字)。 – 2015-01-04 14:29:30