黑马程序员技术交流社区
标题:
一个小问题
[打印本页]
作者:
马上都有
时间:
2014-4-15 16:13
标题:
一个小问题
/* 用控制台程序输出九九乘法表;输出结果按下图所示:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
.........*/
public class Test6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int twoArray[][] = new int[10][10];
int i,j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
twoArray[j][i] =j*i;
System.out.print(j+"*"+i+"="+twoArray[j][i]+" ");
}
System.out.println();
}
}
}
运行结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
当把数组定义为: int twoArray[][] = new int[9][9]就足够了;但运行结果就没有了最后一行的结果,搞不明白,请各位高手指点。
作者:
宋超2356
时间:
2014-4-15 16:17
本帖最后由 宋超2356 于 2014-4-15 16:18 编辑
你这样写就足够了
public class Test6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int twoArray[][] = new int[9][9];
int i,j;
for(i=0;i<9;i++){
for(j=0;j<=i;j++){ //数组都从0开始啊,你从1开始的话自然要多一个
twoArray[j][i] =j*i;
System.out.print(j+"*"+i+"="+twoArray[j][i]+" ");
}
System.out.println();
}
}
}
复制代码
作者:
月光海
时间:
2014-4-15 16:43
public static void main(String[] args)
{
int twoArray[][] = new int[9][9];
int i,j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
twoArray[j-1][i-1] =j*i;
System.out.print(j+"*"+i+"="+twoArray[j-1][i-1]+" ");
}
System.out.println();
}
}
复制代码
数组的下标是从0开始的,你计算的结果是从第二个位置开始的,看下我的代码,把代码改成twoArray[j-1][i-1]这样就可以了
作者:
坏米饭
时间:
2014-4-15 18:05
数组默认从0开始。 当把数组定义为: int twoArray[][] = new int[9][9];
//下面就要改成
int i,j;
for(i=0;i<9;i++){
for(j=0;j<=i;j++){
twoArray[j][i] =(j+1)*(i+1);
System.out.print((j+1)+"*"+(i+1)+"="+twoArray[j][i]+" ");
}
作者:
你为谁归来
时间:
2014-4-15 20:09
数组的角标是从0开始的,当你定义为9的时候,也就代表里面有0,1,2,3,4,5,6,7,8 这9个。
但是你的for循环却是从1开始的,直接把0跳过去了,也就编程1,2,3,4,5,6,7,8 这8个元素了,
所以才会少一个。
作者:
luoyilan222
时间:
2014-4-15 21:02
兄弟 数组的下标是从 0 开始 你循环是从 下标 1 开始 当然最后会少一行
作者:
姜姗姗
时间:
2014-4-15 21:59
public static void main(String[] args) {
// TODO Auto-generated method stub
int twoArray[][] = new int[10][10];
int i,j;
for(i=1;i<=9;i++){//外循环控制乘法表的行数
for(j=1;j<=i;j++){//内循环控制每一行的表达式的个数
twoArray[j][i] =j*i;
System.out.print(j+"*"+i+"="+twoArray[j][i]+" \t");
}
System.out.println();
}
}
你这样写是对的 ,没有错误,只要把外循环控制的开始、结束点都想明白,内循环主要是用来控制每一行的表达式的个数的,分析好了思路,嵌套循环应该米有问题
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2