分别用for循环和while循环实现九九乘法表的正向打印和颠倒打印

来源:互联网 发布:mysql数据库优化方案 编辑:程序博客网 时间:2024/06/05 22:59

需求:1、使用for循环实现九九乘法表正向和颠倒打印。2、使用while循环实现九九乘法表的正向打印和颠倒打印。

public class Print99Demo {public static void main(String[] args) {printFor99();printWhile99();printForReverse99();printWhileReverse99();}/** * for循环实现九九乘法表打印 */public static void printFor99() {for (int x = 1; x <= 9; x++) {// x代表行号for (int y = 1; y <= x; y++) {// y代表列号System.out.print(y + "*" + x + "=" + y * x + "\t");}System.out.println();}}/** * while循环实现九九乘法表打印 */public static void printWhile99() {int x, y;x = 1;while (x <= 9) {y = 1;while (y <= x) {System.out.print(y + "*" + x + "=" + y * x + "\t");y++;}System.out.println();x++;}}/** * for循环实现九九乘法表颠倒打印 */public static void printForReverse99() {for (int x = 9; x >= 1; x--) {for (int y = 1; y <= x; y++) {System.out.print(y + "*" + x + "=" + y * x + "\t");}System.out.println();}}/** * while循环实现九九乘法表颠倒打印 */public static void printWhileReverse99() {int x, y;x = 9;while (x >= 1) {y = 1;while (y <= x) {System.out.print(y + "*" + x + "=" + y * x + "\t");y++;}System.out.println();x--;}}}


0 0
原创粉丝点击