Java基础3 -- 循环语句


小庄zzz:学习循环语句只需要抓住一点——代码执行顺序!

目录

一、Java基础2 -- 课后习题解析:

二、while循环

三、do while循环

四、for循环

五、跳转语句

六、多层循环


一、Java基础2 -- 课后习题解析:

import java.util.Scanner;
public class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble();
        
        //进行降序排列
        if(a < b){
            double t = a;
            a = b;
            b = t;
        }
        if(a < c){
            double t = a;
            a = c;
            c = t;
        }
        if(b < c){
            double t = b;
            b = c;
            c = t;
        }
        
        if(a >= b + c)
            System.out.println("NAO FORMA TRIANGULO");
        else{ //这里没有否则关系 不使用else if
            if(a * a == b * b + c* c)
                System.out.println("TRIANGULO RETANGULO");
            if(a * a > b * b + c* c)
                System.out.println("TRIANGULO OBTUSANGULO");
            if(a * a < b * b + c* c)
                System.out.println("TRIANGULO ACUTANGULO");
            if(a == b &&a == c&&b == c)
                System.out.println("TRIANGULO EQUILATERO");
            else if(a == b || a == c || b == c)
                System.out.println("TRIANGULO ISOSCELES");
        }
    }
}

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(),b = sc.nextInt();
        int c = sc.nextInt(),d = sc.nextInt();
        int x = 60 * a + b,y = 60 * c + d;//全部转换为分钟来计算
        
        int minutes = 0;
        if(x < y) minutes = y - x;
        else minutes = 1440 - x + y;
        
        int h = minutes / 60;
        int m = minutes % 60;
        
        System.out.printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",h,m);
    }
}

二、while循环

可以简单理解为循环版的if语句。if语句是判断一次,如果条件成立,则执行后面的语句;while是每次判断,如果成立,则执行循环体中的语句,否则停止。

public class Main {
    public static void main(String[] args) {
        int i = 0;
        while (a < 10) {
            System.out.println(a);
            a ++ ;
        }
    }
}

练习:求1~100中所有数的立方和

public class Main {
    public static void main(String[] args) {
        int i = 1, sum = 0;
        while (i <= 100) {
            sum += i * i * i;
            i ++ ;
        }
        System.out.println(sum);
    }
}

练习:求斐波那契数列的第n项,f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int a = 1, b = 1, i = 1;
        while (i < n) {
            int c = a + b;
            a = b;
            b = c;
            i ++ ;
        }

        System.out.println(a);
    }
}

 死循环:循环永久执行,无法结束。我们要避免写出死循环。

public class Main {
    public static void main(String[] args) {
        int x = 1;
        while (x == 1)
            System.out.println("!");
    }
}
 


三、do while循环

do while循环不常用。
do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。

public class Main {
    public static void main(String[] args) {
        int x = 1;
        while (x < 1) {
            System.out.println("x!");
        }

        int y = 1;
        do {
            System.out.println("y!");
        } while (y < 1);
    }
}
 


四、for循环

基本思想:把控制循环次数的变量从循环体中剥离。

for (init-statement ; conditionexpression) {
    statement
}

init-statement可以是声明语句、表达式、空语句,一般用来初始化循环变量;
condition是条件表达式,和while中的条件表达式作用一样;可以为空,空语句表示true;
expression一般负责修改循环变量,可以为空。

练习:求1~100中所有数的立方和

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i ++ )
            sum += i * i * i;
        System.out.println(sum);
    }
}

练习:求斐波那契数列的第n项。f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2) 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int a = 1, b = 1;
        for (int i = 1; i < n; i ++ ) {
            int c = a + b;
            a = b;
            b = c;
        }

        System.out.println(a);
    }

 init-statement可以定义多个变量,expression也可以修改多个变量。

例如求 1 * 10 + 2 * 9 + 3 * 8 + 4 * 7 + 5 * 6:

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1, j = 10; i < j; i ++, j -- ) {
            sum += i * j;
        }

        System.out.println(sum);
    }
}


