求数组中回文数的个位数和

To find the single digit sum of palindrome numbers in array

我试图检查回文数并找到回文数的个位数总和,但我的代码没有 return 正确的值(我发现很难 return来自循环的总和值)。谁能帮我弄错。我们将不胜感激。

import java.util.Scanner;

public class SumOfPalindrome {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of array elements");
        int n = sc.nextInt();
        int[] inp = new int[n];
        System.out.println("enter the elements");
        for(int i = 0; i< n ;i++)
        {
            inp[i] = sc.nextInt();
        }
        System.out.println(SumOfPalindromeNumber(inp));
        

    }

    private static int SumOfPalindromeNumber(int[] inp )
    {
        
        int sumpal =0;
        for(int i = 0; i<inp.length;i++)
        {
            int rem =0; 
            int sum = 0;
            while(inp[i]!=0)
            {
                rem = inp[i]%10;
                sum=(sum*10)+rem;
                inp[i]/=10;
            }
            if(inp[i]==sum)
            {
                sumpal+=inp[i];
                
                if(sumpal>9)
                {
                    sumpal=singledigitsum(sumpal);
                }
            }
            
        }
        return sumpal;  
    
    }

    private static int singledigitsum(int sumpal)
    {
        int rem1 = 0;
        int sum1 = 0;
        while(sumpal!=0)
        {
            rem1=sumpal%10;
            sum1+=rem1;
            sumpal/=10;
        }
        if(sum1>9)
        {
            sum1=singledigitsum(sum1);
        }
        return sum1;
    }

}
  1. 输入数字

  2. 检查哪些数字是回文。

  3. 如果这个数字是回文数,则求其数字的和。

代码:

    import java.util.Scanner;

public class SumOfPalindrome {

    public static void main(String[] args) {
        SumOfPalindrome sumOfPalindrome=new SumOfPalindrome();
        Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of array elements");
    int n = sc.nextInt();
    int[] inp = new int[n];
    System.out.println("enter the elements");
    for(int i = 0; i< n ;i++)
    {
        inp[i] = sc.nextInt();
    }
    
    for(int i=0;i<n;i++)
    {
        if(sumOfPalindrome.isPallindrone(inp[i]))
        {
            System.out.println("No -> "+inp[i]+" Sum->"+sumOfPalindrome.sumOfDigits(inp[i]));
        }
    }
}//

public boolean isPallindrone(int no)
{
    int r,sum=0,temp;    
    

    temp=no;    
    while(no>0){    
        r=no%10;  //getting remainder  
        sum=(sum*10)+r;    
        no=no/10;    
    }    
    if(temp==sum)    
    {
        return true;
    }
    else    
    {
        return false;
    }
}



public int sumOfDigits(int no) 
{
    int sum = 0; 
    
    while (no != 0) 
    { 
        sum = sum + no % 10; 
        no = no/10; 
    }   


    return sum;
    }
}

我不明白你的代码中 sumpal 的用途。你想要一个在数组中 return sumOfPalindromes 的方法。我评论了错误的部分。

import java.util.Scanner;

public class SumOfPalindrome_1 
{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of array elements");
        int n = sc.nextInt();
        int[] inp = new int[n];
        System.out.println("enter the elements");
        for(int i = 0; i< n ;i++)
        {
            inp[i] = sc.nextInt();
        }
        //System.out.println(SumOfPalindromeNumber(inp));
        SumOfPalindromeNumber(inp);
        

    }

    private static void SumOfPalindromeNumber(int[] inp )
    {
        
        int sumpal =0;
        for(int i = 0; i<inp.length;i++)
        {
            int rem =0; 
            int sum = 0;
            while(inp[i]!=0)
            {
                rem = inp[i]%10;
                sum=(sum*10)+rem;
                inp[i]/=10;
            }
            if(inp[i]==sum)
            {
               // sumpal+=inp[i];
                
                /*if(sumpal>9)
                {
                    sumpal=singledigitsum(sumpal);
                }
                */
                System.out.println(singleDigitSum(inp[i]));
            }
            
        }
        
    
    }

    private static int singleDigitSum(int sumpal)
    {
        int rem1 = 0;
        int sum1 = 0;
        while(sumpal!=0)
        {
            rem1=sumpal%10;
            sum1+=rem1;
            sumpal/=10;
        }
        if(sum1>9)
        {
            sum1=singleDigitSum(sum1);
        }
        return sum1;
    }

}

如果情况并非如此,我们深表歉意,但这似乎是您在寻找编码测试的答案。