连返回在空白的结果(java)

  • 本文关键字:java 结果 返回 空白 java
  • 更新时间 :
  • 英文 :

public class Amstrong {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
String a = String.valueOf(i);
int b = a.charAt(0);
int c = a.charAt(1);
int d = a.charAt(2);
int e = b * b * b + c * c * c + d * d * d;
if (e == i) {
System.out.println(i);
}
}
}
}

//请帮帮我,没有错误发生但结果返回的空白

您没有正确地将字符转换为数字。

可能的修复方法之一:

int b = a.charAt(0)-'0';
int c = a.charAt(1)-'0';
int d = a.charAt(2)-'0';

的另一种方法:

int b = Character.digit (a.charAt(0),10);
int c = Character.digit (a.charAt(1),10);
int d = Character.digit (a.charAt(2),10);

这两种方法都会输出:

153
370
371
407

另一种方法是:

import java.util.*;
public class ArmstrongNumber3
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number");

int n = sc.nextInt();
int b = n;
int sum = 0;
int d = 0;
while (n != 0) {
d = n % 10;
sum += (d * d * d);
n = n / 10;
}
if (sum == b) {
System.out.println("Armstrong");
} else
{
System.out.println("Not Armstrong");
}




}
}
import java.util.Scanner;
public class Amstrong {
public static void main(String args[]) {
Integer num, temp, len = 0, initVal;
double finalVal = 0;
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number");
num = scn.nextInt();
initVal = num;
temp = num;
while (temp != 0) {
temp = temp / 10;
len++;
}
for (int i = 0; i <= len; i++) {
temp = num % 10;
finalVal = finalVal + Math.pow(temp, len);
num = num / 10;
}
if (Double.valueOf(initVal) == finalVal) {
System.out.println("Amstrong");
} else {
System.out.println("Not Amstrong");
}
}
}

最新更新