五、跳转语句

     1. break

     可以提前从循环中退出,一般与if语句搭配。
     例题:判断一个大于1的数是否是质数:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        boolean isPrime = true;
        for (int i = 2; i < n; i ++ )
            if (n % i == 0) {
                isPrime = false;
                break;
            }

        if (isPrime)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}

     2. continue

     可以直接跳到当前循环体的结尾。作用与if语句类似。
     例题:求1~100中所有偶数的和

public class Main {
    public static void main(String[] args) {
        int sum = 0;

        for (int i = 1; i <= 100; i ++ ) {
            if (i % 2 == 1) continue;
            sum += i;
        }

        System.out.println(sum);
    }
}


六、多层循环

将1~100打印到一个10 * 10的矩阵中:

public class Main {
    public static void main(String[] args) {
        for (int i = 0, k = 1; i < 10; i ++ ) {
            for (int j = 0; j < 10; j ++, k ++ ) {
                System.out.printf("%4d ", k);
            }
            System.out.println();
        }
    }
}

练习:打印1~100中的所有质数

public class Main {
    public static void main(String[] args) {
        for (int i = 2; i <= 100; i ++ ) {
            boolean isPrime = true;
            for (int j = 2; j < i; j ++ ) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
                System.out.println(i);
        }
    }
}

课后作业 

一、

医学部一共进行了 N 场动物实验。

共有三种小动物可用来实验,分别是青蛙、老鼠和兔子。

每次实验都会选取其中一种动物来参与实验,选取数量若干。

现在请你统计一下医学部一共用了多少小动物,每种分别用了多少,每种动物使用数量占总量的百分比分别是多少。

输入格式

第一行包含整数 N,表示实验次数。

接下来 N 行,每行包含一个整数 A(表示一次实验使用的小动物的数量)和一个字符 T(表示一次实验使用的小动物的类型,C 表示兔子(coney),R 表示老鼠(rat),F 表示青蛙(frog))。

输出格式

请你参照输出样例,输出所用动物总数,每种动物的数量,以及每种动物所占百分比。

注意输出百分比时,保留两位小数。

数据范围

1 ≤ N ≤ 100,
1 ≤ A ≤ 15

输入样例:

10

10 C

6 R

15 F

5 C

14 R

9 C

6 R

8 F

5 C

14 R

 输出样例:

Total : 92 animals

Total coneys : 29

Total rats : 40

Total frogs : 23

Persentage of coneys : 31.52 %

Persentage of rats : 43.48 %

Persentage of frogs: 25.00 %

二、 

输入 N 对整数对 X,Y,对于每对 X,Y,请你求出它们之间(不包括 X 和 Y)的所有奇数的和。

输入格式

第一行输入整数 N,表示共有 对测试数据。

接下来 N 行,每行输入一组整数  Y

输出格式

每对 X,Y,输出一个占一行的奇数和。

数据范围

1 ≤ N ≤ 100,
−1000 ≤ X,Y ≤ 1000

输入样例:

7

4 5

13 10

6 4

3 3

3 5

3 4

3 8

输出样例: 

0

11

5

0

0

0

12

三、 

一个整数,除了本身以外的其他所有约数的和如果等于该数,那么我们就称这个整数为完全数。

例如,就是一个完全数,因为它的除了本身以外的其他约数的和为 1+2+3=6。

现在,给定你 N 个整数,请你依次判断这些数是否是完全数。

输入格式

第一行包含整数 N,表示共有 N 个测试用例。

接下来 N 行,每行包含一个需要你进行判断的整数 X

输出格式

每个测试用例输出一个结果,每个结果占一行。

如果测试数据是完全数,则输出 X is perfect,其中 是测试数据。

如果测试数据不是完全数,则输出 X is not perfect,其中 X 是测试数据。

数据范围

1 ≤ N ≤ 100,
1 ≤ X ≤ 1.0*e8

输入样例:

3

6

5

28

输出样例:

6 is perfect

5 is not perfect

28 is perfect

 课后作业代码解析见下一节

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小庄zzz_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值