我已經看過幾個不同的堆棧問題和Google搜索,但我沒有看到真正處理整數的逆轉,但只是字符串。整數的逆轉檢查迴文數
所以現在我的代碼可能根本不可能工作,它可能是你見過的最愚蠢的東西,沒關係,並且更正很受歡迎,但是從我希望我的代碼將要做的事開始通過100 - 999乘以兩個整數,然後檢查它是否是迴文。如果與reverse.equals(總和)是完全僞代碼,顯然不會工作,但我無法弄清楚如何檢查迴文int。有沒有簡單的方法來做到這一點?我已經閱讀了一些非常冗長和複雜的方法,但是我確信有一個簡單的方法。也許不會。 :/。無論如何,這是我的代碼。
public class PalandromicNum {
public static void main(String[] args){
int numOne = 100;
int numTwo = 100;
int toteVal;
int counter = 1000;
int sum = 0;
int finalSum = 0;
for(int i=0; i<counter; i++){
toteVal = numOne * numTwo;
numTwo++;
if(numTwo == 999){
numOne++;
numTwo = 100;
}
if(toteVal < sum){
sum += toteVal;
if(reverse.equals(sum)){
finalSum = sum;
System.out.println(finalSum);
}
}
}
}
}
再次提前致謝!
提示:palindromes與值無關,但代表它們的字符。你如何「串起」字符? – indivisible
雅,我想我可以把int轉換成一個字符串,我想我只是想看看我能用它做什麼,而不用做任何轉換。 – Kristaphonie
只需將'Integer.toString()'掛接到http://stackoverflow.com/questions/4138827/check-string-for-palindrome –