Java初学——50道练习题(1----10)

java练习题

任何编程语言想要熟练掌握,仅仅依靠理论知识,语法用法是完全不够的,需要去多上手操作,有一些bug你不去运行实例,永远都不会想到这样还会出错!
话不多说,如下:


【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第n个月的兔子对数为多少?
程序分析:这道题,我推荐先在演草纸上将前几个月的兔子数量列出来,便于发现规律,进而根据规律可知兔子数量符合“斐波那契数列”。 兔子的规律为数列1,1,2,3,5,8,13,21

	public static void main(String[] args){
		int basic = 1;//初始对数
		int end = 1;//终止对数
		int middle;//用于斐波那契数列前两数相加中间变量
		int month;//月份
		System.out.println("Please enter the month:");
		Scanner input = new Scanner(System.in);//这里注意要在函数前加入 import java.util.Scanner;引入输入
		month = input.nextInt();
		if(month == 1||month == 2){//如果月份为1或2则兔子对数初始与终止相等,兔子还未生小兔子
			System.out.println("The_count = 1");
		}else{
			for(int i=3;i<=month;i++){//计算兔子总数
				middle = end;
				end = end + basic;
				basic = middle;
			}
			System.out.println("The_count = " + end);
		}
		input.close();//记得关闭输入语句,否则java运行会“黄牌”警告(滑稽脸)
	}

运行结果:

Please enter the month:
6
The_count = 8



【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到这个数,如果能被整除,则表明此数不是素数,反之是素数。

public static void main(String[] args){
		Scanner input = new Scanner(System.in);//要先导入java.util.Scanner类
		int small,big;
		int i,j;
		int count = 0;
		System.out.println("Enter the small count:");
		small = input.nextInt();
		System.out.println("Enter the big count:");
		big = input.nextInt();
		for(i = small; i <= big; i++){
			if(i == 1){//1不是素数,如果i为1,则跳过
				i++;
			}
			if(i == 2){//2是素数
				System.out.print(i + " ");
				count++;
				i++;
			}
			for(j = 2; j < i; j++){//判断i是否为素数,如果能被整除,则提前跳出循环,i < j
				if(i % j == 0){
					break;
				}
			}
			if(j >= i){//j >= i说明在上一个循环中并未有能被i整除的数
				System.out.print(i + " ");
				count++;
				if(count % 5 == 0){
					System.out.print("\n");
				}
			}
		}
		if(count == 0){//count记录素数的个数,如果为0,则无素数
			System.out.println("在这两个数中间无素数.");
		}
		input.close();
	}

运行结果:

Enter the small count:
5
Enter the big count:
20
5 7 11 13 17
19



【程序3】
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:控制一个循环,将所有的三位数挨个分解为 百,十,个位,判断是否属于水仙花数的条件

	public static int Judge_narcissistic(int a){//判断是否为水仙花数
		int i = a;
		int sum = 0;
		int b;
		while(i != 0){//利用while循环控制循环次数
			b = i % 10;
			sum = sum + b*b*b;
			i = i / 10;
		}
		if(sum == a){//判断百十个位的立方相加总和等于这个数
			return 1;
		}else{
			return 0;
		}
	}
	
	public static void main(String[] args){//主函数
		int count = 0;
		for(int i = 100;i < 1000;i++){
			if(Judge_narcissistic(i) == 1){//如果为1,说明是水仙花数
				System.out.print(i + "  ");
				count++;
				if(count % 5 == 0){//count是为了输出美观,每五个水仙花数为1行
					System.out.print("\n");
				}
			}
		}
	}

运行结果:

153 370 371 407



【程序4】
题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5。
程序分析:首先最小的质数为2,建立循环从2开始递增,如果这个数能被某个数整除,则这个数赋值为整除后的结果,继续循环这个步骤,直到结束。

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter the num:");
		int num = input.nextInt();
		System.out.print(num + " = ");
		for(int i = 2;i <= num;i++){//确定质因数的范围
			while(num % i == 0&&num != i){//利用while循环来找出那些相同的质因数,例如题目中所给 90 = 2*3*3*5 ;3便是相同的质因数
				num = num / i;
				System.out.print(i + " * ");
			}
			if(i == num){//循环到头,输出最后一个质因数,因为最后一位输出和前面的略有不同
				System.out.print(i);
				break;
			}
		}
		input.close();
	}

运行结果:

Please enter the num:
210
210 = 2 * 3 * 5 * 7



【程序5】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b条件运算符用于此次可以简化程序

