While、do-while练习

1. 使用while循环录入班级人数和学员成绩,计算班级学员的平均成绩。
在这里插入图片描述


import java.util.Scanner;

class  while1{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入班级号:");
		String class1 = input.next();
	    System.out.print("请输入该班级的学员总数:");
		int n = input.nextInt();
		int j=1;
		int sum=0;
		double avg=0;
		int i = n;
		while(i>0){
			System.out.print("请输入学号"+j+"的成绩:");
			int score = input.nextInt();
			sum=sum+score;
			j++;
			i--;
		}
		avg = sum/n;
		System.out.println("该班级的平均成绩为:"+avg);
	}
}

2. 第2题
2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?


import java.util.Scanner;

class  while2{
	public static void main(String[] args) {
		int year = 2006;
		double n = 80000;
		while(n<200000){
			n=n+n*0.25;
			year++;
		}
		System.out.println("在"+year+"年,学员达到20万人");
	}
}

3. 使用while循环实现以下功能:
a)将1~20的数字进行累加,累加过程中当某数能被5或被3整除时不能累加,
当累加值大于100时结束累加。
b)输出所有能被累加的数字,如输出:1,2,4,7,8,11,13,14,16,17,19,
c)最后输出累加的结果,如输出:sum=112
提示:灵活使用continue,break


import java.util.Scanner;
class  while3{
	public static void main(String[] args) {
		int n = 0;
		int sum = 0;
		while(n<=20){
			if(sum>100){
				break;
			}
			n++;
			if(n%5==0 || n%3==0) 
				continue;
			sum =sum +n;
			System.out.print(n+" ");
		}
		System.out.println();
		System.out.println("sum = "+sum);
	}
}

4. 使用do-while实现:连续录入学生姓名,输入“q”则系统退出。

注意:变量str == ”q” 行不行?为什么?


import java.util.Scanner;

class  DoWhile4{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String name = "";
		do{
			System.out.print("请输入学生姓名:");
		    name = input.next();
		}
		while(!"q".equals(name));
		System.out.println("程序结束");
	}
}

5. 使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系: 华氏温度 = 摄氏温度 * 9 / 5.0 + 32
? = 0
9 / 5.0 + 32
? = 20
9 / 5.0 + 32**

提示:
1、循环操作:计算摄氏温度,并输出对照条目
2、循环条件:条目<=10 && 摄氏温度 <= 250


class  DoWhile5{
	public static void main(String[] args) {
		int n = 1;
		int c = 0;
		double hua = 0;
		System.out.println("摄氏度"+"\t"+"华摄氏度");
		do{
			hua = c*9/5.0+32;
			System.out.println(c+"\t"+hua);
			n++;
			c=c+20;
		}while(n <= 10 && c<=250);
		System.out.println("程序结束");
	}
}

class  While6{
	public static void main(String[] args) {
		int i = 1;
		int j = 1;
		int result = 1;
		while(i <= 9){
			while(j<=9){
				result = i * j;
				System.out.print(i+"*"+j+"="+result+"\t");
				j++;
			}

			i++;
			j=i;
			
			System.out.println();
		}
	}
}

6. 分别使用while和do-while实现打印99乘法表。


class  DoWhile6{
	public static void main(String[] args) {
		int i = 1;
		int j = 1;
		int result = 1;
		do{
				do{
					result = i * j;
					System.out.print(i+"*"+j+"="+result+"\t");
					j++;
				}while(j<=9);

				i++;
			    j=i;
				System.out.println();
		 }while(i <= 9);
	}
}

7. 使用while循环编写一个控制台程序从键盘输入一个4位数的整数并对该整数实现反转输出;如输入:1234,输出4321


import java.util.Scanner;

class  While7{
	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		System.out.print("请输入四位整数:");
		int n = input.nextInt();

		while(n != 0){
			int i= n%10;
			n=n/10;
			System.out.print(i);
		}

		System.out.println();

	}
}

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值