循环结构之WHILE语句
2012-04-24 17:33 精诚所至 金石为开 阅读(233) 评论(0) 编辑 收藏 举报WHILE语句类似于DO语句,它在代码执行前对布尔值进行判断,若为FALSE,直接跳过WHILE循环。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; namespace a { class Program { public static void Main( string [] args) { double balance,interestRate,targetBalance; Console.WriteLine( "What is your current balance?" ); balance=Convert.ToDouble(Console.ReadLine()); Console.WriteLine( "What is your current annual interest rate (in %)?" ); interestRate= 1+Convert.ToDouble(Console.ReadLine())/100.0; Console.WriteLine( "What balance would you like to have?" ); targetBalance=Convert.ToDouble(Console.ReadLine()); int totalYears=0; while (balance <targetBalance) { balance*=interestRate; ++totalYears; } while (balance<targetBalance); Console.WriteLine( "In {0} year {1} you'll have a balance of {2}." ,totalYears,totalYears==1? "" : "s" ,balance); Console.ReadKey(); } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步