【问题标题】:For loop to create an array which stores values of a number to the power of another numberFor循环创建一个数组,该数组将一个数字的值存储到另一个数字的幂
【发布时间】:2014-05-17 21:26:05
【问题描述】:

我刚刚完成了一个代码,允许用户输入两个数字(a 和 b),然后程序会计算 a^b。这段代码必须在不使用Math.pow 方法的情况下完成。

我必须将 1-10 到 1-3 的幂的结果保存在一个数组中。当我运行我的代码 4 存储在所有。这是我在 javadoc 中的全部代码的问题。

/**
 * a. Declare a two dimensional double array with 10 rows and 3 columns. b.
 * Store the results from the question above in a 2D array. c. Use a nested loop
 * to print this array out and also add up all the array values. d. Print this
 * sum to the screen. 7. Calling public static methods from another class: a.
 * Write as second class called MyTestProgram which has only a main method in
 * it. b. In this main method make use of the toPowerOf method defined in
 * BlueTest2 to calculate 73 (7 cubed or 7*7*7) and write the result to the
 * screen.
 * 
 */

public class BlueTest2 {
    public static void main(String[] args) {
        int result = toPowerOf(20, 5);
        System.out.println("The power of these numbers is: " + result);
        {
            for (int i = 1; i <= 10; i++) {
                for (int j = 1; j <= 3; j++) {
                    int loopResult = toPowerOf(i, j);
                    System.out.println(i + " to the power of " + j + " is: "
                            + loopResult);
                }
            }

        }
        {
            int[][] array3d = new int [10] [3];
         for (int i = 1; i <= array3d.length; i++) 
          {
         for (int j = 1; j <= array3d[0].length; j++) 
          {
        int loopResult = toPowerOf(i, j);
        array3d[i][j] = loopResult;
        System.out.println("The variable here is: " + array3d[i][j]);
                }
            }
        }
    }

    public static int toPowerOf(int a, int b) {
        int t = a;
        int result = a;
        for (int i = 1; i < b; i++) {
            t = t * a;
            result = t;
        }
        return result;
    }
}

我的新变化只是我的主要方法的第二部分

       {
        int[][] array3d = new int [10] [3];
        for (int i = 1; i <= array3d.length; i++) 
          {
         for (int j = 1; j <= array3d[0].length; j++) 
           {
            int loopResult = toPowerOf(i, j);
            array3d[i][j] = loopResult;
            System.out.println("The variable here is: " + array3d[i][j]);
           }
           } 
      }

【问题讨论】:

  • 问问自己,变量ab在第二部分有什么作用。
  • 是的,我将它们用于 toPowerOf 方法,但我不确定
  • 如果您不确定变量,请不要使用它们。

标签: java arrays for-loop methods nested-loops


【解决方案1】:

问题是您在每次迭代中都在计算a^b,并且您将ab 初始化为1。所以,每次迭代计算(i+1)^(j+1) 会更好:

for (int i = 0; i < array2d.length; i++) {
    for (int j = 0; j < array2d[0].length; j++) {
            //int a = 1;
            //a++;
            //int b = 1;
            //b++;
            int loopResult = toPowerOf(i+1, j+1);
            array2d[i][j] = loopResult;
            System.out.println("The variable at " + i + " " + j
                    + " is: " + array2d[i][j]);
    }
}

【讨论】:

    【解决方案2】:
    {
     int a = 1;
     a++;
     int b = 1;
     b++;
     int loopResult = toPowerOf (a, b);
     array2d[i][j] = loopResult;
     System.out.println("The variable at " + i + " " + j + " is: " +array2d[i][j]);
    }
    

    您将 a 设置为 1 并递增它,然后将 b 设置为 1 并在每次循环运行时递增它。如果每次循环执行时 a 和 b 都需要递增,那么它们需要在循环外初始化。

    【讨论】:

    • 刚刚尝试在外面声明 a 和 b 但现在当我尝试打印数组时,它给出了完全错误的答案,例如:-306639989。
    【解决方案3】:

    你总是在这里计算 2^2:

    int a = 1;
    a++;
    int b = 1;
    b++;
    int loopResult = toPowerOf(a, b);
    

    你想要像上面那样的东西(打印结果的地方),像这样:

    for (int i = 0; i < array2d.length; i++) {
        for (int j = 0; j < array2d[0].length; j++) {
            {
                int loopResult = toPowerOf(i, j);
                array2d[i][j] = loopResult;
                System.out.println("The variable at " + i + " " + j
                    + " is: " + array2d[i][j]);
            }
        }
    }
    

    您修改中的问题是 for 循环中的转义条件。

    数组是 0 索引的,因此长度为 5 的数组从索引 0 到 4。您可以像这样遍历数组:

    for (int j = 0; j < array2d[0].length; j++)
    

    但你正在这样做:

    for (int j = 1; j <= array2d[0].length; j++)
    

    在索引为 0 到 4 的数组中从 1 到 5,因此一旦到达 5,就会出现异常。

    【讨论】:

    • @user3501243 只是让你知道,这将执行0^0,它应该返回无穷大,但你的方法返回0...
    • 正如我所说的......新手。但是出现的错误是 java.lang.ArrayIndexOutOfBoundsException: 3
    • @user3501243 我的修改在我的机器上工作,请发布您的更改。
    • @Lauren_Burke 将新答案添加到我的旧答案中;)
    • 哦,好吧,我明白了我的问题,即使我删除了等号,当我将 i=1 更改为 i=0 时,它也只是计算 1 和 2 的幂,它修复了它,现在是完美打印:) 感谢您的所有帮助:D
    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2018-04-23
    • 2012-08-11
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多