Why my code for checking if a number is a palindrom won't work?

后端 未结 5 1694
逝去的感伤
逝去的感伤 2021-01-29 06:28

My Java code is here:

import java.util.Scanner;
public class task2 {
    public static void main(String args[])  {
        System.out.print(\"Input a 3 digit int         


        
5条回答
  •  没有蜡笔的小新
    2021-01-29 07:08

    Here's how I'd do it: I'd use the libraries more and write a lot less code.

    I'd recommend that you learn the Sun Java coding standards and develop a formatting style. Readability promotes understanding. Style and neatness matter.

    package misc;
    
    public class PalindromeChecker {
        public static void main(String args[]) {
            for (String arg : args) {
                System.out.println(String.format("arg '%s' is" + (isPalindrome(Integer.valueOf(arg)) ? "" : " not") + " a palindrome", arg));
            }
        }
    
        public static boolean isPalindrome(int value) {
            String s = Integer.toString(value);
            String reversed = new StringBuilder(s).reverse().toString();
            return reversed.equals(s);
        }
    }
    

提交回复
热议问题