修正判断阿姆斯特朗数的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
isNumberboolean is never updated from its initialfalsevalue, so the method always returns false. - The
ivariable 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
isNumberboolean 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
isArmstrongfor 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
iand unupdatedisNumber).
Testing this code:
- Input
153→ Output0(it's a 3-digit Armstrong number: 1³+5³+3³=153) - Input
9474→ Output0(4-digit: 9⁴+4⁴+7⁴+4⁴=9474) - Input
123→ Output1(not an Armstrong number) - Input
0→ Output0 - Input
-5→ Output1
内容的提问来源于stack exchange,提问作者Sanjyukta
相关产品推荐
相关产品推荐

