有道java题:
Write a program that prints the values
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
Implement a class
public class PowerGenerator
{
    /**
    Constructs a power generator.
    @param aFactor the number that will be multiplied by itself
    */
    public PowerGenerator(int aFactor) { . . . }
    /**
    Computes the next power.
    */
    public double nextPower() { . . . }
    . . .
}
Then supply a test class PowerGeneratorRunner that calls
System.out.println(myGenerator.nextPower()) twelve times.
Use the following class as your main class:
public class PowerGeneratorRunner
{
    public static void main(String[] args)
    {
        PowerGenerator myGenerator = new PowerGenerator(10);
        for (int i = 1; i <= 12; i++)
            System.out.println(myGenerator.nextPower());
     }
}
请给个答案,谢谢

解决方案 »

  1.   

    public class PowerGenerator
    {
       private long result;     /**
      Constructs a power generator.
      @param aFactor the number that will be multiplied by itself
      */
      public PowerGenerator(int aFactor) { result = aFactor}
      /**
      Computes the next power.
      */
      public double nextPower() { result=result*result }
      . . .
    }
    Then supply a test class PowerGeneratorRunner that calls
    System.out.println(myGenerator.nextPower()) twelve times.
    Use the following class as your main class:
    public class PowerGeneratorRunner
    {
      public static void main(String[] args)
      {
      PowerGenerator myGenerator = new PowerGenerator(10);
      for (int i = 1; i <= 12; i++)
      System.out.println(myGenerator.nextPower());
      }
    }
      

  2.   

    不能有两个public类;nextPower方法应返回long型;请楼主及时结贴~O(∩_∩)O~class PowerGenerator{
    final static double FT1=0.1;
    double aFactor;
      /**
      Constructs a power generator.
      @param aFactor the number that will be multiplied by itself
      */
      public PowerGenerator(int aFactor){
      this.aFactor=aFactor*FT1*FT1;
      }
      /**
      Computes the next power.
      */
      public long nextPower(){
      aFactor=aFactor/FT1;
      return (long)aFactor;
      }
    }public class PowerGeneratorRunner {
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    PowerGenerator myGenerator = new PowerGenerator(10);
    for (int i = 1; i <= 12; i++){
    System.out.println(myGenerator.nextPower());
    }
    }
    }
      

  3.   

    Integer的最大值是2147483647,10^11越界了,可以使用long。