フロー制御と制御ステートメントの紹介

「プロセス制御」:これは、プログラムが正常な状態または異常な状態でどのように実行されるかを調整することを意味します。これには何らかの判断が必要であり、この判断に基づいて、「プロセス制御」である「その他のこと」を実行し
ます。

判断ステートメント_ifステートメント:

if statement_format 1:
if(ブール式){
//「ブール式」の結果がtrueの場合、これを実行する
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if (age >= 23 && age <= 25) {
System.out.println("年龄合适,那么你的身高呢?");
double h = sc.nextDouble();
if (h > 1.6) {
System.out.println("身高也很合适,那么你的月收入呢?");
int s = sc.nextInt();
if (s > 20000) {
System.out.println("年龄、身高、月薪都很合适...我们约会吧!!");
} } }
System.out.println("程序结束!");
}

ifステートメント_形式2:
if(ブール式){
//ブール式がtrueを返した場合はここで実行
} else {
//ブール式がfalseを返した場合はここで実行
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你考试的分数:");
int s = sc.nextInt();
if (s >= 60) {
System.out.println("及格!");
}else{
System.out.println("不及格!");
}
System.out.println("程序结束!");
}

if statement_format 3:
if(ブール式1){
式1が真:ここで実行
} else if(ブール式2){//式1が偽:ここで実行
式2が真:ここで実行
} else if(ブール式3){//式2が偽:ここで実行
式3は真:ここで実行
} else {//式3はe:ここで実行
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的分数:");
int s = sc.nextInt();
if (s >= 0 && s < 60) {
System.out.println("凉凉!!");
} else if (s >= 60 && s < 80) {
System.out.println("及格!");
} else if (s >= 80 && s < 90) {
System.out.println("良好!");
} else if (s >= 90 && s <= 100) {
System.out.println("优秀!");
}else{
System.out.println("你的分数不正确!");
} } 

Selectステートメント_switchステートメント:

1)。switchステートメントには、「判断」に使用されるifステートメントと同じ機能があります。ただし、複数の値の同等性を判断する場合は、switchステートメント構造を使用する方が明確です。
2)。例:ユーザーが1-7の値を入力すると、プログラムは判断して印刷する必要があります:月曜日、火曜日...

public class Demo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入1--7的星期值:");
int w = sc.nextInt();
//使用switch语句--switch(byte,short,int,char,String,枚举 变量/表达式)
switch (w){
case 1://case 后面必须是"常量",不能是”变量"/"表达式" System.out.println("星期一");
break;//表示:跳出switch语句
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4 :
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default://相当于:else{...}
System.out.println("你的输入有误!");
break; } } }

注:
複数のケースの値を同じにすることはできません。
それぞれの場合の区切りは、switchステートメントからジャンプする必要がないことを意味します。ただし、実行中にケース内のコードが実行されると、breakに遭遇するか、switchステートメントが終了するまで、次のケースのコードは無条件に実行され続けます。
**

ループステートメント_forステートメント:

**
「サイクル」:1つのことを繰り返します。
例:プログラムはHelloWorldを10回印刷したい

public static void main(String[] args) {
//打印10次HelloWorld
for(int i = 0 ; i < 10 ; i++){
System.out.println("HelloWorld!");
} }

ループステートメント_whileループステートメント:

形式:while(ブール式){
//「ブール式」がtrueの場合、ループ本体を実行します
//実行が完了した後、ループは上がり、「ブール式」を実行し続けます
}

public class Demo08 {
public static void main(String[] args) {
//1.打印10次HelloWorld
/*int i = 0;
while (i < 10) {
System.out.println("HelloWorld");
i++;
}*/

ループステートメント_do_whileループステートメント:

フォーマット:do {
//ループ本体
} while(ブール式);
例:

public class Demo {
public static void main(String[] args) {
//1.打印10次HelloWorld
/* int i = 0;
do{
System.out.println("HelloWorld");
i++;
}while(i < 10);*/

小さな知識変数を挿入する範囲:

変数のスコープは、変数を定義する中括弧内にあります。中括弧が指定されていない場合、変数にアクセスできません。

public static void main(String[] args){
int a = 10;
if(...){
int b = 20;
System.out.println(a);//OK的
System.out.println(b);//OK的if(...){
int c = 30;
System.out.println(c);//OK的
System.out.println(a);//OK的
System.out.println(b);//OK的
}
System.out.println(c);//错误
}
System.out.println(a);//OK的
System.out.println(b);//错误
}

ループステートメント_無限ループ:

public static void main(String[] args) {
/*for(;; ) {
System.out.println("呵呵");
}*/
/*while (true) {
System.out.println("嘻嘻");
}
*/
do{
System.out.println("嘻嘻");
}while(true);
}

ループステートメント_ネストされたループ:

1)。ネストされたループ:別のループを含むループを指します。
例:

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

ループステートメント_continueおよびbreakステートメント:

1).continue:このサイクルを終了し、次のサイクルに進みます。

//1.打印1--100所有的偶数
for(int i= 1 ;i <= 100 ;i++){
if(i % 2 == 0){
System.out.println(i);
} }

for(int i = 1 ;i <= 100; i++){
if(i % 2 != 0){
continue;//结束本次循环,继续下一次循环
}
System.out.println(i);
}

2).break:ループステートメントからジャンプします(ループを終了します)。

//1.从1开始,找出10个能同时被2,3,5整除的数
int i = 1;//存储数
int c = 0;//存储找到的个数
while(true){
if(i % 2 == 0 && i % 3 == 0 && i % 5 == 0){
c++;
System.out.println(i);
}
i++;
if(c == 10){
break;//跳出while循环
} }
オリジナルの記事を8件公開 Likes0 Visits 38

おすすめ

転載: blog.csdn.net/FearSun/article/details/105381750