阿姆斯特朗号码java

时间:2014-08-04 15:38:18

标签: java

我遇到了一些简单代码的问题。我还没有看到我的代码中的问题出在哪里。 应该返回true 时返回false ,因为153是一个阿姆斯特朗号。

以下是我的代码:

public class Armstrong {

   static double nArms, unidad, decena, centena, aux;


   Armstrong(){

   }

   Armstrong(double nArms){
      this.nArms = nArms;
   }

   public boolean esArmstrong(double nArms){

    aux = nArms % 100;
    centena = nArms / 100;
    decena = aux / 10;
    unidad = aux % 10;


    this.nArms = Math.pow(unidad, 3) + Math.pow(decena, 3) +Math.pow(centena, 3);

    if(this.nArms == nArms){
        return true;
    }else{
        return false;
    }
}



public static void main(String[] args) {

    Armstrong arms = new Armstrong();


    System.out.println(arms.esArmstrong(153));



}

}

4 个答案:

答案 0 :(得分:2)

当您打算进行整数运算时,您正在使用double。例如,当你写

centena = nArms / 100;

您正在进行浮点除法(并且centena被赋值1.53)但您想执行整数除法。请改用intlong(或BigInteger)。

答案 1 :(得分:0)

正如其他人已提到的那样,从不使用Double进行Integer计算 现在,如果我是你,我会优化我的代码

int check=0;
int n=num; // num is user-input number
while(n>0)//suppose n =153
{
int rem=n%10;
check=check+(int)Math.pow(rem,3);
n=n/10;
}
if(check==num)
System.out.println(num+" is Armstrong");

/*First time while loop runs rem = 3 and n= 15
So check = 0+3*3*3=27
Second time while loop runs rem = 5  and n= 1
So check = 27+5*5*5 = 152
Again n =1 so rem = 1 and check = 152+1*1*1 = 153
This time the thw while fails the exits
ultimately check == num so it is armstrong */

答案 2 :(得分:0)

import java.util.Scanner;

public class Armstrong {
public static void main(String args[]) {
    System.out.println("Input number of digit to find out Armstrong");
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    String s[] = new String[r];
    System.out.println("Please enter digits");
    StringBuilder sb = new StringBuilder(r);
    for (int i = 0; i < r; i++) {
        String userInput = sc.next();
        s[i] = userInput;
        sb.append(s[i]);
    }
    int e = Integer.parseInt(sb.toString()); //this is the Integer value entered to check Armstrong number

    int d;
    int k[] = new int[r];
    for (int j = 0; j < r; j++) {
        d = Integer.parseInt(s[j]);
        k[j] = d * d * d * d* d* d;
    }
    int m[] = new int[r + 1];
    int n[] = new int[r];
    for (int l = 1; l <= r; l++) {
        n[l - 1] = m[l - 1] + k[l - 1];
        m[l] = n[l - 1];
    }
    if (e == m[r]) {
        System.out.println("Entered number is Armstrong number");
    } else {
        System.out.println("Entered number is not an Armstrong number");
    }
}

答案 3 :(得分:-1)

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number");
    int a, c, d, e = 0, temp = 0;
    a = sc.nextInt();
    c = a;
    int z = a;
    while (c > 0) {
        c = c / 10;
        temp++;
    }

    System.out.println("//");
    int temp2 = temp;

    while (temp2 > 0) {
        int temp1 = 1;

        d = a % 10;
        for (int i = 0; i < temp; i++) {
            temp1 = temp1 * d;

        }
        e = e + temp1;

        a = a / 10;
        temp2--;
    }
    if (z == e) {
        System.out.println("number is armstrong");
    } else {

        System.out.println("number is not armstrong");
    }

}