Java基础练习:1—5

基础题一:

/**
	古典问题:
		有一只兔子,从出生后第3个月起每个月都生一只兔子,
		小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,
		问每个月的兔子只数为多少?
		
	程序分析:
		仔细分析可知,这是一个求斐波那契数列:1,1,2,3,5,8,13,21.... 
		数列的第一项和第二项都是1,从第三项开始,每一项的值为前两项的值的和
		解决方法:使用递归,返回前两项的值
*/
import java.util.Scanner;

public class Program01{
	public static void main(String[] args){
		System.out.print("请输入月数:");
		Scanner sc = new Scanner(System.in);
		int month = sc.nextInt();
		System.out.println("第" + month + "个月兔子总数为:" + fiBo(month) + "只");
	}
	
	public static int fiBo(int mon){
		if(mon==1 || mon==2){
			return 1;
		}else{
			return fiBo(mon-1) + fiBo(mon-2);
		}
	}
}

基础题二:

/**
	题目:
		判断任意输入的两个数之间有多少个素数,输出所有素数。
		
	程序分析:
		判断素数的方法:用一个数分别去除2到sqrt(这个数),
		如果能被整除,则表明此数不是素数,反之是素数。
*/

import java.util.Scanner;
import java.lang.*;

public class Program02{
	public static void main(String[] args){
		int count = 0; //用于统计素数的个数
		
		System.out.print("请输入第一个数:");
		Scanner sc = new Scanner(System.in);
		int first = sc.nextInt();
		System.out.print("请输入第二个数:");
		int second = sc.nextInt();
		
		for(int i = first; i < second; i++){
			if(isPrime(i)){
				count++;
				System.out.print(" " + i);
				if(count%10 == 0){
					System.out.println();
				}
			}
		}
		System.out.println();
		System.out.println("在" + first + "和" + second + "之间一共有 " + count + " 个素数");
		
	}
	
	public static boolean isPrime(int number){
		boolean flag = true;
		if(number == 1){
			flag = true;
		}else{
			for(int i = 2; i <= Math.sqrt(number); i++){
				if((number%i) == 0 || number == 1){
					flag = false;
					break;
				}else
					flag = true;
			}
		}
		return flag;
	}
}

基础题三:

/**
	题目:
		打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,
		其各位数字立方和等于该数本身。例如:153是一个"水仙花数",
		因为153=1的三次方+5的三次方+3的三次方。
		
	程序分析:
		利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*/

public class Program03{
	public static void main(String[] args){
		int count = 0; //用于统计水仙花数的个数
		
		System.out.print("Result is:");
		for(int i = 100; i < 999; i++){
			if(isLotus(i)){
				count++;
				System.out.print(" " + i);
				if(count%10 == 0){
					System.out.println();
				}
			}
		}
		
		System.out.println();
		System.out.println("一共有 " + count + " 个水仙花数");
		
	}
	
	public static boolean isLotus(int number){
		int ge, shi, bai;
		int n = number;
		
		bai = n/100; //提取该数的百位数
		n -= bai*100;
		
		shi = n/10; //提取该数的十位数
		
		ge = n%10; //提取该数的个位数
		
		if(number == bai*bai*bai + shi*shi*shi + ge*ge*ge)
			return true;
		else
			return false;
	}
}

基础题四:

/**
	题目:
		将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
		
	程序分析:
		对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
			(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
			(2)如果n < k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。
			(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

*/
import java.util.Scanner;

public class Program04{
	public static void main(String[] args){	
		System.out.print("Please input a number: ");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		System.out.print(num + " = ");
		for(int i = 2; i <= num; i++){
			while(num%i == 0 && num != i){
				System.out.print(i + "*");
				num /= i;
			}
			if(num == i){
				System.out.println(i);
				break;
			}
		}		
	}
}

基础题五:

/**
	题目:
		利用条件运算符的嵌套来完成此题:
		学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
		
	程序分析:
		(a>b)?a:b这是条件运算符的基本例子。 

*/
import java.util.Scanner;
import java.lang.String;

public class Program05{
	public static void main(String[] args){	
		System.out.print("Please input a score: ");
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		String str = null;
		
		if(score > 100 || score < 0)
			System.out.println("input error!");
		else
			str = (score >= 90) ? "分,属于A等" : ((score < 60) ? "分,属于C等" : "分,属于B等");
		System.out.println(score + str);
	}
}

下一节:Java基础练习:6-10

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值