注意for循环的嵌套使用,外行内列
package demo;
import java.util.Scanner;
public class demo7 {
public static void main(String[] args) {
/* 请编写程序,打印出如下图形,并在main方法中调用
1
12
123
1234
12345
.....*/
getNum();
}
private static int getNum() {
Scanner sc=new Scanner(System.in);
System.out.println("输入一个行数:");
int n=sc.nextInt();
for (int i = 1; i <=n; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println();
}
return n;
}
}
package demo;
import java.util.Scanner;
import static sun.plugin.util.ProgressMonitor.get;
public class demo8 {
public static void main(String[] args) {
// 案例演示:
// 需求:根据键盘录入的数据输出对应的乘法表
Scanner sc=new Scanner(System.in);
System.out.println("请输入数字:");
int n=sc.nextInt();
getChenfabiao(n);
}
private static void getChenfabiao(int a) {
for(int i=1;i<=a;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
package demo;
import java.util.Scanner;
public class demo9 {
public static void main(String[] args) {
//按照输入得行数和列数,打印相应得“*”
Scanner sc=new Scanner(System.in);
System.out.println("请输入行数:");
int x = sc.nextInt();
System.out.println("请输入列数:");
int y = sc.nextInt();
showStar(x,y);
}
private static void showStar(int x, int y) {
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
System.out.print("*");
}
System.out.println();
}
}
}