【JavaSE】07-ループ構造[while,do・・while,for]
07-循環構造 ①.whileサイクル ②.do・・whileサイクル ③.forサイクル ④.強化forサイクル ⑤.breakとcontinue goto文 ⑥9*9乗算表、三角形 を印刷
①.whileサイクル
構造:while(ブール式){//ループ文}ブール式がtrueである限りループは継続する.通常はループを止める方法が必要で、プログラムを常にループさせることはできません.サーバリクエストの応答リスニングなど、一部のループは常に実行されます.
②.do・・whileサイクル
whileと区別:条件を満たさなくても、プログラムが一度実行されることを保証できます.
③.forサイクル forサイクルは反復をサポートする汎用構造であり、最も有効で、最も柔軟な汎用構造である. 構文形式:for(初期化式;ブール式;更新式){//ループコード文} forループのループ回数は、for文を定義するときに限定される.IDEAにおけるショートカットキー100.for+Enterは、for(int i=0;i<100;i++) を生成することができる
forサイクルでは3つの式を省略できます
④.強化forサイクルの役割:JDK 5の後に導入された新しい特性は、主に配列と集合を遍歴するために使用される. 構文:for(宣言文:式){//コード文} 宣言文:宣言されたローカル変数のタイプは、配列要素のタイプと一致する必要があります.その値は、ループ内で対応する配列要素の額値と等しい.式:遍歴する配列名にアクセスするか、値が配列であることを返す方法です.
⑤.breakとcontinue
break:任意のループ文の本体部分では、breakを使用してループの流れを制御できます.breakは、ループを強制的に終了するために使用されます.continue:文は循環体の中で、ある循環プロセスを終了するために使用されます.すなわち、実行されていない文をスキップして、次のループを再開します.
goto文
初期のJava言語の設計はgoto文の設計があり,ラベルlabel:によって文のジャンプを実現した.しかしJavaの公式利用では使用されない.
⑥9*9乗算表、三角形を印刷
9*9乗算表の印刷
三角形を印刷
①.whileサイクル
構造:while(ブール式){//ループ文}ブール式がtrueである限りループは継続する.通常はループを止める方法が必要で、プログラムを常にループさせることはできません.サーバリクエストの応答リスニングなど、一部のループは常に実行されます.
// while 1+2+3···+100
public static void main(String[] args){
int i = 0;
int sum = 0;
while(i <= 100){
sum += i;
i++;
System.out.println(sum);
}
②.do・・whileサイクル
whileと区別:条件を満たさなくても、プログラムが一度実行されることを保証できます.
public static void main(String[] args){
int a = 0;
// while
while(a < 0){
System.out.println(a); //
a++; //
}
System.out.println("=====================");
// , do
do{
System.out.println(a); // 0
a++;
}while(a < 0);
// do···while 1+2+3···+100
int sum = 0;
do{
sum += a;
a++;
System.out.println(sum);
}while(a <= 100);
}
③.forサイクル
forサイクルでは3つの式を省略できます
// 0~100
public static void main(String[] args){
int oddsum = 0;
int evensum = 0;
for (int i = 0; i <= 100; i++) {
if(i % 2 == 0){
evensum += i;
}else{
oddsum += i;
}
}
System.out.println("oddsum = " + oddsum);
System.out.println("evensum = " + evensum);
}
public static void main(String[] args){
// 1000 5 , 6
//while
int w = 1;
while(w <= 1000){
if(w % 5 == 0) System.out.print(w + "\t"); // 5
if(w % (5*6) == 0) System.out.println(); // 6
w++;
}
System.out.println("
==============================================");
//for
for (int i = 1; i <= 1000; i++) {
if(i % 5 == 0) System.out.print(i + "\t"); // 5
if(i % (5*6) == 0) System.out.println(); // 6
}
}
④.強化forサイクル
public static void main(String[] args){
int[] numbers = {
10,20,30,40,50}; //
for(int x:numbers){
// numbers , x
System.out.println(x);
}
System.out.println("====================================");
//
for(int x = 0;x < 5;x++){
System.out.println(numbers[x]);
}
}
⑤.breakとcontinue
break:任意のループ文の本体部分では、breakを使用してループの流れを制御できます.breakは、ループを強制的に終了するために使用されます.continue:文は循環体の中で、ある循環プロセスを終了するために使用されます.すなわち、実行されていない文をスキップして、次のループを再開します.
public static void main(String[] args){
int i = 0;
while(i <= 100){
i++;
System.out.println(i);
if (i == 30) break; // i=30
}
}
public static void main(String[] args){
int i = 0;
while(i <= 100){
i++;
if(i % 10 ==0){
//i 10 ,
System.out.println();
continue;
}
System.out.print("\t" + i);
}
/*
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
*/
}
goto文
初期のJava言語の設計はgoto文の設計があり,ラベルlabel:によって文のジャンプを実現した.しかしJavaの公式利用では使用されない.
⑥9*9乗算表、三角形を印刷
9*9乗算表の印刷
public static void main(String[] args){
// 9*9
/*
1*1
1*2 2*2
1*3 2*3 3*3
1*4 2*4 3*4 4*4
*/
for(int x = 1;x <= 9;x++){
// x , 9
for(int y = 1;y <= x;y++){
// ,
System.out.print("\t" + y + "*" + x + "=" + (y*x));
}
System.out.println();
}
/*
1*4 2*4 3*4 4*4
1*3 2*3 3*3
1*2 2*2
1*1
*/
for(int x = 9;x >= 0;x--){
//
for(int y = 1;y <= x;y++){
// ,
System.out.print("\t" + y + "*" + x + "=" + (y*x));
}
System.out.println();
}
/*
1*1 1*2 1*3 1*4
1*2 2*2 2*3 2*4
1*3 2*3 3*3 3*4
1*4 2*4 3*4 4*4
*/
for(int x = 1;x <= 9;x++){
// x , 9
for(int y = 1;y <= 9;y++){
//
System.out.print("\t" + x + "*" + y + "=" + (y*x));
}
System.out.println();
}
}
三角形を印刷
public static void main(String[] args){
int line = 100;
//
for (int i = 1; i <= line; i++) {
for(int j = i;j <= line;j++) System.out.print(" "); //
for(int j = 1;j <= i;j++) System.out.print("*"); // *
for(int j = 1;j < i;j++) System.out.print("*"); // *
// , ,
System.out.println();
}
}