2015-10-15 120 views
0

我是java中的新成員,我得到了阿姆斯特朗數字的分配。 我已經創造了新的類ArmstrongNumber.java,我從本網站初始化方法:http://www.programmingsimplified.com/java/source-code/java-program-armstrong-numberJAVA如何返回間隔[100; 999]

現在,在其中的主要方法我創造了另一個方法的類在那裏我打電話ArmstrongNumber類,現在我要回到阿姆斯特朗數間隔從[100至999]。

有我現在卡在哪裏。

public static void armtrongNumbs() 
    { 
     ArmstrongNumber returnObj = new ArmstrongNumber(); // here i m calling class. 

     int start = 100; 
     int end = 999; 

     for(int i = start; i<= end; i++) 
     { 
      number = i + number; 
      returnObj.Armstrong(number); 
     } 
     //returnObj.Armstrong(); 
    } 

我的循環怎麼會只返回阿姆斯特朗數?

編輯:ArmstrongNumber類

class ArmstrongNumber 
{ 
    public void Armstrong(int number) 
    { 
     int n, sum = 0, temp, remainder, digits = 0; 
     Scanner in = new Scanner(System.in); 
     System.out.println("Input a number to check if it is an Armstrong number");  
     n = in.nextInt();  
     temp = n;  
     // Count number of digits 
     while (temp != 0) { 
     digits++; 
     temp = temp/10; 
     } 

     temp = n; 

     while (temp != 0) { 
     remainder = temp%10; 
     sum = sum + power(remainder, digits); 
     temp = temp/10; 
     } 

     if (n == sum) 
     System.out.println(n + " is an Armstrong number."); 
     else 
     System.out.println(n + " is not an Armstrong number.");   
    } 

    static int power(int n, int r) { 
     int c, p = 1; 

     for (c = 1; c <= r; c++) 
     p = p*n; 

     return p; 
    } 
} 
+0

什麼returnObj? –

+0

@ParaSara這是新創建的對象,我打電話給ArmstrongNumber類,我初始化armostring算法。 – BinaryTie

+0

ok,returnObj.Armstrong(number)返回true或false,對不對? –

回答

0

根據您的要求,您需要的ArmstrongNumber.java邏輯和模具以適合按您的要求。

你只需要使用下面的代碼,並可以不用擔心使用ArmstrongNumber.java

package hello; 

public class Abc { 
public static void main(String[] args) { 
    int n, sum, temp, remainder, digits; 

    int start = 100; 
    int end = 999; 

    for (int i = start; i <= end; i++) { 

     sum = 0; 
     digits = 0; 

     temp = i; 

     // Count number of digits 

     while (temp != 0) { 
      digits++; 
      temp = temp/10; 
     } 

     temp = i; 

     while (temp != 0) { 
      remainder = temp % 10; 
      sum = sum + power(remainder, digits); 
      temp = temp/10; 
     } 

     if (i == sum) 
      System.out.println(i + " is an Armstrong number."); 

    } 
} 

static int power(int n, int r) { 
    int c, p = 1; 

    for (c = 1; c <= r; c++) 
     p = p * n; 

    return p; 
} 

} 

在這裏你可以看到,總和和數字如何初始化爲零的每一個數字,然後邏輯的其餘部分相同。您可以驗證153,370,371,407是否打印爲阿姆斯壯數字。

希望這有助於

0

嘗試像

public int[] Armstrong(int start ,int end){ 
int a[],i=0; 
for(int i = start; i<= end; i++) 
{ 
     number = i + number; 

     int n, sum = 0, temp, remainder, digits = 0; 
     Scanner in = new Scanner(System.in); 
     System.out.println("Input a number to check if it is an Armstrong number");  
     n = in.nextInt();  
     temp = n;  
     // Count number of digits 
     while (temp != 0) { 
     digits++; 
     temp = temp/10; 
     } 

     temp = n; 

     while (temp != 0) { 
     remainder = temp%10; 
     sum = sum + power(remainder, digits); 
     temp = temp/10; 
     } 

     if (n == sum) 
     a[i++]=n; 
     else 
     System.out.println(n + " is not an Armstrong number."); 
} 
return a;  
    } 
static int power(int n, int r) { 
    int c, p = 1; 

    for (c = 1; c <= r; c++) 
     p = p * n; 

    return p; 
} 

}