gpt4 book ai didi

java - 谁能告诉我这段代码出了什么问题?

转载 作者:行者123 更新时间:2023-12-02 03:15:17 25 4
gpt4 key购买 nike

这是代码片段。我在这里找不到任何错误...有人可以帮忙吗?

while(num != 0) {
rev = rev * 10;
rev = rev + num % 10;
num = num / 10;
}

if (num == rev)
System.out.println("The number is Palindrome");
else
System.out.println("The number is not Palindrome");

最佳答案

此代码的问题是您在反转之前没有存储 num 变量的值。随着 while 开始,num 值开始变化。你应该这样做:

int temp = num; // storing the original value
int rev = 0; // initial value of rev
// --------
// while loop logic here
// --------
if(temp == rev) // change your condition here
// and you're good to go

关于java - 谁能告诉我这段代码出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56966248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com