需求一:打印出九九乘法表
public class MultiplicationTable { public static void main(String[] args) { int a; int b; int c; for(int i = 1; i < 10; i++) { for(int j = 1; j <= i; j++) { a = i; b = j; c = a * b; System.out.print(a + " * " + b + " = " + c + " "); } System.out.println("\n"); } } }
需求二:使用循环输出等腰三角形
import java.util.Scanner; public class IsoscelesTriangle { public static void main(String[] args) { System.out.println("请输入一个整数:"); //在输出端输入一个数 Scanner input = new Scanner(System.in); int num = input.nextInt(); for(int i = 1; i <= num; i++) { int a = num - i; while(a != 0) { System.out.print(" "); a--; } int b = i * 2 - 1; while(b != 0) { System.out.print("*"); b--; } System.out.println("\n"); } } }