4-2-2 多重循环-break跳出
来源:互联网 发布:围棋软件排行 编辑:程序博客网 时间:2024/05/29 03:36
让程序输出100以内素数
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in);// int n = in.nextInt(); for(int n = 2;n < 100; n++) { int isPrime = 1; for (int i = 2; i < n ; i++ ) { if(n % i == 0) { isPrime = 0; break; } } if( isPrime == 1) { System.out.print(n+" "); } else {// System.out.println(n+"不是素数"); } } } }
错:
1、放在for循环之外,导致输出2 3
int isPrime = 1;
注:
1、for里面还有for,叫做多重循环/嵌套循环
多重循环/嵌套循环
例1
for ( int n1 = 0; n1<10 ; ++n1 ) { for (int n2 = 0; n2 <50; ++n2) { System.out.println(n1+":"+n2); } }
例2(99乘法表)
for ( int i = 1; i <= 9 ; i++ ) { for (int j = 1; j <= 9; j++) { System.out.println("\t"+(i*j)); } System.out.println(); }
里面的循环做更多次
输出前50素数的程序
1.不知道循环次数,for不合适
2.需要有一个数数器
留作作业
凑硬币(枚举,穷举)
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int amount = in.nextInt(); for (int one = 0; one <= amount; ++one ) { for (int five = 0; five <= amount/5; ++five ) { for (int ten = 0; ten <= amount/10; ++ten ) { for (int twenty = 0; twenty <= amount/20; ++twenty ) { if ( one + five*5+ten*10+twenty*20 == amount) { System.out.println(one+"张1圆,"+five+"张5圆,"+ten+"张5圆,"+twenty+"张20圆"); } } } } } } }
如果只要一种
用break?麻烦,只能跳出一层循环
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int amount = in.nextInt(); int isExit = 0; for (int one = 0; one <= amount; ++one ) { for (int five = 0; five <= amount/5; ++five ) { for (int ten = 0; ten <= amount/10; ++ten ) { for (int twenty = 0; twenty <= amount/20; ++twenty ) { if ( one + five*5+ten*10+twenty*20 == amount) { System.out.println(one+"张1圆,"+five+"张5圆,"+ten+"张5圆,"+twenty+"张20圆"); isExit = 1; break; } } if (isExit == 1) { break; } } if (isExit == 1) { break; } } if (isExit == 1) { break; } } } }
或者,加个标号
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int amount = in.nextInt(); int isExit = 0;// 标号,命名下面那个for OUT: for (int one = 0; one <= amount; ++one ) { for (int five = 0; five <= amount/5; ++five ) { for (int ten = 0; ten <= amount/10; ++ten ) { for (int twenty = 0; twenty <= amount/20; ++twenty ) { if ( one + five*5+ten*10+twenty*20 == amount) { System.out.println(one+"张1圆,"+five+"张5圆,"+ten+"张5圆,"+twenty+"张20圆"); break OUT; } } } } } } }
1、break、continue都有用
2、起名任意
0 0
- 4-2-2 多重循环-break跳出
- break跳出多重循环
- JavaScript break跳出多重循环
- JavaScript break跳出多重循环
- 用break跳出多重循环
- java中利用break跳出多重循环
- javaScript如何跳出多重循环break、continue
- JavaScript break跳出多重循环以及退出each循环
- java for循环跳出(break)/结束本次(continue)多重循环
- java带标号的break跳出多重循环
- java中如何跳出多重循环,方法不止break一种
- JAVA中如何实用break,continue,跳出多重循环
- 关于js的break不能跳出多重循环的解决方法。
- javaScript知识点----如何跳出多重循环break,continue
- break跳出循环
- break跳出循环用法
- break 跳出一层循环
- break跳出两重循环
- Mybatis入门学习篇(二)之基于注解的增删改查
- 关于iOS中的短连接方法
- POJ 2228 Naptime 环状DP
- Linux socket
- 数据结构算法——网络资源篇
- 4-2-2 多重循环-break跳出
- NAT连接方式连接主机和vmware--给虚拟机上的linux配置ip
- uva1593 Alignment of Code 习题5-1 战胜心魔觅自我!
- Numbering Paths - UVa 125 Floyd求环
- Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use
- Ubuntu下安装中文输入法
- SVN简介
- LeetCode Min Stack
- LeetCode(173) Binary Tree Iterator