for loop:
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
output:
0
1
2
3
4
shape pattern:
package B1;public class Shape {public static void main(String[] args) {//shape1();//shape2();//shape3();//shape4();//shape5();//shape6();//shape7();}private static void shape7() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row+col==10||row==col)System.out.print("* ");else if(row==1||col==1||row==9)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * * * * * * * ** * ** * ** * ** ** * ** * ** * ** * * * * * * * *private static void shape6() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row+col==10||row==col)System.out.print("* ");else if(row==1||col==9||row==9)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * * * * * * * ** * ** * ** * ** ** * ** * ** * ** * * * * * * * *private static void shape5() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row+col==10||row==col)System.out.print("* ");else if(col==1||col==9||row==1)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* ** * * ** * * ** * * ** * ** * * ** * * ** * * ** * * * * * * * *private static void shape4() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row+col==10||row==col)System.out.print("* ");else if(col==1||col==9||row==9)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* ** * * ** * * ** * * ** * ** * * ** * * ** * * ** * * * * * * * *private static void shape3() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row+col==10||row==col)System.out.print("* ");else if(col==1||col==9)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* ** * * ** * * ** * * ** * ** * * ** * * ** * * ** *private static void shape2() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row==1||row==9||row==col)System.out.print("* ");else if(row+col==10||col==1||col==9)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * * * * * * * ** * * ** * * ** * * ** * ** * * ** * * ** * * ** * * * * * * * *private static void shape1() {for (int row = 1; row <= 9; row++) {for (int col = 1; col <= 9; col++) {if(row==1||row==9||row==col)System.out.print("* ");else if(row+col==10)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}}output:* * * * * * * * ** ** ** *** ** ** ** * * * * * * * *package B1; public class Shape { public static void main(String[] args) { //shape1(); //shape2(); //shape3(); //shape4(); //shape5(); //shape6(); //shape7(); } private static void shape7() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(row==1||col==1||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape6() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(row==1||col==9||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape5() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9||row==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape4() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape3() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape2() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row==1||row==9||row==col) System.out.print("* "); else if(row+col==10||col==1||col==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape1() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row==1||row==9||row==col) System.out.print("* "); else if(row+col==10) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *package B1; public class Shape { public static void main(String[] args) { //shape1(); //shape2(); //shape3(); //shape4(); //shape5(); //shape6(); //shape7(); } private static void shape7() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(row==1||col==1||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape6() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(row==1||col==9||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape5() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9||row==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape4() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9||row==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape3() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row+col==10||row==col) System.out.print("* "); else if(col==1||col==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape2() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row==1||row==9||row==col) System.out.print("* "); else if(row+col==10||col==1||col==9) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private static void shape1() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= 9; col++) { if(row==1||row==9||row==col) System.out.print("* "); else if(row+col==10) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Enter fullscreen mode Exit fullscreen mode
Letters pattern:
package B1;public class Spattern2 {public static void main(String[] args) {//patternE();//patternI();//patternL();//patternF();//patternH();//patternM();(TBD)//patternK();}private static void patternK() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(row+col==6|| col==1)System.out.print("* ");else if(col==row-4)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* ** ** ** *** ** ** ** *private static void patternM() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(col==1||col==9-1)System.out.print("* ");else if(row==col&&col<=9/2||row+col==9-1&&col>=9/2)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * ** * * ** * * ** * ** ** ** ** ** *private static void patternH() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(col==9|| row==5||col==1)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* ** ** ** ** * * * * * * * ** ** ** ** *private static void patternF() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(row==1|| row==5||col==1)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * * * * * * * ***** * * * * * * * *****private static void patternL() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(row==9||col==1)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:********* * * * * * * * *private static void patternI() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(row==1||row==9||col==5)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}output:* * * * * * * * ********* * * * * * * * *private static void patternE() {for(int row = 1; row<=9; row++){for(int col=1; col<=9; col++) {if(row==1||row==9||col==1)System.out.print("* ");else if(row==5)System.out.print("* ");elseSystem.out.print(" ");}System.out.println();}}}output:* * * * * * * * ***** * * * * * * * ***** * * * * * * * *package B1; public class Spattern2 { public static void main(String[] args) { //patternE(); //patternI(); //patternL(); //patternF(); //patternH(); //patternM();(TBD) //patternK(); } private static void patternK() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row+col==6|| col==1) System.out.print("* "); else if(col==row-4) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * private static void patternM() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(col==1||col==9-1) System.out.print("* "); else if(row==col&&col<=9/2||row+col==9-1&&col>=9/2) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * private static void patternH() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(col==9|| row==5||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternF() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1|| row==5||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternL() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==9||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * private static void patternI() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1||row==9||col==5) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternE() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1||row==9||col==1) System.out.print("* "); else if(row==5) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *package B1; public class Spattern2 { public static void main(String[] args) { //patternE(); //patternI(); //patternL(); //patternF(); //patternH(); //patternM();(TBD) //patternK(); } private static void patternK() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row+col==6|| col==1) System.out.print("* "); else if(col==row-4) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * private static void patternM() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(col==1||col==9-1) System.out.print("* "); else if(row==col&&col<=9/2||row+col==9-1&&col>=9/2) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * private static void patternH() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(col==9|| row==5||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternF() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1|| row==5||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternL() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==9||col==1) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * private static void patternI() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1||row==9||col==5) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } output: * * * * * * * * * * * * * * * * * * * * * * * * * private static void patternE() { for(int row = 1; row<=9; row++) { for(int col=1; col<=9; col++) { if(row==1||row==9||col==1) System.out.print("* "); else if(row==5) System.out.print("* "); else System.out.print(" "); } System.out.println(); } } } output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Enter fullscreen mode Exit fullscreen mode
原文链接:Patterns:
暂无评论内容