栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA循环练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA循环练习

long float double boolean 不能做switch的参数
循环结构:while for do--while
循环重复的做一件事情
while(boolean布尔表达式){语句块}
如果布尔表达式为真,那就执行语句块

break跳出整个循环
continue跳出本次循环1

程序执行到断点处并没有往下执行,
自动根据当前代码的情况,显示当前变量的值
点击第一个调试按钮往下一行进行调试,逐过程进行调试
点击第二个按钮进入语句/函数

调试窗口界面的四个值:
步过,一行一行地往下走,如果这一行上有方法不会进入方法。
步入,如果当前行有方法,可以进入方法内部,一般用于进入自定义方法内,不会进入官方类库的方法,
强制步入,能进入任何方法,查看底层源码的时候可以用这个进入官方类库的方法
步出,从步入的方法内退出到方法调用处,此时方法已执行完毕,只是还没有完成赋值。


代码总结
public class TestDemo01 {
    public static void main(String[] args) {
        Random random = new Random();    //此处可以给上一个种子,加了参数,但是每次运行都会生成的数字都是唯一的,生成的是一个伪随机数
        int randNum = random.nextInt(100) + 1;       //生成[0-100)之间的随机数
        Scanner scan = new Scanner(System.in);
        while(true){
            System.out.println("请输入你要猜的数字");
            int num = scan.nextInt();
            if(num < randNum){
                System.out.println("低了");
            }else if(num == randNum){
                System.out.println("找到了");
                break;
            }else{
                System.out.println("高了");
            }
        }
    }
    //ctrl + d会终止数字的输入输出
    //如果是在OJ上,后台会帮你结束这个循环的
    public static void main28(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()){
            int n = scan.nextInt();
            System.out.println(n);
        }

    }
    public static void main27(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入浮点数");
        System.out.println("请输入字符串");
        String str1 = scan.next();    //输入数据 如果是遇到了空格键,就会停止输入
        String str2 = scan.nextLine();//遇到空格会继续进行输入
        System.out.println(str1);
    }
    public static void main26(String[] args) {
        //此处先输入数字,控制台不会让你进行输入字符串的,系统问题
        //但是先输入字符串之后可以输入数字
        //导包操作, Java.util.Scanner
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数字");
        int i = scan.nextInt();
        System.out.println("请输入字符串");
        String str = scan.nextLine();
        System.out.println(str);
        System.out.println(i);
    }
    //输入,这种写法很少用
    public static void main25(String[] args) throws IOException {
        System.out.println("从键盘输入一个字符");
        //只能从键盘上输入一个数字,真正意义上不会去使用他的
        char i = (char)System.in.read();        //alt+enter自动添加代码,引入相应的头文件
        System.out.println("你的输入是"+i);

    }

    //输出格式
    public static void main24(String[] args) {
        System.out.println("换行");
        System.out.print("不换行");
        System.out.printf("%dn",4);
    }
    //输出1-10之间的数据 do-while实现
    public static void main23(String[] args) {
        int i = 1;
        do{
            //至少会被执行一次,没有把握的时候不要去挑战他
            System.out.println(i);
            i++;
        }while(i<=10);
    }

    //for循环求i的阶乘的和
    public static void main22(String[] args) {
        int sum = 0;
        for (int j = 1; j <= 5; j++) {
            int ret = 1;
            for (int i = 1; i <= j; i++) {
                ret  = ret*i;
            }
            sum += ret;
        }
        System.out.println(sum);
    }

    //for循环求i的阶乘,固定数字
    public static void main21(String[] args) {
        int ret = 1;
        for (int i = 1; i <= 5; i++) {
            ret  = ret*i;
        }
        System.out.println(ret);
    }

    //for语句练习,输出1-10之间的数字和
    public static void main20(String[] args) {
        int sum = 0;
        for(int i = 1;i <= 10;i++){
            sum += i;
        }
        System.out.println(sum);
    }


    //找到1-100之间既能被3整除又能被5整除的数
    public static void main19(String[] args) {
        int i = 1;
        while(i <= 100){
            if(i % 15 != 0){
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }

    }
    //1-20之间3的倍数
    public static void main18(String[] args) {
        int  i = 1;
        while(i <= 20){
            if(i % 3 != 0){
                i++;
                continue;    //跳过本次循环,结束本躺循环
            }
            System.out.println(i);
            i++;
        }
    }
    //找出1-10之间的第一个3的倍数
    public static void main17(String[] args) {
        int i = 1;
        while(i <= 10){
            if(i % 10 == 3){
                System.out.println(i);
                break;      //跳出整个循环
            }
            i++;
        }
    }


    public static void main16(String[] args) {
        System.out.println("hee");
    }
 
 
    //输出5的阶乘的和
    public static void main14(String[] args) {
        int j = 1;
        int n = 5;
        int sum = 0;
        while(j <= n){
            //求数字n的阶乘
            int i = 1;
            int ret = 1;
            while(i <= j){
                ret *=  i;
                i++;
            }
            sum += ret;
            j++;
        }
        System.out.println(sum);
    }
   

    //1-100 奇数的和 偶数的和
    public static void main12(String[] args) {
        int i = 2;
        int sumOdd = 0;
        int sumEvn = 0;
        while(i <= 100){
            sumOdd = sumOdd + i;
            i = i + 2;
        }
         i = 1;
        System.out.println(sumOdd);
        while(i <= 100){
            sumEvn = sumEvn + i;
            i = i + 2 ;
        }
        System.out.println(sumEvn);
    }
    //计算1-100之间的奇数和 偶数和
    public static void main11(String[] args) {
        int i = 1;
        int sum1 = 0;
        int sum2 = 0;
        while(i <= 100){
            if(i % 2 != 0){
                sum1++;
            }else{
                sum2++;
            }
            i++;
        }
        System.out.println("奇数和为"+sum1+"偶数和为"+sum2);
    }
    //输入出1-100之间的数据和
    public static void main10(String[] args) {
        int i = 1;
        int sum = 0;
        while(i <= 100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
    //输出1-10之间的数
    public static void main9(String[] args) {
            //注意避免死循环的产生,判断条件永远为真了
           
            int i =1;       //循环的初始条件
            while(i <= 10){ //循环的判断条件
                System.out.println(i);
                i++;        //循环的迭代条件
            }
    }
    public static void main8(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输出整数");
        int n = scan.nextInt();
        System.out.println(n);
        //系统不会让你输入字符串的,因为java控制台的问题
        //如果是先输出字符串再输出数字可以进行输出
        System.out.println("请输入字符串");
        String str = scan.nextLine();
        System.out.println(str);
    }
    //1----100之间的,既能除3有能除5的数
    public static void main7(String[] args) {
        //1----100之间的,既能除3有能除5的数
        for(int i = 1;i <= 100;i++){
            if(i % 15 != 0){
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
    //输入想要的字符串,但是遇到了空格就不会继续输入
    public static void main6(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        System.out.println(str);
    }
    //输入想要的字符串
    public static void main5(String[] args) {
        Scanner scan  = new Scanner(System.in);
        while(scan.hasNextLine()){
            String str = scan.nextLine();
            System.out.println(str);
        }

    }
    //不断输入想要的数据元素,输入Ctrl+D结束输入
    public static void main4(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()){
            int n = scan.nextInt();
            System.out.println(n);
        }
    }
    //求数据n的阶乘的阶乘的和
    public static void main3(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int sum = 0;
        for(int j = 1; j <= n ;j++){
            int ret = 1;
            for( int i = 1; i <= j ;i++){
                ret *= i;
            }
            sum += ret;
        }
        System.out.println(sum);
    }
//    public static void main2(String[] args) {
//        int j = 1;
//        int n = 5;
//        int sum = 0;
//        while(j <= n){
//            int i = 1;
//            int ret = 1;
//            while(i <= j){
//                ret *= i;
//                i++;
//            }
//            sum += ret;
//            j++;
//        }
//        System.out.println(sum);
//
//    }
    public static void main1(String[] args) {
        int i = 1;
        int sum1 = 0;
        int sum2 = 0;
        while(i <= 100){
            if(i % 2 != 0){
                sum1 += i;
            }else{
                sum2 += i;
            }
            i++;
        }
        System.out.println("1--100之间的奇数和为"+sum1);
        System.out.println("1--100之间的偶数和为"+sum2);
    }
}


public class TestDemo02 {
    //最大公约数在数学上的算法有很多种,这里讲解一种辗转相除法
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num1 = scan.nextInt();
        int num2 = scan.nextInt();
        int c = num1 % num2;
        while(c != 0){
            num1 = num2;
            num2 = c;
            c = num1 % num2;
        }
        System.out.println(num1 +"与"+num2+"的最小约倍数为"+num2);
    }


    //打印输出9*9乘法口诀表
    public static void main7(String[] args) {
        for (int i = 1; i <= 9 ; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*"+ j +"="+i*j+"  ");
            }
            System.out.println();
        }
    }
    //输出1000到2000之间的闰年
    public static void main6(String[] args) {
        for( int y = 1000;y <= 2000;y++){
            if((y % 400 == 0) || ((y % 4 == 0)&&(y % 100)!=0)){
                System.out.println(y+"是素数");
            }
        }
    }
    //输出2到输入数字之间的整数
    public static void main5(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        if(n == 1){
            System.out.println("1不是素数");
        }
        for (int j = 2; j <= n ; j++) {
            int i = 2;
            for(; i <= Math.sqrt(j);i++){
                if(j % i == 0){
                    //System.out.println("不是素数");
                    break;
                }
            }
            if(i > Math.sqrt(j)){
                System.out.println(j + "是素数");
            }
        }

    }
    //判断一个数是不是素数的优化
    public static void main4(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int i = 2;
        for (; i <= num / 2; i++) {
            if(num % i == 0){
                System.out.println("不是素数");
                break; }
        }
        if(i > num / 2){
            System.out.println("是素数");
        }
    }
    //判断一个数是不是素数的优化
    public static void main3(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int i = 2;
        //这也是一种优化手段for(;i <= n/2; ++)
        for(;i <= Math.sqrt(num);i++){
                if(num % i == 0){
                    System.out.println("不是素数");
                    break;
                }
        }
        if(i > Math.sqrt(num)){
            System.out.println("不是素数");
        }

    }
    //求素数最基本的方法
    public static void main2(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        if(n == 1){
            System.out.println("1既不是素数也不是合数");
        }
        int i = 2;
        for (; i < n; i++) {
            if(n % i == 0){
                System.out.println("不是素数");
                break;
            }
        }
        if(i == n){
            System.out.println("是素数");
        }
    }
    //判断青年老年中年
    public static void main1(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()){
            int age = scan.nextInt();
            if(age < 18){
                System.out.println("少年");
            }else if (age >= 18 && age <= 28){
                System.out.println("青年");
            }else if(age >= 29 && age <= 55){
                System.out.println("中年");
            }else{
                System.out.println("老年");
            }
        }
    }
}


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322563.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号