gpt4 book ai didi

Java 数字回文

转载 作者:行者123 更新时间:2023-11-29 04:32:57 26 4
gpt4 key购买 nike

我想编写一个回文程序,打印所有由两位数 (10-99) 相乘产生的回文数?

到目前为止,这是我的代码:

public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (result != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
result = result/10;
System.out.println("palindrom is " + result);
}
} else {
System.out.println("Wrong numbers");
}
return result;
}
}

如何打印两位数(10-99)相乘产生的回文数?

我的方法现在有效...

最佳答案

请检查此代码:

 public class PrintPalindrom {
public int printPalindrom (int a, int b) {
int result = a*b;
int temp = result;
int reverse = 0;
if (a >= 10 && a <= 99 && b >= 10 && b <= 99) {
while (temp != 0) {
reverse = reverse * 10;
reverse = reverse + result % 10;
temp = temp/10;
}
if(result == reverse) {
return result;
}
}
return 0;

}

int from = 10;
int to = 99;
for (int i = from; i < to; i++) {
for (int j = i + 1 ; j <= to; j++) {
int result = printPalindrom(i, j);
if(result > 0) {
System.out.println(result + ",");
}
}
}
}

关于Java 数字回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043810/

26 4 0