Java项目用于识别回文数字,返回的是与匹配输出不符合。

huangapple go评论138阅读模式
英文:

Java project to identify palindrome number is returning false with matching output

问题

我学习编程已经三周了,这是我第一次在Stack Overflow上提问。我在课程项目中遇到了一个问题。我需要获取一个数字,并在数字是回文数时返回true。似乎,如果我输入的数字位数是偶数,我的while循环可以正确地判断数字是否是回文数。但如果我输入的数字位数是奇数,循环会输出一个与输入相匹配的数字,但会返回false。我尝试了移动代码位置,并尝试改变while循环的要求,结果各不相同,但无法解决这个问题。我可以查看其他同学完成的代码来修改我的代码并完成项目,但我想理解我没有理解的部分。

public class Main {

    public static void main(String[] args) {
        System.out.println(isPalindrome(111));
    }

    public static boolean isPalindrome(int number) {
        
        int lastDigit = 0;
        int reverseNumber = 0;
        
        while (number != 0) {
            lastDigit = (number % 10);
            reverseNumber = reverseNumber * 10 + lastDigit;
            number /= 10;
            System.out.println(reverseNumber);
            if (reverseNumber == number) {
                return true;
            }
        }
        return false;
    }
}

我打印了变量"reverseNumber"以查看输出是否与输入匹配。我对数组或Math.whatever等一切数学知识一无所知,所以我在寻找基础帮助。感谢您帮助我解决我承认相当问题。

英文:

I am three weeks into learning programming and this is my first time on stack overflow. I have run into an issue on a course project. I'm meant to take a number and return true if it is a palindrome. It appears that if I enter a number with an even number of digits, my while loop correctly identifies the number as a palindrome or not. If the number I plug in has an odd number of digits then the loop spits out a number that matches the input but returns false to the method. I've moved stuff around and tried changing requirements for my while loop with varying results but can't figure this issue out. I can access other students completed code to change mine and finish the project but I want to understand whatever it is that I'm not understanding.

public class Main {

public static void main ( String [] args ) {
	System.out.println ( isPalindrome ( 111 ) ) ;
}

public static boolean isPalindrome ( int number ) {
	
	int lastDigit = 0 ;
	int reverseNumber = 0 ;
	
	while ( number != 0 ) {
		lastDigit = ( number % 10 ) ;
		reverseNumber = reverseNumber * 10 + lastDigit ;
		number /= 10 ;
		System.out.println ( reverseNumber ) ;
		if ( reverseNumber == number ) {
			return true ;
		}
	}
	return false ;
}

}

I'm printing my variable "reverseNumber" to see if my output matches my input. I also have no knowledge of arrays or anything Math.whatever so I'm looking for pretty bare bones help. Thanks for helping me through my admittedly pathetic problem.

答案1

得分: 0

public static void main(String[] args) {
    System.out.println(isPalindrome(111));
}

public static boolean isPalindrome(int number) {
    int lastDigit = 0;
    int reverseNumber = 0;
    int num = number;
    int originalNumber = num;

    while (num != 0) {
        lastDigit = (num % 10);
        reverseNumber = reverseNumber * 10 + lastDigit;
        num /= 10;
        System.out.println(reverseNumber);
        if (reverseNumber == originalNumber) {
            return true;
        }
    }
    return false;
}
英文:
public static void main ( String [] args ) {
    System.out.println ( isPalindrome ( 111 ) ) ;
}

public static boolean isPalindrome ( int number ) {

    int lastDigit = 0 ;
    int reverseNumber = 0 ;
    int num = number;

    while ( num != 0 ) {
        lastDigit = ( num % 10 ) ;
        reverseNumber = reverseNumber * 10 + lastDigit ;
        num /= 10 ;
        System.out.println ( reverseNumber ) ;
        if ( reverseNumber == number ) {
            return true ;
        }
    }
    return false ;
}

you are changing your number every time and in the end comparing it to reversed value which will be 111==0, just store your original value in some temp varialbe lets say num.

huangapple
  • 本文由 发表于 2020年3月16日 10:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/60699682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定