1

整数の桁の合計を計算するメソッドを作成します。次のメソッド ヘッダーを使用します。public static int sumDigits(long n)

プログラミングの問題 5.2. 212ページ。

プログラミング初心者であることをお許しください。この質問を理解して答えるのに苦労しています。これが私がこれまでに持っているものです。助けてください。気にしない場合は、私が間違っていることを説明してください。

import java.util.Scanner;

public class PP52v2 {

    public static void main(String [] args) {

    int sum = sumDigits(n);
    System.out.println("The sum is: " + sum);

    }//main

        public static int sumDigits(long n) {

            Scanner input = new Scanner(System.in);

            System.out.println("Enter your digits");
            n = input.nextLong();
            int num = (int)(n);
            int sum;


            while(num > 0) {

            sum += num % 10; //must mod - gives individual numbers
            num = num / 10; //must divide - gives new num

            }//loop
            return sum;
        }//sumDigits

}//class
4

6 に答える 6

2

基本的に、メソッド内でユーザー入力を処理するべきではありません。ユーザー入力をメソッドに渡す必要があります。それ以外は、すべてがよさそうです。以下にそのわずかな変更を加えました。

import java.util.Scanner;

public class PP52v2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your digits");
        long n = input.nextLong();
        int sum = sumDigits(n);
        System.out.println("The sum is: " + sum);
    }// main

    public static int sumDigits(long n) {

        int num = (int) (n);
        int sum = 0;

        while (num > 0) {

            sum += num % 10; // must mod - gives individual numbers
            num = num / 10; // must divide - gives new num

        }// loop
        return sum;
    }// sumDigits

}// class
于 2014-03-17T00:31:06.557 に答える
0

あなたのコードは私にとってはうまくいきます。

宣言されていなかったので、私はちょうど変更int sum = sumDigits(n)しました。int sum = sumDigits(0)n

正しく実行するには、スキャナーをメイン メソッドに配置し、その結果 (long値) をメソッドに渡すだけsumDigits(long n)です。

于 2014-03-17T00:27:16.837 に答える
0

プロンプトを実行する

System.out.println("Enter your digits"); n = input.nextLong();

n は現在、main(String[] args)メイン メソッドのスコープで宣言されていないためです。

于 2014-03-17T00:32:04.033 に答える
0
public static int sumDigits(int num) {
    int sum = 0;
    while(num > 0) {

        sum += num % 10; //must mod - gives individual numbers
        num = num / 10; //must divide - gives new number
    } //End loop
    return sum;
}

For one, you should not read in the number within this method, as it accepts the number as a parameter. The method should be invoked after calling long inputNum = input.nextLong(); by using int digitSum = sumDigits((int)inputNum).

于 2014-03-17T00:34:28.837 に答える
0

When writing a method, you have input, output, and side effects. The goal is to choose the right combination of the three so that the method, and program as a whole, words as expected.

It seems like your method is supposed to take a number as input and return each digit added together into one final sum.

Write A Test

Usually when you program, you come up with some code that uses your imaginary function. This is called a test. For a test, this could work:

System.out.println("123 should be 6: " + sumDigits(123));

Choose A Signature

You've already managed to right the correct signature. Nice!

Implement Method

Here's where you're a bit confused. Read through what every line of code does, and see if it is accomplishing your goal.

// set up a scanner for reading from the command line
// and print a message that you expect digits
Scanner input = new Scanner(System.in);
System.out.println("Enter your digits");

// read the next long number from the input stream
n = input.nextLong();

Why is this part of your method? You already have the number passed in as the argument n.

// cast the number to an integer
int num = (int)(n);

繰り返しますが、これが何を達成しているのかはわかりませんが、多数の場合にバグが発生する可能性があります。

// initialize the sum variable to 0.
int sum;

合計を明示的に 0 に設定する方が明確です。int sum = 0;

// add the last digit and truncate the number in a loop
while(num > 0) {
    sum += num % 10; //must mod - gives individual numbers
    num = num / 10; //must divide - gives new num
}

// actually return the calculated sum
return sum;

これは、必要なメソッドの唯一の部分のようです。うまくいけば、これが役に立ちます!

于 2014-03-17T00:34:31.507 に答える
0

入力数値は正または負のいずれかになる可能性があるため、桁数の合計を取得するには絶対値に変換する必要があります。次に、反復ごとに、商が 0 になるまで残りを合計に追加します。

public static int sumDigits(long n) {
    int sum = 0;
    long quotient = Math.abs(n);
    while(quotient > 0) {
        sum += quotient % 10;
        quotient = (long) quotient / 10;
    }
    return sum;
}
于 2014-03-17T00:35:10.993 に答える