java 如何检查输入到数组中的用户是否为回文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25634832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:21:47  来源:igfitidea点击:

How can I check if the user entered in an array is palindrome or not?

javaarrayspalindrome

提问by Angela Vinagrera

I need help guys I have been checking on google for similar question but I cant find the most suitable answer for this. Here is my output.

我需要帮助我一直在谷歌上检查类似问题的人,但我找不到最合适的答案。这是我的输出。

Enter 3 Elements  
23
Is a palindrome  
22
Is not a palindrome  
11
Is not a palindrome  

The program:

该程序:

public static void main(String [] args)
{
  int number = 0;
  int reverse =0;
  int numCopy = 0; 
  Scanner scan = new Scanner(System.in);
  System.out.println("Enter how many numbers you want to enter");
  int num = scan.nextInt();
  System.out.println("Enter "+num +" Elements");
  numCopy = num;          
  int[] array = new int[num];             
  for(int i = 0; i < num; i++)
  { 
    array[i] = scan.nextInt();
    int digit = numCopy % 10;
    numCopy = numCopy / 10;
    reverse = (reverse * 10) +digit;
    if(num == reverse)          
    {
      System.out.println("Is a palindrome");    
    }
    else
    {
        System.out.println("Is not a palindrome");
    }
  }
}

回答by Jay

A Palindrome by definition is the same forwards and back so 11would be and 22would be. 23would not etc.

回文的定义是相同的向前和向后所以11会和22会。23不会等

Quick easy psudo code for checking if Palindrome as a STRING:

用于检查回文是否为字符串的快速简单伪代码:

function isPalin(string str)
   if(str.length() == 0) {
     return true;
   }

   int end = str.length() - 1;
   int start = 0;
   while(start < end) {
     if(str[start++] != str[end--]) {
       return false;
     }
   }
   return true; 

If you have numbers just covert it to a string then use the function.

如果您有数字只是将其转换为字符串,则使用该函数。

回答by adiggo

A palindrome, for example, we need to consider two difference case: AA and ABA. These two are all palindrome. So for integers, you need to confirm its length is even or odd. Then check from both sides: start, end.

以回文为例,我们需要考虑两种不同的情况:AA 和 ABA。这两个都是回文。所以对于整数,你需要确认它的长度是偶数还是奇数。然后从双方检查:开始,结束。

This should get the correct result.

这应该得到正确的结果。

回答by user3487063

you should have your program like below:

你应该有你的程序如下:

public static void main(String[] args)
{ 
      int number = 0;
      int reverse =0;
      int numCopy = 0;

      Scanner scan = new Scanner(System.in);
      System.out.println("Enter how many numbers you want to enter");
      int num = scan.nextInt();
      System.out.println("Enter "+num +" Elements");
      numCopy = num;

String[] array = new String[num];   

for(int i = 0; i < num; i++)
{ 

    array[i] = new Integer(scan.nextInt()).toString();
    String rev="";
for(int k=array[i].length()-1;k>=0;k--){
    rev +=array[i].charAt(k);
}
    if(array[i].equals(rev))

    { 
        System.out.println("Is a palindrome");

    } 
    else 
        System.out.println("Is not a palindrome");
} 



}