2016-05-12 2 views
1

私は2つのクラス、DigitMath.javaとDigitMathRunner.javaを持っています。実験室で必要な設定はDigitMathRunnerをDigitMathのランチャーとして使用します。私の平均/出力から0.0が返されました

私のコードの目的は、Average of Numberを表示し、それを文に出力することです。 コンパイラはエラーは発生しませんが、出力は以下のとおりです。ここ

> > 

run DigitMathRunner 

234 has a digit average of 0.0 

10000 has a digit average of 0.0 

111 has a digit average of 0.0 

9005 has a digit average of 0.0 

84645 has a digit average of 0.0 

8547 has a digit average of 0.0 

123456789 has a digit average of 0.0 
> 

はDigitMath.java

import static java.lang.System.*; 

public class DigitMath 
{ 
    private int number; 
    private int count; 
    private int s; 
    private int sum; 
    private int input; 
    private double average; 



    public DigitMath() 
{ 
    number = 0; 
    sum=0; 
    count = 0; 
    input = 0; 
} 


    public DigitMath(int s) 
    { 
    number = s; 
    input = s; 
    } 


    public void setNums(int s) 
    { 
    number = s; 
    input = s; 
    } 


    public int sumDigits() 
    { 
    int sum=0; 
    while(input > 0) 
    { 
    sum += input % 10; 
    input /= 10; 
    } 
    return sum; 
    } 


public int countDigits() 
{ 
    count = (int)(Math.log10(number)+1); 
    return count; 
} 


public double averageDigits() 
{ 
    double average = sum/count; 
    return average; 
} 


public int output() 
{ 
    System.out.println(""+number +" has a digit average of "+""+average); 
    return number; 
} 

} 

ためのコードであり、コードの次のブロックは、ランナーです。

//Name - Seth Garcia 
//Date - 5/12/16 
//Class - 3rd Period Monaghan 
//Lab - DigitMath 

import static java.lang.System.*; 

public class DigitMathRunner 
{ 
public static void main(String args[]) 
{ 
    DigitMath test = new DigitMath(); 

     test.setNums(234); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(10000); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(111); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(9005); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(84645); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(8547); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 

     test.setNums(123456789); 
     test.sumDigits(); 
     test.countDigits(); 
     test.averageDigits(); 
     test.output(); 
} 
} 

私も高校のコンピュータサイエンス1に勤務しています。私のコードが悪い場合は、どのように改善するか感謝してください。

答えて

6

可変シャドーイングが問題です。

double average = sum/count; 

doubleを削除してください。

力浮動小数点除算

average = sum/(double)count; 

同様の問題は、それが計算値であるため、また、あなたが完全にprivate double averageを削除することができますsumDigits()

です。

public double averageDigits() 
{ 
    return sum/(double)count; 
} 

、あなたが代わりにローカル変数を使用してのsumDigits()averageDigits()にクラスのメンバを使用する必要がある代わりにaverage

+4

'sum /(1.0)* count; - より良い' sum /(double)count; ' – Thomas

+0

括弧を忘れてしまったので、 –

2

outputメソッド内でそのメソッドを使用します。

public int sumDigits() 
    { 
    sum=0; 
    while(input > 0) 
    { 
    sum += input % 10; 
    input /= 10; 
    } 
    return sum; 
    } 

public double averageDigits() 
{ 
    average = (double)sum/count; 
    return average; 
} 
0

あなたにも変更される可能性があなたのコードとこのためのストリームを使用します。 (言いたいことは、違う方法で問題を解決できるということだけです)

IntSummaryStatistics summaryStatistics = String.valueOf(number).chars().map(i -> i - '0').summaryStatistics();  
    System.out.println(summaryStatistics.getAverage()); 

String.valueOf(number) 

あなたの数の文字列

.chars() 

は(int型の値として)あなたの文字列の文字の流れを作る作る

.map(i -> i - '0') 

'0'

のintvalueを引きます
.summaryStatistics() 

は、あなたにIntSummar yStatisticsオブジェクト

関連する問題