for循环的结构体
for (/*初始部分;循环条件;迭代部分*/) {
//语句体(循环操作)
}
//执行顺序:
// 1、初始部分
// 2、循环条件,满足执行语句体,不满足则跳出循环
// 3、最后执行迭代部分
// for循环中的括号部分均可省略,若全部省略则成为死循环。
执行流程
首次执行初始部分(仅执行一次)
对布尔表达式进行判断,结果为true,则执行后续逻辑代码(语句体)
本次执行完毕之后,执行迭代部分,再次判断,结果仍旧为true,则再次执行逻辑代码(语句体)
直至布尔表达式的结果为false才会退出循环结构,执行后续代码。
for循环和while循环区别
for循环与while循环相同,首次判断不满足,则一次循环都不会执行。
特点:首次即有入口条件,先判断、再执行,适用于循环次数明确的情况。
while (true) {
}
for (;true;) {
}
//死循环
break
break:终止、跳出循环结构。
示例
for (int i = 1; i < 10; i++) {
if (i == 5) {
System.out.println("终止循环");
break;
}
}
continue
continue:遇到continue,则跳出本次、进入下一次循环
示例
public static void main (String[] args) {
int sum = 0 ;
Scanner sc = new Scanner(System.in);
System.out.print("请输入科目:");
String subject = sc.next();
System.out.print("请输入班级人数:");
int n = sc.nextInt();
for (int i = 1 ; i <= n ; ) {
System.out.print("请输入第"+i+"位同学的成绩:");
int score = sc.nextInt();
if (score < 0 || score > 150) {
System.out.println("输入不合法,请重新输入。");
continue;
}
sum += score;
i++;
}
double avg = sum / 5;
System.out.println("这"+n+"位同学"+subject+"成绩的平均分是:"+avg);
}
经典示例
示例一、直角三角形
/* 倒直角三角形:
*****
****
***
**
*
*/
for (int i = 1; i <= 5; i++) { //外层控制行数
for (int j = 5; j >= i; j--) {//内层控制列数
System.out.print("*");
}
System.out.println("");
}
/*正直角三角形
*
**
***
****
*****
*/
for (int i = 1; i <= 5; i++ ) {
for (int j = 1; j <= i; j++ ) {
System.out.print("*");
}
System.out.println("");
}
打印图案 要记住 外层循环控制行,内层循环(多层循环同样)控制列
示例二、打印等腰三角形
/*等腰三角形
*
***
*****
*******
*********
***********
*/
int rows = 6;
for (int i = 1;i <= rows;i++ ) { //行
for (int j = rows - 1;j >= i;j-- ) {//列
System.out.print(" ");
}
for (int k = 1; k <= i * 2 -1 ;k++) {//三层循环也是根据最外层的循环条件
System.out.print("*");
}
System.out.println("");
}
/*倒立等腰三角形
***********
*********
*******
*****
***
*
*/
int rows = 6;
for (int i = 1; i <= rows; i++ ) {
for (int j = 1; j <= i; j++ ) {
System.out.print(" ");
}
for (int k = rows * 2; k >= i * 2; k--) {
System.out.print("*");
}
System.out.println("");
}
示例三、菱形
import java.util.Scanner;
class Demo1 {
public static void main(String[] args) { //打几颗星根据外层循环条件的改变而改变
Scanner sc = new Scanner(System.in);
int totalRows = 0;
do {
System.out.println("请输入菱形行数(数字范围在 [3,∞) 之后)");
totalRows = sc.nextInt();
} while (totalRows > 2 && totalRows % 2 != 1);
int downTotal = totalRows / 2;
int upTotal = totalRows / 2 + 1;
for (int i = 1; i <= upTotal; i++ ) {
for (int k = upTotal ; k >= i ; k--) {
System.out.print(" ");
}
for (int j = 1;j <= i * 2 -1 ; j ++) {
System.out.print("*");
}
System.out.println();
}
for (int l = 1; l <= downTotal; l++) {
for (int m = 1 ; m <= l+1 ; m++) {
System.out.print(" ");
}
for (int n = 2 * downTotal; n >= 2 * l; n-- ) {
System.out.print("*");
}
System.out.println();
}
}
}
/*
输出结果:
*
***
*****
*******
*********
*******
*****
***
*
*/
for循环的花样题目变种
示例一、心形(楼主没做输入,借鉴的话可以自行修改)
/*
* *
*** ***
***** *****
**************
************
**********
********
******
****
**
*/
class Heart {
public static void main(String[] args) {
int x = 6;
int z = 6;
int y = 6;
for (int i = 1; i <= 4; i++ ) {
for (int j = 4 ; j > i; j--) {
System.out.print(' ');
}
for (int k = 1; k <= 2 * i - 1; k++ ) {
System.out.print('*');
}
for (int l = 1; l <= x ; l++) {
System.out.print(' ');
}
x-=2;
for (int m = 1; m <= 2 * i - 1; m++ ) {
System.out.print('*');
}
System.out.println();
}
for (int n = 1; n <= z; n++ ) {
for (int p = 1; p <= n ; p++ ) {
System.out.print(' ');
}
for (int q = 2 * y; q >= 1 ; q-- ){
System.out.print('*');
}
y--;
System.out.println();
}
}
}
示例二、长方形套菱形
/*
###*###
##*#*##
#*###*#
*#####*
#*###*#
##*#*##
###*###
*/
import java.util.Scanner;
class Demo10 {
public static void main(String[] args) {
printPic();
}
public static void printPic() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图案的大小(行数,以单数为准):");
int rows = sc.nextInt();
//上半部循环
for (int i = 1; i <= rows / 2 + 1; i++ ) {
for (int j = rows / 2; j >= i; j-- ) {
System.out.print('#');
}
for (int k = 1; k <= i * 2 - 1; k++ ) {
if (k > 1 && k < i * 2 - 1) {
System.out.print('#');
}else {
System.out.print('*');
}
}
for(int l = rows / 2; l >= i; l-- ) {
System.out.print('#');
}
System.out.println();
}
//下半部循环
for (int i = 1; i <= rows / 2; i++ ) {
for (int j = 1; j <= i ; j++) {
System.out.print('#');
}
for (int k = rows - 1; k >= 2 * i ; k-- ) {
if (k >= 2 * i + 1 && k < rows - 1) {
System.out.print('#');
}else {
System.out.print('*');
}
}
for (int l = 1; l <= i ; l++ ) {
System.out.print('#');
}
System.out.println();
}
}
}
示例三、特殊的菱形【带字母】
/*
A
ABA
ABCBA
ABCDCBA
ABCDC
ABC
A
*/
import java.util.Scanner;
class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入行数:");
int totalRows = sc.nextInt();
char ch = 'A';
char m = 'A';
char n = 'A';
char ch2 = 'A';
int rows1 = totalRows / 2 + 1;
int rows2 = totalRows / 2;
//上三角
for (int i = 1; i <= rows1; i++) {
for (int j = rows1; j >= i ; j--) {
System.out.print(' ');
}
for (int k = 1; k <= 2 * i -1 ; k++) {
ch = 'A';
if (k <= i ) {
m = ch+=k-1;
System.out.print(m);
} else if (k > i) {
m--;
System.out.print(m);
}
}
System.out.println();
}
//下三角
for (int i = 1; i <= rows2;i++) {
ch2 = 'A';
for (int j = 0; j <= i; j++) {
System.out.print(' ');
}// 7 5 3 1 4 3 2 1
for (int k = rows2 * 2; k >= i * 2 ;k--) {
if (k >= rows2) {
n = ch2++;
System.out.print(n);
}else if(k < rows2 && k >= 1){
n--;
System.out.print(n);
}
}
System.out.println();
}
}
}
楼主要朝着核心架构师、技术总监前进了------Java蔡徐坤