java中的数字比较

时间:2017-07-05 14:09:39

标签: java

这可能是一个非常愚蠢的问题。我试图扭转输入数字并进行比较。如果它们相同则输出应该是“输入的数字是回文”但是,我正在为每个数字退出,就像它是回文。

package com.practise.examples;

import java.util.Scanner;

public class Practise 
{
    public static void main(String[] args) {


        Scanner s=new Scanner(System.in);
        System.out.println("enter the number to reverse it:\n");
        int num=s.nextInt();
        int revNum=0;

        while(num!=0)
        {
            revNum=revNum *10;
            revNum= revNum+ num%10;
            num=num/10;
        }
        System.out.println("the reverse of the number is: " +revNum);

            if(revNum==num)
            {
                System.out.println("the number is a palindrome" );

            }
            else
            {
                System.out.println("the number entered is not a palindrome");

            }

        }
    }

5 个答案:

答案 0 :(得分:4)

更简单的方法:

String num=Integer.toString(s.nextInt());
String revNum = new StringBuffer(num).reverse().toString();
System.out.println("the reverse of the number is: " +revNum);

if(revNum.equals(num))
    System.out.println("the number is a palindrome" );
else
    System.out.println("the number entered is not a palindrome");

如果你坚持你的方法:

    Scanner s=new Scanner(System.in);
    System.out.println("enter the number to reverse it:\n");
    int num=s.nextInt();
    int original = num;
    int revNum=0;

    while(num!=0)
    {
        revNum=revNum *10;
        revNum= revNum+ num%10;
        num=num/10;
    }
    System.out.println("the reverse of the number is: " +revNum);

        if(revNum==original)
        {
            System.out.println("the number is a palindrome" );

        }
        else
        {
            System.out.println("the number entered is not a palindrome");

        }

    }

答案 1 :(得分:1)

尝试这样的事情:

public static int reverse(int num)
{
  try
  {
    return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
  }
  catch (Exception ex)
  {
    // Should not happen...
  }
}

答案 2 :(得分:0)

您已修改num以获得反向,然后使用相同的num进行比较。使用临时变量。

答案 3 :(得分:0)

你的想法似乎只适用于非常具体的实例,代码本身......好吧,生病只是给你一个不同的想法。

创建一个堆栈和一个队列。

获取原始输入,并将每个连续项添加到每个堆栈和队列中。

一旦队列和堆栈已满......

iterate until length=0
if stack.pop != queue.dequeue
    palindrome=false

答案 4 :(得分:0)

你可以将你的号码转换成一个字符串并检查它的回文吗?

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("enter the number to reverse it:\n");
        int num=s.nextInt();
        if (isPalindrome(num+"")){
            System.out.println("the number is a palindrome" );
        }else{
            System.out.println("the number entered is not a palindrome");
        }
    }
    public static boolean isPalindrome(String s) {
        int n = s.length();
        for (int i = 0; i < (n/2); ++i) {
            if (s.charAt(i) != s.charAt(n - i - 1)) {
                return false;
            }
        }

        return true;
    }
}