zoukankan      html  css  js  c++  java
  • while、do while练习——7月24日

    while循环的格式是for循环的变形

    //while 循环(当循环),是for循环的变形
                //for(int i=0;i<=5;i++)
                //{
                //    Console.WriteLine("hello!");
                //}
                //Console.ReadLine();
    
                //还可以写成
                //int i = 0;
                //for (; i <= 5; )
                //{
                //    Console.WriteLine("hello!");
                //    i++;
                //}
                //Console.ReadLine();
    
                //while  格式
                //int i = 0;
                //while (i <= 5)
                //{
                //    Console.WriteLine("hello!");
                //    i++;
                //}
                //Console.ReadLine();
    
    
                //do  while 格式  
                int i = 0;
                do//不管后面的判断对不对,先执行了再说
                {
                    Console.WriteLine("hello!");
                } while (i < 5);
                Console.ReadLine();

    练习一:用while循环计算折纸次数

    //一张纸厚度为0.07毫米,问对折多少次至少超过8848米
                //8848米=8848000毫米
                //int sum = 0;
                //double h = 0.07;
                //while (h < 8848000)
                //{
                //    h *= 2;
                //    sum++;
                //}
                //Console.WriteLine("至少需要折叠"+sum+"次。");
                //Console.ReadLine();
    
                //方法2:
                int sum = 0;
                double h= 0.07;
                while (1 == 1)//死循环
                {                                
                    h *= 2;
                    sum++;
                    Console.WriteLine("" + sum + "次的高度是:" + h / 1000 + "米。");
                    if (h >= 8848000)
                    {
                        break;//结束整个循环,并跳出循环;continue是结束本次循环,继续下次循环
                    }               
                }
                Console.WriteLine("至少需要折叠" + sum + "次。");
                Console.ReadLine();

    练习二:用while打印九九口诀表

    //用while循环打印99口诀表
                int i = 1;
                while (i <= 9)//打印行
                {               
                    int j = 1;
                    while (j <= i)//打印列
                    {                    
                        Console.Write(j+"*"+i+"="+j*i+"	");
                        j++;                   
                    }                
                    i++;
                    Console.WriteLine();
                }          
                Console.ReadLine();
  • 相关阅读:
    【JavaScript】出现即使设置了ID也获取不到的可能原因与window.onload
    雄踞AppStore榜首的游戏&lt;别踩到白块儿&gt;源码分析和下载(一)
    WordPress公布新文章Email通知注冊用户
    BZOJ 1861 ZJOI2006 Book 书架 Splay
    Oracle Outline总结
    Matplot中文乱码完美解决方式
    Linux 在一个命令行上执行多个命令
    tophat
    echo输出到stderr
    随机森林
  • 原文地址:https://www.cnblogs.com/juyangchao12/p/5701940.html
Copyright © 2011-2022 走看看