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

huangapple go评论89阅读模式
英文:

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 sumlong 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声明为intsumDigits方法的参数具有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;
}

huangapple
  • 本文由 发表于 2020年4月7日 13:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61073655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定