一个非常简单的题
九九乘法 这个也是在论坛看到的
1x1=1
2x2=4 2x1=2
3x3=9 3x2=6 3x1=3
4x4=16 4x3=12 4x2=8 4x1=4
5x5=25 5x4=20 5x3=15 5x2=10 5x1=5
6x6=36 6x5=30 6x4=24 6x3=18 6x2=12 6x1=6
7x7=49 7x6=42 7x5=35 7x4=28 7x3=21 7x2=14 7x1=7
8x8=64 8x7=56 8x6=48 8x5=40 8x4=32 8x3=24 8x2=16 8x1=8
9x9=81 9x8=72 9x7=63 9x6=54 9x5=45 9x4=36 9x3=27 9x2=18 9x1=9我要输出的是这个模样public class MultTable99{
public static void main(String[] args){
String spaces=" ";
for(int i=1;i<10;i++){
System.out.print(spaces.substring(0,3*(9-i)));
for(int j=i;j>=1;j--){
System.out.print(i+"x"+j+"="+i*j+" ");
}
System.out.println("");
}
}
}正确的是这个
在这输出空格的时候有一点点问题substring 是什么意思
还有后面的(0,3*(9-i))); 为什么是这样 (3*(9-i));
不能这么去掉0吗?
九九乘法 这个也是在论坛看到的
1x1=1
2x2=4 2x1=2
3x3=9 3x2=6 3x1=3
4x4=16 4x3=12 4x2=8 4x1=4
5x5=25 5x4=20 5x3=15 5x2=10 5x1=5
6x6=36 6x5=30 6x4=24 6x3=18 6x2=12 6x1=6
7x7=49 7x6=42 7x5=35 7x4=28 7x3=21 7x2=14 7x1=7
8x8=64 8x7=56 8x6=48 8x5=40 8x4=32 8x3=24 8x2=16 8x1=8
9x9=81 9x8=72 9x7=63 9x6=54 9x5=45 9x4=36 9x3=27 9x2=18 9x1=9我要输出的是这个模样public class MultTable99{
public static void main(String[] args){
String spaces=" ";
for(int i=1;i<10;i++){
System.out.print(spaces.substring(0,3*(9-i)));
for(int j=i;j>=1;j--){
System.out.print(i+"x"+j+"="+i*j+" ");
}
System.out.println("");
}
}
}正确的是这个
在这输出空格的时候有一点点问题substring 是什么意思
还有后面的(0,3*(9-i))); 为什么是这样 (3*(9-i));
不能这么去掉0吗?
两个参数:beginIndex表示从那个位置开始,endIndex表示结束位置。(位置值的范围是从0-String.length-1)
方法返回值:取一个从开始位置到结束位置的子串作为返回值。spaces.substring(0,3*(9-i)),取spaces的第0个字符开始,到spaces的第3*(9-i)字符之间的字符作为一个字符串
spaces是上面定义的一个全由空格(空格的数量我没有刻意定量,多一点没有关系)组成的字符串,由于每一行都要先打印一些空格才能得到预期的结果,随着行数增加,需打印的空格数递减,减少的数目就是你打印的非空格字符串所增加的长度的一半。这样才有居中的效果。每一行增加的长度是"aXb=c "的长度,大概是6个字符(c是两位数时,是7个字符),所以每一行打印的空格数应该递减3个字符。
总共打印9行,第9行应该递减到0,所以用3*(9-i)来实现预期。如果你想把结果放到屏幕的中间可以用spaces.substring(0,3*(9-i)+offset)来实现,offset是一个整数,大约等于屏幕的一半再减去第9行长度的一半。
for (int i = 1,j = 1; j <= 9; i++) {
System.out.print(i+"*"+j+"="+i*j+" ");
if(i==j){
i=0;
j++;
System.out.println();
}
}
}
public static void main (String[]args){
int i;
int k=0;
int z=0;
int j;
for(i=1;i<=9;i++){
for( j=1;j<=i;j++){
k=i*j;
System.out.print(i+" * "+j+" = "+k+"\t");
}
System.out.println();
z++;
}
}
}看看我写的这个咯~~~~
效果是
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
public static void main(String[] args){
for(int i=1;i<=5;i++){
for(int j=1;j<=5-i;j++){
System.out.print(" ");
}
for(int k=1;k<=2*i-1;k++){
System.out.print("*");
}
System.out.println("");
}
for(int i=1;i<=4;i++){
for(int m=1;m<=i;m++){
System.out.print(" ");
}
for(int n=1;n<=9-2*i;n++){
System.out.print("*");
}
System.out.println("");
}
} *
***
*****
*******
*********
*******
*****
***
*