java05 流程控制语句练习

一.无限循环

无限循环下面的代码永远执行不到

1.for 语句

 for(;;){
            System.out.println("学习");
        }

2.while语句

  while(true) {
            System.out.println("");
        }

3.do ...while语句

 do {
            System.out.println("0");
        }do(true);

二.跳转控制语句 (和C语言一样)

continue:跳过本次循环,继续执行下次循环

break:结束本次循环

三.练习

1.逢7过

public class Test5 {
    public static  void main(String[] args){
        for (int i = 1 ;i <= 100; i++) {
            //个位7 十位7 7倍数
            if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0){
                System.out.println("过");
            }
            else {
                System.out.println(i);
            }
        }
    }
}

2.输入一个数,返回它的平方根(只保留整数)

import java.util.Scanner;

public class Test5 {
    public static  void main(String[] args){
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入要求平方根的数:");
        int a = sc.nextInt();
        for (int i = 1;i <= a;i++){
            if (i * i < a){
                continue;
            }
            else if (i * i == a){
                System.out.println("这个数的平方根是"+i);
                //一旦找到了就停止循环!!!!
                break ;
            }
            else {
                System.out.println(i-1);
                System.out.println("这个数的平方根是"+(i-1));
                //一旦找到了就停止循环!!!!
                break ;
            }
        }
    }
}

3.判断是否为质数

import java.util.Scanner;

public class Test5 {
    public static  void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int a = sc.nextInt();

        for(int i = 2;i <= a-1;i++){
            if(a % i == 0){
                System.out.println("不是");
                break;
            }
            else{
                System.out.println("是");
            }
        }
//优化
        //for(int i = 2;i <= 该数字的平法根;i++){
       
        //    }
    }

}

4.猜数字

随机数的知识:

        a.导包 import java.util.Random

        b.创建对象 Random r = new Random();

        c.生成随机数  int a = r.nextInt(随机数的范围) 这个范围包含了左边不包含右边,且一定是从0开始

生成任意数之间的随机数:

        a.让这个范围的头尾都减去一个值,使其从0开始

        b.将修改之后的范围尾巴得到x

        c.最终的结果x再整体加上第一步减去的值

 //1-100
        Random r = new Random();
        int a = r.nextInt(100)+1;
                //0-99 ->100 100+1  
import java.util.Random;
import java.util.Scanner;

public class Test5 {
    public static  void main(String[] args){

        Random r = new Random();
        int a = r.nextInt(100)+1;
        System.out.println(a);

        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("输入猜的数字:");
            int guess = sc.nextInt();
            if (guess > a){
                System.out.println("大了");
            }
            else if (guess < a){
                System.out.println("小了");
            }
            else{
                System.out.println("对了");
                break;
                //直到猜对为止,就结束循环
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值