【发布时间】:2022-01-20 15:48:59
【问题描述】:
我正在尝试编写一个程序来打印给定值的一定数量的倍数。我的课在下面:
public class Multiples{
private int m; //the base number used for listing multiples
public Multiples(int x){
m = x; //x is passed in from the user
}
public int getM(){
return m;
}
public int[] make (int num){
//num gives the total amount of multiples to be printed for a given m
int[] temp = new int[num];
for (int j = 1; j < num; j++){
System.out.println("j: "+ j + ", num: " + num + ", m: "+ m);
//I'm printing the line above to check the values being used
temp[j] = j * m; //fills the array with num multiples of j
}
return temp;
}//ends make method
}
我的输出 Screen Run # 13
鉴于我用于 j、num 和 m 的值,我不明白为什么要打印 0。鉴于我从 j = 1 开始,前 5 个 7 的倍数的输出不应该是 7 14 21 35 42 吗?还要注意,前几个 6 的倍数的运行我有同样的问题(我有点理解,因为我也使用相同的算法来打印这些问题)。我尝试使用增强的 for 循环并得到完全相同的输出。
这是我第一次涉足数组;我已经查看了 here 和 here 以更好地理解数组,也许可以弄清楚为什么我的输出表现如此但看不到我的错误。
【问题讨论】:
-
索引从
0开始,您将第一个值放在索引1
标签: java arrays math printing output