Java基础-控制流程-2. 循环

本文详细介绍了while循环和do/while循环的基本概念、语法及用法,通过两个实例展示了如何在Java中实现不同场景下的循环逻辑。重点阐述了两种循环语句在条件设置和执行流程上的差异,帮助读者掌握循环控制结构的灵活运用。

while循环当条件为true时执行一条语句(也可以是一个语句块)。通常格式为 while(condition) statement

如果开始循环条件就为false,则循环体一次也不执行。

 

while循环语句首先检测循环条件。因此,循环体中的代码有坑不被执行。如果希望循环体至少执行一次,就应该将检测条件放置在最后。使用do/while循环语句。语法格式为 do statement while (condition);

这种循环语句先执行语句(通常是一个语句块),在检测循环条件;然后重复语句,在检测循环条件,以此类推。下面附上两个例子,大家自己运行一下看看:

    例子一:

 

Java代码 
  1. import java.util.*;  
  2.   
  3. public class Retirement {  
  4.     public static void main(String[] args) {  
  5.         // read inputs  
  6.         Scanner in=new Scnner(System.in);  
  7.           
  8.         System.out.print("How much money do you need to retire?");  
  9.         double goal=in.nextDouble();  
  10.           
  11.         System.out.print("How much money will you contribute every year?");  
  12.         double payment=in.nextDouble();  
  13.           
  14.         System.out.print("Interest rate in %: ");  
  15.         double interestRate=in.nextDouble();  
  16.           
  17.         double balance=0;  
  18.         int years=0;  
  19.           
  20.         // update account balance while goal isn't reached  
  21.         while(balance < goal) {  
  22.             // add this year's payment and interest  
  23.             balance +=payment;  
  24.             double interest=balance*interestRate/100;  
  25.             balance +=interest;  
  26.             years++;  
  27.         }  
  28.           
  29.         System.out.println("You can retire in "+years+" years.");  
  30.     }  
  31. }  

    例子二:

 

Java代码 
  1. import java.util.*;  
  2.   
  3. public class Retire {  
  4.     public static void main(String[] args) {  
  5.         Scanner in=new Scanner(System.in);  
  6.           
  7.         System.out.print("How much money will you contribute every year?");  
  8.         double payment=in.nextDouble();  
  9.           
  10.         System.out.print("Interest rate in %: ");  
  11.         double interestRate=in.nextDouble();  
  12.           
  13.         double balance=0;  
  14.         int year=0;  
  15.           
  16.         String input;  
  17.           
  18.         // update account balance while user isn't ready to retire  
  19.         do{  
  20.             // add this year's payment and interest  
  21.             balance +=payment;  
  22.             double interest=balance*interestRate/100;  
  23.             balance +=interest;  
  24.               
  25.             year++;  
  26.               
  27.             // print current balance  
  28.             System.out.printf("After year %d, your balance is %, .2f%n", year, balance);  
  29.               
  30.             // ask if ready to retire and get input  
  31.             System.out.print("Ready to retire? (Y/N) ");  
  32.             input=in.next();  
  33.         }  
  34.         while(input.equals("N"));  
  35.     }  
  36. }  

转载于:https://my.oschina.net/javacy/blog/77665

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值