前言
本期任务:毕向东老师Java视频教程学习笔记(共计25天)
- 原视频链接:黑马程序员_毕向东_Java基础视频教程
- day01:编写HelloWorld程序
- day02:操作符与条件选择语句
- day03:循环语句与函数
- day04:数组
- day07:继承、抽象类与接口
- day08:多态
- day09:异常处理
- day11:多线程
- day12:线程安全与同步机制
- day13:String类
- day14:集合(ArrayList,LinkedList,HashSet)
- day15:集合(TreeSet)和泛型)
- day16:集合(HashMap、TreeMap)
- day17:集合框架的工具类(Arrays、Collections)
- day18:IO流(字符流读写)
- day19:IO流(字节流、转换流读写)
- day20:IO流(File对象)
一、打印三角形
/*
需求:打印三角形
思路;
两重循环
步骤:
1. 第一层循环控制三角形层数
2. 第二层循环控制'*'的个数和位置
3. 注意换行
思路;
函数实现
步骤:返回值为空,参数可设置为输出行数
1. 重载两种print99函数,一种指定输出行数,一种默认打印9行
2. 主逻辑部分复制非函数方案
*/
public class PrintTriangle {
public static void main(String[] args) {
int n = 5;
/*
打印如下三角形:
*
* *
* * *
* * * *
* * * * *
*/
printTriangle90(n);
// for (int x = 1; x <= n; x++) {
// for (int y = 1; y <= x; y++) {
// System.out.print("* ");
// }
// System.out.println();
// }
// System.out.println("--------------");
/*
打印如下三角形:
*
* *
* * *
* * * *
* * * * *
*/
printTriangle60(n);
// for (int x = 1; x <= n; x++) {
// for (int y = 1; y <= n - x; y++) {
// System.out.print(" ");
// }
// for (int y = 1; y <= x; y++) {
// System.out.print("* ");
// }
// System.out.println();
// }
}
// 打印直角三角形阵
public static void printTriangle90(int n) {
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= x; y++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("--------------");
}
// 打印等边三角形阵
public static void printTriangle60(int n) {
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= n - x; y++) {
System.out.print(" ");
}
for (int y = 1; y <= x; y++) {
System.out.print("* ");
}
System.out.println();
}
}
}
二、打印九九乘法表
/*
需求:打印九九乘法表
思路;
两重循环
步骤:
1. 第一层循环控制乘法表行数
2. 第二层循环控制公式个数和内容
3. 注意换行
思路;
函数实现
步骤:返回值为空,参数可设置为输出行数
1. 重载两种print99函数,一种指定输出行数,一种默认打印9行
2. 主逻辑部分复制非函数方案
*/
public class PrintMultiplicationTable {
public static void main(String[] args) {
System.out.println("=======================================================================");
// int n = 9;
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= i; j++) {
// System.out.print(i + "*" + j + "=" + (i * j) + "\t");
// }
// System.out.println();
// }
print99();
print99(8);
System.out.println("=======================================================================");
}
// 打印指定行数的九九乘法表
public static void print99(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
}
// 打印指定9行的九九乘法表(函数重载)
public static void print99() {
print99(9);
}
}