Java打印实心、空心的三角形和菱形
1.实心三角形
代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int rows;
Scanner sc = new Scanner(System.in);
System.out.println("Please input rows:");
rows = sc.nextInt();
sc.close();
for (int i = 1; i <= rows; i++) {//控制打印行数
for (int j = 1; j <= rows - i; j++) {//控制每行打印空格数
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 1; j++) {//控制打印星号数
System.out.print("*");
}
System.out.println();//每打一行,换行
}
}
}
2.空心三角形
代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int rows;
Scanner sc = new Scanner(System.in);
System.out.println("Please input rows:");
rows = sc.nextInt();
sc.close();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (i == 1 || i == rows) { // 如果是第一行或最后一行,打印所有星号
System.out.print("*");
} else if (j == 1 || j == 2 * i - 1) { // 如果是每行的第一个或者最后一个,打印星号
System.out.print("*");
} else { // 其余打印空格
System.out.print(" ");
}
}
System.out.println();
}
}
}
3.实心菱形
代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int rows;
Scanner sc = new Scanner(System.in);
System.out.println("Please input odd rows:");// 只能是奇数行
rows = (sc.nextInt() + 1) / 2;// 上半部分行数
sc.close();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) { // 下半部分不可重复打印上半部分最后一行,i=rows-1)
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4.空心菱形
代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int rows;
Scanner sc = new Scanner(System.in);
System.out.println("Please input odd rows:");// 只能是奇数行
rows = (sc.nextInt() + 1) / 2;// 上半部分行数
sc.close();
sc.close();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (i == 1) {
System.out.print("*");
} else if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {// 此处只需i=rows-1即可
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (i == 1) {
System.out.print("*");
} else if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Java打印实心、空心的三角形和菱形的更多相关文章
- java打印实心10*10正方形, 空心10*10正方形
public class PrintSquare { public static void main(String[] args) { printSolidSquare(10); System.out ...
- c++打印实心菱形,空心三角形,十字星,空心正方形,实心平行四边形
今天翻资料的时候,无意间发现了一个文件,是刚接触编程的时候用c++写的一段程序,我称之为"图形打印机",想着把所有图形都打印出来,后来发现其实每种图形的代码都是一个思路,就不想做重 ...
- Java基础知识强化07:打印出空心菱形
1.如图打印出空心菱形: 2.下面是逻辑实现代码: package himi.hebao04; import java.util.Scanner; public class TestDemo08 { ...
- Java打印空心菱形
使用Java打印空心菱形 public static void main(String[] args) { int n = 5; //这里输出菱形的上半部分 for (int i = 1; i < ...
- python打印实心等边三角形和空心等边三角形
#1 打印实心等边三角形 n = 5 for i in range(1, n+1): # 控制三角形的高,也就是层数 for k in range(2*(n-i)): # 控制每层第一个*的空格,从最 ...
- Java打印空心三角
Java打印空心三角 public static void main(String[] args) { int n=5; //n表示输出空心三角形行数,这里以5行为例 for(int i=1;i< ...
- 通过原生JS打印一个空心菱形图案
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- java打印正金字塔,倒金字塔和“水影”金字塔
java打印正金字塔,倒金字塔和"水影"金字塔 --------原创文章,若要转载,请注明出处 小小少年 闲来无事,想起自己初学java的时候做的经典的无非就是打印出一些有意思 ...
- Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录)
Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录) 作者:王可利(Star·星星) 效果图: 代码如下: class Star8 { public static void m ...
随机推荐
- C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口
原文:C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http ...
- 浅析JAVA的垃圾回收机制(GC)
1.什么是垃圾回收? 垃圾回收(Garbage Collection)是Java虚拟机(JVM)垃圾回收器提供的一种用于在空闲时间不定时回收无任何对象引用的对象占据的内存空间的一种机制. 注意:垃圾回 ...
- HDU 2842 Chinese Rings(矩阵高速功率+递归)
职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第 ...
- 统计推断(statistical inference)
样本是统计推断的依据: 统计推断的基本问题可以分为两大类: 估计问题 点估计, 区间估计 假设检验 1. 点估计 设总体 X 的分布函数 F(x;θ) 的形式已知,θ 是待估参数.X1,X2,-,Xn ...
- $.extend(true,{},a,b)解析
原文:$.extend(true,{},a,b)解析 版权声明: https://blog.csdn.net/bsfz_2018/article/details/81738437 什么是$.exten ...
- 在efcore 中创建类 通过实现IEntityTypeConfiguration<T>接口 实现实体类的伙伴类 实现FluentApi
1 创建实体类: public partial class NewsCategory : IAggregationRoot { public NewsCategory() { } public Gui ...
- ArcGIS API for Silverlight 学习笔记
这里主要讲解展示不同的服务地图 先看一个实例: 新建一个Silverlight项目,在MainPage.xaml文件中,引入 ESRI.ArcGIS.Client 命名空间和 ESRI.ArcGIS. ...
- NS2网络模拟(5)-homework01.tcl
1: #NS2_有线部分\homework01.tcl 2: 3: #创建两个结点,深圳到北京的TCP连接,图形将数据显示出来,计算吞吐率,画图分析 4: #tcp上层用ftp 5: #udp上层用c ...
- SQL查询表结构 相关语句
--查看列注释select * from all_col_comments where table_name=upper('XXXXX')--查看表结构select * from user_tab_c ...
- windows IIS发布.net core网站的环境配置
1.安装对应的.net core的runtime2.安装Windows Server Hosting下载地址:https://www.microsoft.com/net/download/core#/ ...