Java For循环创建一个数组,该数组将一个数的值存储为另一个数的幂
我刚刚完成了一个代码,允许用户输入两个数字a和b,程序将计算a^b。此代码必须在不使用Math.pow方法的情况下完成 我必须在数组中保存从1-10到1-3的幂的结果。当我运行代码4时,它存储在all中。下面是我在javadoc中的全部代码,这就是问题所在Java For循环创建一个数组,该数组将一个数的值存储为另一个数的幂,java,arrays,for-loop,methods,nested-loops,Java,Arrays,For Loop,Methods,Nested Loops,我刚刚完成了一个代码,允许用户输入两个数字a和b,程序将计算a^b。此代码必须在不使用Math.pow方法的情况下完成 我必须在数组中保存从1-10到1-3的幂的结果。当我运行代码4时,它存储在all中。下面是我在javadoc中的全部代码,这就是问题所在 /** * a. Declare a two dimensional double array with 10 rows and 3 columns. b. * Store the results from the question ab
/**
* 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]);
}
}
}
将a设置为1并递增,然后将b设置为1并在每次循环运行时递增。如果a和b需要每次循环都递增,那么它们需要在循环外部初始化。问题是,在每次迭代中计算a^b,并且将a和b初始化为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^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 (int j = 0; j < array2d[0].length; j++)
修改中的问题是for循环中的转义条件
数组是0索引的,因此长度为5的数组从索引0变为4。您可以像这样在阵列中穿行:
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 (int j = 0; j < array2d[0].length; j++)
但你正在这样做:
for (int j = 1; j <= array2d[0].length; j++)
在索引为0到4的数组中,从1到5,因此一旦到达5,就会出现异常。问问自己,变量a和b在第二部分中扮演什么角色。是的,我使用它们作为toPowerOf方法我不确定,如果你不确定变量,不要使用它们。只是尝试在外部声明a和b,但现在当我尝试打印数组时,它给出了完全错误的答案,如:-306639989。@user3501243只是想让您知道,这将执行0^0,它应该返回无穷大,但您的方法返回0…正如我所说的。。。新手。但是出现的错误是java.lang.ArrayIndexOutOfBoundsException:3@user3501243我的修改可以在我的机器上运行,请发布您的修改。@Lauren_Burke将新答案添加到我的旧答案中;哦,好吧,我明白我的问题了,即使我删除了等号,当我将I=1改为I=0时,它也只是计算到1和2的幂,它修复了它,现在打印得非常完美:谢谢你的帮助:D