英文:
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) {
long sum = 0; // Change the data type of sum to long
while (n != 0) {
sum = sum + n % 10; // Remove the 'long' before 'sum'
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;
}
}
The issue in the first version of the code is that you have defined sum
twice within the sumDigits
method: once as a long
variable and again as an int
variable. This leads to a compilation error. To fix it, you need to ensure that you only declare sum
once with the appropriate data type (long
). The corrected version of the code is shown above.
英文:
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;
}
}
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
答案1
得分: 2
你在相同的作用域中有2个名为 "sum" 的变量。
int sum,long sum。
英文:
Take a look at
public static long sumDigits(long n) {
int sum = 0;
while (n != 0) {
long sum = sum + n % 10;
n = n/10;
}
return sum;
}
You have 2 variables named "sum" in the same scope.
int sum, long sum.
答案2
得分: 0
为什么你将sum
声明为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;
}
英文:
Why you have sum
deklaret with int? The parameter from the sumDigits
method has the datatype long. So change the datatype from sum
in the methode from int to long. Then you must declare the sum
in the while no more.
I hope this is what you searching for, otherwise you must ask your question more clearly where the problem is...
First Example:
public static long sumDigits(long n) {
long sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n/10;
}
return sum;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论