You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修正判断阿姆斯特朗数的Java代码:输出0(是)/1(否)

Fixing the Armstrong Number Checker in Java

Alright, let's walk through fixing your code—there are a few key issues that are keeping it from working as expected for Armstrong numbers, plus it's not outputting the 0/1 result you need.

First, let's break down the problems in your original code:

  • It only calculates the cube of each digit, which only works for 3-digit Armstrong numbers. Armstrong numbers (narcissistic numbers) require summing each digit raised to the power of the number's total digits (e.g., 1 is a 1-digit Armstrong number: 1¹ = 1; 9474 is 4-digit: 9⁴ + 4⁴ +7⁴ +4⁴ = 9474).
  • The isNumber boolean is never updated from its initial false value, so the method always returns false.
  • The i variable is completely unused and unnecessary.
  • The output logic doesn't match your requirement: you need to print 0 if it's an Armstrong number, 1 otherwise, but the code prints the unupdated isNumber boolean instead.
  • It doesn't handle edge cases like 0 (which is an Armstrong number) or negative numbers.

Here's the corrected code that addresses all these issues:

import java.util.Scanner;

class Example {
    // Returns true if N is an Armstrong number, false otherwise
    public static boolean isArmstrong(int N) {
        // Handle negative numbers (Armstrong numbers are non-negative)
        if (N < 0) {
            return false;
        }
        
        // Special case: 0 is an Armstrong number
        if (N == 0) {
            return true;
        }
        
        int originalNumber = N;
        int digitCount = 0;
        int temp = N;
        
        // Calculate the number of digits
        while (temp != 0) {
            temp /= 10;
            digitCount++;
        }
        
        int sumOfPowers = 0;
        temp = originalNumber;
        
        // Calculate sum of each digit raised to the power of digitCount
        while (temp != 0) {
            int digit = temp % 10;
            // Compute digit^digitCount (using Math.pow, then cast to int)
            sumOfPowers += (int) Math.pow(digit, digitCount);
            temp /= 10;
        }
        
        // Check if sum equals original number
        return sumOfPowers == originalNumber;
    }
}

class Arm {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        s.close(); // Properly close the scanner to avoid resource leaks
        
        boolean result = Example.isArmstrong(N);
        // Output 0 if it's an Armstrong number, 1 otherwise
        System.out.println(result ? 0 : 1);
    }
}

Key improvements made:

  • Renamed the method to isArmstrong for better readability (clearer what the method does).
  • Calculates the number of digits first, then raises each digit to that power (not just cube) to correctly identify Armstrong numbers of any length.
  • Handles edge cases: negative numbers return false, 0 returns true.
  • Properly updates the return value based on whether the sum matches the original number.
  • In the main method, we use the method's return value to print 0 or 1 as required, and properly close the Scanner to avoid resource warnings.
  • Removed all unused variables (like the old i and unupdated isNumber).

Testing this code:

  • Input 153 → Output 0 (it's a 3-digit Armstrong number: 1³+5³+3³=153)
  • Input 9474 → Output 0 (4-digit: 9⁴+4⁴+7⁴+4⁴=9474)
  • Input 123 → Output 1 (not an Armstrong number)
  • Input 0 → Output 0
  • Input -5 → Output 1

内容的提问来源于stack exchange,提问作者Sanjyukta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:46:11