0.0从我的平均值/输出值返回

1 java double average

我有2个类,DigitMath.java和DigitMathRunner.java,实验室需要的设置使用DigitMathRunner作为DigitMath的启动器.

我的代码的目的是显示Number 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
> 
Run Code Online (Sandbox Code Playgroud)

这是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;
 }

 }
Run Code Online (Sandbox Code Playgroud)

下一个代码块是Runner.

//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();
 }
}
Run Code Online (Sandbox Code Playgroud)

我也在Highschool的计算机科学1,如果我的代码不好,请告诉我如何改进,谢谢.

cri*_*007 6

变量阴影是你的问题.

double average = sum/count;
Run Code Online (Sandbox Code Playgroud)

double请删除.

并强制浮点除法

average = sum/(double)count;
Run Code Online (Sandbox Code Playgroud)

类似的问题在于 sumDigits()

此外,您可以private double average完全删除它,因为它是一个计算值.

public double averageDigits()
{
    return sum/(double)count;
}
Run Code Online (Sandbox Code Playgroud)

而在output方法中使用该方法代替average

  • `sum /(1.0)*count;` - 更好`sum /(double)count;` (4认同)