public static void main(String[] args){
		System.out.println("************输入-1为结束************");
		Scanner input = new Scanner(System.in);
		int n;
		do{
			System.out.println("Plese Enter the grade:");
			n = input.nextInt();
			if(n>100 || n<0){//如果输入不符合范围,则提示输入错误
				System.out.println("Your Input is wrong!");
			}else{
				String str = (n >= 90)?"分,属于A等":((n >= 60)?"分,属于B等":"分,属于C等");//条件嵌套
				System.out.println(n + str);
			}
		}while(n != -1);//利用 do while循环使语句先运行一次,再进行判断
		input.close();
	}

运行结果:

*** 输入-1为结束 ***
Plese Enter the grade:
98
98分,属于A等
Plese Enter the grade:
10
10分,属于C等
Plese Enter the grade:
-1
Your Input is wrong!



【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:辗转相除法

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int m,n,temp;
		int bshu;
		System.out.println("请输入第一个数:");
		m = input.nextInt();
		System.out.println("请输入第二个数:");
		n = input.nextInt();
		bshu = m * n;
		if(n < m){//如果n<m,则交换n和m的值
			temp = n;
			n = m;
			m = temp;
		}
		while(m != 0){//辗转相除法
			temp = n % m;
			n = m;
			m = temp;
		}
		System.out.println("最大公约数为:"+n);
		System.out.println("最小公倍数为:"+bshu/n);
		input.close();
	}

运行结果:

请输入第一个数:
27
请输入第二个数:
78
最大公约数为:3
最小公倍数为:702



【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:在java中,没有单个输入字符的输入语句,因此我们需要输入一个字符串,并用java自带函数toCharArray()将字符串转化为字符数组,再利用循环结构判断.

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		String s = input.nextLine();//输入字符串
		int i;
		int countletter,countDigit,countkong,countother;//计数变量
		countletter = countDigit = countkong = countother = 0;
		char[] ch = s.toCharArray();//将字符串传入字符数组ch中
		for(i = 0;i<ch.length;i++){//循环判断
			if(ch[i] >= 'a'&&ch[i] <= 'z'||ch[i] >= 'A'&&ch[i] <= 'Z'){
				countletter++;
			}else{
				if(ch[i] >= '0'&&ch[i] <= '9'){
					countDigit++;
				}else{
					if(ch[i] == ' '){
						countkong++;
					}else{
						countother++;
					}
				}
			}
		}
		System.out.println("字母:"+countletter);
		System.out.println("数字:"+countDigit);
		System.out.println("空格:"+countkong);
		System.out.println("其他字符:"+countother);
		input.close();
	}

运行结果:

Java is good 233!!
字母:10
数字:3
空格:3
其他字符:2



【程序8】
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
程序分析:计算出每一项的值,然后计算总和。

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int a;
		int n;
		int item,sum;
		sum = 0;
		System.out.println("请输入a:");
		a = input.nextInt();
		System.out.println("请输入数据的个数:");
		n = input.nextInt();
		item = a;
		for(int i=1;i<=n;i++){
			sum = sum + item;
			item = item * 10 + a;//计算每一项的值,由题容易看出求法
		}
		System.out.println("sum = "+sum);
		input.close();
	}

运行结果:

请输入a:
2
请输入数据的个数:
5
sum = 24690



【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
程序分析:利用循环结构将这个数的所有因子全部加起来,除以2,判断总和是否等于这个数,除以2是因为最后一个因子为这个数本身。

public static void main(String[] args){
		int i;
		int count = 0;
		int sum;
		for(i = 1;i<=1000;i++){
			sum = 0;
			for(int j=1;j<=i;j++){
				if(i%j == 0){
					sum = sum + j;//因子相加
				}
			}
			if(sum/2 == i){//判断因子总和是否等于这个数
				System.out.print(i + " ");
				count++;
				if(count%5 == 0){
					System.out.print("\n");
				}
			}
		}
	}

运行结果:

6 28 496



【程序10】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第n次反弹多高?
程序分析:这是一道典型的数学题,先在草纸上进行验算,最后将过程转化为代码语句即可。

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double height = 100;
		double sum = 100;
		int n;
		n = input.nextInt();
		if(n == 1){
			System.out.println("共经过100m,第一次反弹50m.");
		}else{
			for(int i=2;i<=n;i++){
				sum = sum + height;
				height = height/2;//这里height计算的落在地上的前一次的高度
			}
			System.out.println("共经过"+sum+"m,"+"第"+n+"次反弹"+height/2+"m.");//因此这里height需要除以2
		}
		input.close();
	}

运行结果:

5
共经过287.5m,第5次反弹3.125m.

在这里插入图片描述
持续更新中… …

  • 10
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太吾传人,玛卡巴卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值