各种数字形状打印(嵌套for循环的应用)

1、12345

12345

12345

12345

<span style="font-size:14px;">public class Demo01 {

	public static void main(String[] args) {
		for(int i = 1;i <= 5; i++){
			for (int j = 1; j < 6; j++) {
				System.out.print(j);
			}
			System.out.println();
		}
	}

}

</span>

2、

11111

22222

33333

44444

55555

<span style="font-size:14px;">public class Demo02 {

	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 5; j++) {
				System.out.print(i);
			}
			System.out.println();
		}
	}

}</span>

3、

1

22

333

4444

55555

<span style="font-size:14px;">public class Demo03 {

public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}

}</span>

4、

1

12

123

1234

12345

<span style="font-size:14px;">public class Demo04 {

	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + " ");
			}
			System.out.println();
		}
	}

}</span>

5、  5

454

34543

2345432

123454321

<span style="font-size:14px;">public class Homework {

	public static void main(String[] args) {

		  for(int i=5;i>0;i--){
			  for(int j=i-1;j>0;j--){
				  System.out.print(" ");
				  }
			  for(int j=i;j<5;j++){
				  System.out.print(j);
				  }
			  for(int j=5;j>=i;j--){
				  System.out.print(j);
				  }
			  System.out.println();
			  } 

			System.out.println("=========================="); 

			for(int i = 5;i >= 0;i--){
				String txt = "";
				for(int k = 1;k < (6 -i);k++){
					txt = (6 - k) + txt;
				}
				for(int o = txt.length();o < 5;o++){
					txt = " " + txt;
				}
				txt += new StringBuffer(txt).reverse().toString().substring(1);
				System.out.println(txt);
			}

		  }
}
</span>
时间: 2024-08-01 16:36:20

各种数字形状打印(嵌套for循环的应用)的相关文章

python练习题:循环打印嵌套列表

好久没写博文了,添加一个练习题,选自<head_first_python>~~ python列表:以中括号开始和结束"[]":列表项以逗号","分隔开,使用赋值操作符"="赋予一个标识符.如: movies=["the holy",1975,"terry jones",91,["graham",["michael","john",&qu

打印数字 形状有点得味

打印如下形状的东西: 刚开始看了半天,愣住了,然后才知道了如何做. 一:先来点简单的 就先打印了如下: 这样的还是挺容易的,for循环打印每一行,每一行打印出特定多的,只是在for循环内部确定下一次是要增加打印的个数还是减少,代码: #include <iostream> using namespace std; int main(void) { const int LINES = 15; //n*n的矩形 int cnt = 1; //表示某行该输出多少个数字 for (int i = 0;

谈谈嵌套for循环的理解

谈谈嵌套for循环的理解     说for的嵌套,先说一下一个for循环的是怎么用的.      这次的目的是为了用for循环输出一个乘法口诀表,一下就是我的一步步理解.    一.   语法:            for(表达式1;表达式2;表达式3){                java语句;            }                表达式1是初始化表达式,最先执行,只执行一次.        表达式2必须是boolean类型的表达式.        for循环开始执行

递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.treejs.cn/v3/main.php#_zTreeInfo 例如现在需要编写一个这样的菜单列表.那么就可以使用JQuery的zTree插件. 先看一下数据库表结构. CREATE TABLE `permission` ( `id` int(11) NOT NULL AUTO_INCREMENT, `

列表中使用嵌套for循环[i*j for i in range(3) for j in range(3)]

利用嵌套for循环形成一个新列表 [i*j for i in range(3) for j in range(3)]相当于如下代码 li=[] for i in range(3): for j in range(3): print(i*j) li.append(i*j) print(li) 结果和[i*j for i in range(3) for j in range(3)]的结果一样都是: [0, 0, 0, 0, 1, 2, 0, 2, 4] 以上就是利用嵌套for循环形成一个新列表是怎么

&lt;09&gt; for循环的嵌套+for循环嵌套应用:打印图形+ 按要求打印图形+

for循环的嵌套: 1 #include <stdio.h> 2 3 int main(int argc, const char * argv[]) { 4 //打印出矩形的“*” 5 //控制打印了5行 6 //第一种理解方式:把打印一行的代码,执行了5次 7 for (int j=0; j<5; j++) { 8 //打印一行 * ,有5个 9 10 for(int i=0;i<5;i++){ 11 printf("*\t"); 12 } 13 printf

Java 使用嵌套 for 循环打印皇冠

1 //需求:打印王冠 2 /** 3 System.out.println("* * *"); 4 System.out.println(" *** *** *** "); 5 System.out.println(" ***** ***** ***** "); 6 System.out.println(" ***************** "); 7 System.out.println(" *********

用itertools.product简化嵌套for循环

今天这一题叫做"偷瞄到的密码": 警察跟踪一名窃贼来到了一个仓库门前.仓库的密码锁盘如下: 1 2 3 4 5 6 7 8 9 0 窃贼输入密码后进了门.警察"觉得"自己看到了密码比如1357,但是也有可能是相邻的数字(相邻仅包括正上下左右,不包括对角线),比如第一位不是1,而是相邻的4和2(不包括5). 可能的密码是哪些组合呢? 这道题目可以抽象成: 1. 0-9各自都对应了一组相邻数字 现给定一个数字串: 2. 对数字串中的每个数字,找到对应的相邻数组 3. 计

算法:用itertools.product()简化嵌套for循环

今天这一题叫做"偷瞄到的密码": 警察跟踪一名窃贼来到了一个仓库门前.仓库的密码锁盘如下: 1 2 3 4 5 6 7 8 9 0 窃贼输入密码后进了门.警察"觉得"自己看到了密码比如1357,但是也有可能是相邻的数字(相邻仅包括正上下左右,不包括对角线),比如第一位不是1,而是相邻的4和2(不包括5). 可能的密码是哪些组合呢? 这道题目可以抽象成: 1. 0-9各自都对应了一组相邻数字 现给定一个数字串: 2. 对数字串中的每个数字,找到对应的相邻数组 3. 计