数字三角形的显示 java

tech2022-08-04  161

数字三角形的显示 java

实现一行符号的输出

public class line { public static void main(String[] args) { // 实现输出一行符号的功能 int start_num = 2; // 符号数量 for (int num = 1; num <= start_num; num++) { System.out.print("*"); } } }

效果

"C:\Program Files\Java\jdk1.7.0_17\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\lib\idea_rt.jar=32825:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\untitled\out\production\untitled" line ** Process finished with exit code 0

实现直角三角形的输出 1 3 5

public class sanzhi { public static void main(String[] args) { // 输出一个直角三角形 // 1行1符号,2行3符号,3行5符号 // 输出数据 int line_num = 6; // 行数 for (int row = 0; row < line_num; row++) { // 根据row算出应显示的符号数量 int char_num = 1 + 2 * row; // char num 是多少,就会输出多少个符号 for (int num = 1; num <= char_num; num++) { System.out.print("*"); } System.out.println(); } } }

效果

"C:\Program Files\Java\jdk1.7.0_17\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\lib\idea_rt.jar=32834:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\untitled\out\production\untitled" sanzhi * *** ***** ******* ********* *********** Process finished with exit code 0

实现等腰三角形的输出

代码

public class sanzhi { public static void main(String[] args) { // 为了优化性能,把常用的变量声明在这里 int char_num; int row_num; int space_num; // 输出数据 int line_num = 6; // 行数 for (int row_idx = 0; row_idx < line_num; row_idx++) { // 根据row算出应显示的符号数量 char_num = 1 + 2 * row_idx; row_num = row_idx + 1; space_num = line_num - row_num; // 输出空格 for (int sn = 0; sn < space_num; sn++) { System.out.print(" "); } // 输出符号 // char num 是多少,就会输出多少个符号 for (int num = 1; num <= char_num; num++) { System.out.print("*"); } System.out.println(); } } }

效果

"C:\Program Files\Java\jdk1.7.0_17\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\lib\idea_rt.jar=32999:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\untitled\out\production\untitled" sanzhi * *** ***** ******* ********* *********** Process finished with exit code 0

数字功能的实现

public class testnum { public static void main(String[] args) { // 生成特别的数据 int mid_num = 5; for (int t = 1; t <= mid_num; t++) { // t = 1,2,3 System.out.print(t); } for (int f = mid_num - 1; f > 0; f--) { System.out.print(f); } } }

效果

"C:\Program Files\Java\jdk1.7.0_17\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\lib\idea_rt.jar=33172:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\untitled\out\production\untitled" testnum 123454321 Process finished with exit code 0

最终完成

public class sanzhi { public static void main(String[] args) { // 为了优化性能,把常用的变量声明在这里 int char_num; int row_num; int space_num; // 输出数据 int line_num = 9; // 行数 for (int row_idx = 0; row_idx < line_num; row_idx++) { // 根据row算出应显示的符号数量 char_num = 1 + 2 * row_idx; row_num = row_idx + 1; // 行号 space_num = line_num - row_num; // 输出空格 for (int sn = 0; sn < space_num; sn++) { System.out.print(" "); } // 输出符号,符号与行号相关联 int mid_num = row_num; // 最中间的数与行号是一样的 for (int t = 1; t <= mid_num; t++) { // t = 1,2,3 System.out.print(t); } for (int f = mid_num - 1; f > 0; f--) { System.out.print(f); } System.out.println(); } } }

效果

"C:\Program Files\Java\jdk1.7.0_17\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\lib\idea_rt.jar=33178:D:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\untitled\out\production\untitled" sanzhi 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 Process finished with exit code 0
最新回复(0)