为什么第一个不能执行?如果我想使用第一个,我应该添加什么??是 sum = (long) sum + n % 10; ?帮助我
Why is the first one cannot be executed?? and if i want to use the first one what should i add?? is it sum = (long) sum + n % 10; ? HELP MEEEEE
import java.util.Scanner;
public class Exercise33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input an integer: ");
long n = input.nextLong();
System.out.println("The sum of the digits is: " + sumDigits(n));
}
public static long sumDigits(long n) {
int sum = 0;
while (n != 0) {
long sum = sum + n % 10;
n = n/10;
}
return sum;
}
}
import java.util.Scanner;
public class Exercise33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input an integer: ");
long n = input.nextLong();
System.out.println("The sum of the digits is: " + sumDigits(n));
}
public static int sumDigits(long n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /=10;
}
return sum;
}
}
为什么第一个执行不了??如果我想使用第一个,我应该添加什么??是 sum = (long) sum + n % 10; ?帮助 MEEEEE
看看
public static long sumDigits(long n) {
int sum = 0;
while (n != 0) {
long sum = sum + n % 10;
n = n/10;
}
return sum;
}
您在同一范围内有 2 个名为 "sum" 的变量。
整数总和, 长总和.
为什么 sum
deklaret 与 int?来自 sumDigits
方法的参数具有数据类型 long。因此,将方法中的数据类型从 sum
从 int 更改为 long。然后你必须在 while 中声明 sum
不再。
我希望这就是你要找的,否则你必须更清楚地问你的问题是哪里出了问题...
第一个例子:
public static long sumDigits(long n) {
long sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n/10;
}
return sum;
}
import java.util.Scanner;
public class Exercise33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input an integer: ");
long n = input.nextLong();
System.out.println("The sum of the digits is: " + sumDigits(n));
}
public static long sumDigits(long n) {
int sum = 0;
while (n != 0) {
long sum = sum + n % 10;
n = n/10;
}
return sum;
}
}
import java.util.Scanner;
public class Exercise33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input an integer: ");
long n = input.nextLong();
System.out.println("The sum of the digits is: " + sumDigits(n));
}
public static int sumDigits(long n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /=10;
}
return sum;
}
}
为什么第一个执行不了??如果我想使用第一个,我应该添加什么??是 sum = (long) sum + n % 10; ?帮助 MEEEEE
看看
public static long sumDigits(long n) {
int sum = 0;
while (n != 0) {
long sum = sum + n % 10;
n = n/10;
}
return sum;
}
您在同一范围内有 2 个名为 "sum" 的变量。
整数总和, 长总和.
为什么 sum
deklaret 与 int?来自 sumDigits
方法的参数具有数据类型 long。因此,将方法中的数据类型从 sum
从 int 更改为 long。然后你必须在 while 中声明 sum
不再。
我希望这就是你要找的,否则你必须更清楚地问你的问题是哪里出了问题...
第一个例子:
public static long sumDigits(long n) {
long sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n/10;
}
return sum;
}