JAVA初学上机练习(二)-循环语句
**
计算各层次的顾客比例
需求:商场对顾客的年龄层次进行调查
**
代码如下:
//年龄比例计算
Scanner sc=new Scanner(System.in);
int ageup= 0;//存储大于30岁的人数
int agedown=0;//存储小于30岁的人数
for(int i =0;i<10;i++){
System.out.println("please input number "+ i +"age:");
int age= sc.nextInt();
if(age>30){
ageup++;
}else{
agedown++;
}
}
System.out.println("up 30 years:"+(ageup/10.0)*100+"%");
System.out.println("down 30 years:"+(agedown/10.0)*100+"%");
输出结果为: