for循环创建一个数组,该数组将一个数字的值存储到另一个数字的幂

时间:2014-04-05 14:01:31

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

我刚刚完成了一个代码,允许用户输入两个数字(a和b),程序会计算^ 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]);
           }
           } 
      }

3 个答案:

答案 0 :(得分:0)

{
 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需要在每次循环时递增,那么它们需要在循环之外进行初始化。

答案 1 :(得分:0)

问题在于您在每次迭代中计算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 :(得分:0)

你总是在这里计算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,就会出现异常。