zoukankan      html  css  js  c++  java
  • 20.java中的while循环

    需求:需要打印一行字符串"hello gzitcast",100次

    就需要将该语句打印100遍System.out.println("hello gzitcast");

    那么如何解决该问题?

       Java提供个一个称之为循环的结构,用来控制一个操作的重复执行。

    int count = 0;
    while (count < 100) {
                System.out.println("hello gzitcast");
                count++;
    }
    System.out.println("over");

    变量count初始化值为0,循环检查count<100 是否为true,如果为true执行循环体(while后{}之间的语句),输出"hello gzitcast"语句,然后count自增一,重复循环,直到count是100时,也就是count<100为false时,循环停止。执行循环之后的下一条语句。

       Java提供了三种类型的循环语句:while循环,do-while循环和for循环。

    1、while语句格式:
    while(条件表达式)
    {
        执行语句;
    }

    定义需求: 想要打印5次helloworld

    public static void main(String[] args) {
            System.out.println("hello world");
            System.out.println("hello world");
            System.out.println("hello world");
            System.out.println("hello world");
            System.out.println("hello world");
    }

    2、

    public static void main(String[] args) {
            int x = 0;
            while (x < 5) {
                System.out.println("hello java ");
            }
    }

    如果是在dos里编译和运行,是不会停止,除非系统死机。需要ctrl+c来结束

    这就是真循环或者死循环。因为x<5 永远为真。

    public static void main(String[] args) {
            int x = 0;
            while (x < 5) {
                System.out.println("hello java ");
                x++;
            }
        }

    让x自增,那么就会有不满足条件的时候。循环就会结束。

    练习:想要打印出1-100之间的奇数

    public static void main(String[] args) {
            int x = 1;
            while (x < 100) {
                System.out.println(x);
                x = x + 2;
            }
        }
    public static void main(String[] args){
            int x=1;
            while(x<100){
                
                if(x%2!=0){
                    System.out.print(x);
                }
                x++;
            }
            System.out.println();        
        }

    练习2:计算1+2+3+4+5+6+7+8+9 的值

    int sum = 0;
    int i = 1;
    while (i < 10) {
        sum = sum + i;
        i++;
    }
    system.out.println(sum);

    注意:要精确控制循环的次数。常犯错误是是循环多执行一次或者少执行一次。

    例如会执行101次,想要执行100次,要么是count初始值为1,然后count<=100

    要么是count初始值为0,coung<100

    int count = 0;
            while (count <=100) {
                System.out.println("hello gzitcast");
                count++;
            }
            System.out.println("over");

    猜数字游戏:

    编写程序随即生成一个0-100之间的随机数。程序提示用户输入一个数字,不停猜测,直到猜对为止。最后输出猜测的数字,和猜测的次数。并且如果没有猜中要提示用户输入的值是大了还是小了。

    思考:

    如何生成1-100之间随机数?

    (int)(Math.random()*100)+1;

    如何提示用户输入数字,

    Scanner  sc=new Scanner(System.in);

    int guessNum = sc.nextInt();

    需要将随机数和用户输入的数字进行比较。

     猜一次

    Scanner sc = new Scanner(System.in);
    int num = (int)(Math.random()*100)+1;
            System.out.println("请输入0-100之间整数");
            int guessNum = sc.nextInt();
            if (guessNum == num) {
                System.out.println("中啦");
            } else if (guessNum < num) {
                System.out.println("小啦");
            } else {
                System.out.println("大了");
            }

    这个程序只能才一次,如何让用户重复输入直到猜对?

    可以使用while循环

    public static void main(String[] args) {
            int num = (int)(Math.random()*100)+1;
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请输入1-100之间整数");
                int guessNum = sc.nextInt();
                if (guessNum == num) {
                    System.out.println("中啦");
                } else if (guessNum < num) {
                    System.out.println("小啦");
                } else {
                    System.out.println("大了");
                }
            }
        }

    该方案发现了问题,虽然实现了让用户不停的输入,但是即使猜中了程序也不会停止。

    那么就需要控制循环次数了。也就是while() 括号中的条件表达式。当用户猜测的数和系统生成的数字不相等时,就需要继续循环。

    int num = (int)(Math.random()*100)+1;
            Scanner sc = new Scanner(System.in);
            
            int guessNum = -1;
            while (guessNum != num) {
    System.out.println("请输入1-100之间整数");
                guessNum = sc.nextInt();
                if (guessNum == num) {
                    System.out.println("中啦");
                } else if (guessNum < num) {
                    System.out.println("小啦");
                } else {
                    System.out.println("大了");
                }
            }

    为什么将guessNum初始化值为-1?因为如果初始化为0到100之间程序会出错,因为可能是要猜的数。

    1:首先程序生成了一个随机数

    2:用户输入一个数字

    3:循环检查用户数字和随机数是否相同,知道相同位置,循环结束

     do while 语句

      

    do while语句格式:

    do
    {
        执行语句;
    }while(条件表达式);
    do while特点是条件无论是否满足,
    循环体至少被执行一次。
    public static void main(String[] args) {
            int x = 0, y = 0;
            do {
                System.out.println(x);
                x++;
            } while (x < 0);
            // do while do会先执行一次,不管是否满足循环条件。
            while (y < 0) {
                System.out.println(y);
                y++;
            }
        }

    while:先判断条件,只有条件满足才执行循环体。   

    do while: 先执行循环体,再判断条件,条件满足,再继续执行循环体。

    简单一句话:do while:无论条件是否满足,循环体至少执行一次。

    注意一个细节do  while 后面的分号;

    案例:改写猜数字游戏

    public static void main(String[] args) {
            // 记录用户输入的数字
            int guess = -1;
            // 记录用户输入次数
            int count = 0;
            // 生成1-100之间随机数
            int num = (int) (int)(Math.random()*100)+1;
    Scanner sc = new Scanner(System.in);
    
            // 循环猜数字
            do {
                System.out.println("请输入1-100之间的数字");
                guess = sc.nextInt();
                if (guess > num) {
    
                    System.out.println("哥们,太大了");
                } else if (guess < num) {
    
                    System.out.println("哥们,太小了");
                } else {
    
                    System.out.println("恭喜,中啦");
                }
                count++;
    
            } while (num != guess);
            System.out.println("你猜测的数字是:" + num + "猜测了" + count + "次");
        }

    案例:计算器

         系统自动生成2个随机数用于参与运算。

         系统生成0-4之间的随机数,表示加减乘除取模运算。

         使用switch 进行匹配

    class Couter {
        public static void main(String[] args) throws InterruptedException {
            // 生成随机数Math.random()生成0-1值,不包含0和1,
             //乘以10得到0和10之间的数(double类型),不包含0和10
             //强转为int,并加1得到1和10之间的数,包含1和10
             int x = (int)(Math.random()*10)+1;        
    int y = (int)(Math.random()*10)+1;            
    System.out.println(x);
            System.out.println(y);
            // 创建0-4随机数 0 1 2 3 4 各表示加减乘除取模
            int z = (int) (int)(Math.random()*5);
            System.out.println(z);
    
            switch (z) {
            case 0:
                System.out.println(x + "+" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "+" + y + "=" + (x + y));
                break;
            case 1:
                System.out.println(x + "-" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "-" + y + "=" + (x - y));
                break;
            case 2:
                System.out.println(x + "*" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "*" + y + "=" + (x * y));
                break;
            case 3:
                System.out.println(x + "/" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "/" + y + "=" + (x / y));
                break;
            case 4:
                System.out.println(x + "%" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "%" + y + "=" + (x % y));
                break;
            }

        }

    }

    计算器2:上述中只能计算一次。可以使用whileu循环来不停计算。

    程序生成了3个随机数,前两个数参与运算,第三个数用于匹配运算符。要注意除数为0的情况。

    int x = (int)(Math.random()*10)+1;    

    Math.random() 生成0-1之间的数字,double类型

    Math.random()*10 就是0-9之间的数,是double类型

    (int)(Math.random()*10)将double类型强转成int类型,去掉小数点,便于计算。

    (int)(Math.random()*10)+1,生成了1到10之间随机数。

    int z = (int) (int)(Math.random()*5);

    生成0-4之间的数字,可以用0表示加,1表示减,2表示乘,3表示除,4表示取模

    为了减慢程序,使用了Thread.sleep(2000); 让程序等待一会。

    author@nohert
  • 相关阅读:
    iOS-Core Text 入门
    Mac浏览器全屏设置
    Quartz 2D - 图形上下文(Graphics Contexts)
    Quartz 2D 概述
    Quartz 2D官方文档翻译(持续更新中)
    那些年,我们常掉进去的坑
    CGAffineTransformMake(a,b,c,d,tx,ty) 矩阵运算的原理 (转载)
    科大讯飞
    Object-C语言类的扩展
    科大讯飞语音识别
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13574987.html
Copyright © 2011-2022 走看看