JAVA 循环语句作业

来源:互联网 发布:无人机与人工智能 编辑:程序博客网 时间:2024/06/04 19:41

题目:某机构2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?


while循环语句:


/*
 * 某机构2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
 */
package com.task01;
public class JTZY {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int year = 2006;//年数2006年
  int student = 80000;//2006年的学员人数
  while (student<200000) {//判断条件
   student = (int)(student*(1+0.25));//每年增长的人数计算公式
   year++;//年份
  }
  System.out.println(year+"年,培训人数达到20万");//输出结果
 }
}


do...while循环语句:


/*
 * 某机构2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
 */
package com.task01;
public class JTTZ {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int year = 2006;//年数2006年
  int student = 80000;//2006年的学员人数
  do{
   student = (int)(student*(1+0.25));//每年增长的人数计算公式
   year++;//年份
  }
   while (student<200000);//判断条件
   System.out.println(year+"年,培训人数达到20万");//输出结果
  }
 }


for循环语句:


/*
 * 某机构2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
 */
package com.task01;
public class JZZY {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int year = 2006;//年数2006年
  int student = 80000;//2006年的学员人数
  for(student = 80000;student<=200000;year++)//判断条件,年份
  {
   student = (int)(student*(1+0.25));//每年增长的人数计算公式
  }
  System.out.println(year+"年,培训人数达到20万");//输出结果
 }
}


原创粉丝点击