2014.10.17--Java基础课第四天学习总结

1.五人分鱼问题

package com.day1017;

/**
 * 五人分鱼问题
 * 每次分成5份多1条,分五次,求至少多少条鱼。
 * @author hechao
 *
 */
public class Test01 {
	public static void main(String[] args) {
		
		// 穷举法
		for (int i = 1;; i++) {
			int fish = i;// 用变量fish记录下假设的鱼的条数
			boolean isEnough = true;// 鱼还够不够分
			for (int j = 1; isEnough && j <= 5; j++) {
				if ((fish - 1) % 5 == 0) {
					fish = (fish - 1) * 4 / 5;
				} else {
					isEnough = false;// 鱼不够分了
				}
			}
			if (isEnough) {
				System.out.println(i);
				break;
			}
		}
	}
}</span>

2.打印1-100之间的素数

package com.day1017;

/**
 * 打印1-100之间的素数
 * 只能被1和自身整除的数
 * 判断2到n-1之间是否有因子
 * 
 * @author 何超
 *
 */
public class Test02 {
	public static void main(String[] args) {		
		System.out.println("1到100的素数有:"); 
		for(int i = 1; i <= 100; i++){ 
			boolean isPrime = true;//用布尔值表示i是不是素数			
			for(int j = 2; isPrime && j <= i - 1; j++){
			//for(int j = 2; isPrime && j <= Math.sqrt(i); j++){
			//在2到根号i之间寻找i的因子
				if (i % j == 0){
					isPrime = false;//不是素数讲布尔值赋为false
				}
			}
			if(isPrime) {
				System.out.print(i + ", ");
			}
		}
	}
}


3.将素数和阶乘方法保存为常用方法,方便调用

package com.day1017;

/**
 * 将该方法保持为方法,方便调用
 * 打印1-100之间的素数
 * 只能被1和自身整除的数
 * 判断2到n-1之间是否有因子
 * 
 * @author 何超
 *
 */
public class Test03 {
	public static boolean isPrime(int n) {//素数判定
		for(int j = 2; j <= Math.sqrt(n); j++){			
				if (n % j == 0) {
					return false;
				}
			}
		return true;
	}//将判断是否为素数存储为一个isPrime方法,方便以后调用 
	public static double jc(int n){//阶乘	//进入数	 
			double result = 1; // 累乘变量			
			for (int i = 1; i <= n; i++) {
				result *= i;
			}
		return(result);	//返回数
	}//阶乘
	public static void main(String[] args) {
		double n = 0;
		for(int i = 1; i <= 9; i++){			
			n += jc(i);						
		}
		System.out.printf("%.0f",n);	
		
	}
}


4.计算两个数的最大公约数 

package com.day1017;

import java.util.Scanner;

/**
 * 计算最大公约数(greateest common divisor) * 
 * @param x
 * @param y
 * @return返回 参数x和y的最大公约数
 * @author hechao
 *
 */
public class Test04 {
	public static  int gcd(int x,int y){
		for(int i = x < y ? x : y; i > 1; i--){
			if(x % i == 0 && y % i == 0){
				return i;
			}
		}		
			
		return 1;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数:");
		int a = sc.nextInt();
		System.out.println("请输入的二个数:");
		int b = sc.nextInt();
		int c = gcd(a,b);
		System.out.printf("%d和%d的最大公约数是:%d",a,b,c);
		sc.close();
		
	}
}


5.七星彩随机选号程序

package com.day1017;

import java.util.Scanner;

/**
 * 七星彩随机选号程序
 * 输入购买彩票注数n,输出n注7个0到9的随机数
 * @author hechao
 *
 */
public class Test05 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入所购七星彩注数:");
		int n = sc.nextInt();
		for(int a = 1; a <= n; a++){
			for(int i = 1; i <= 7; i++){
				int j = (int)(Math.random() * 10);
				System.out.print(j+" ");
			}
			System.out.println();
		}
		sc.close();
	}
}



6.猜数字游戏

package com.day1017;

import java.util.Scanner;

/**
 * 猜数字游戏
 * 
 * @author hechao
 *
 */
public class Test06 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int counter = 0;
		int answer = (int) (Math.random() * 100 + 1);
		int yourAnswer;
		do {
			counter++;
			System.out.println("亲,你猜的数字是:");
			yourAnswer = sc.nextInt();
			if (answer > yourAnswer) {
				System.out.println("亲,再大一点!");
			} else if (answer < yourAnswer) {
				System.out.println("亲,再小一点!");
			} else {
				System.out.println("恭喜你,猜对了!");
				System.out.println("亲,你猜了" + counter + "次");
			}
		} while (yourAnswer != answer);
		sc.close();
	}
}


7.Craps赌博游戏 

package com.day1017;


/**
 * Craps赌博游戏 
 * @author hechao
 *
 */
public class Test07 { 
	/**
	 * 摇一颗骰子
	 * @return 摇出点数
	 */ 
	public static int roll(){
		return (int) (Math.random() * 6 + 1); 
	}
	
	public static void main(String[] args) {		
		int firstPoint, currentPoint;
		firstPoint = currentPoint = roll() + roll();
		System.out.println("玩家摇出了" + currentPoint + "点");
		boolean goon = false;
		switch(currentPoint){
			case 7:
			case 11:
				System.out.println("玩家胜!!!");
				break;
			case 2:
			case 3:
			case 12:
				System.out.println("庄家胜!!!");
				break;
			default:
				goon = true; 				
		}
		while(goon) {
			currentPoint = roll() + roll();
			System.out.println("玩家摇出了" + currentPoint + "点");
			if(currentPoint == 7) {
				System.out.println("庄家胜!!!");
				goon = false;
			}
			else if(currentPoint == firstPoint) {
				System.out.println("玩家胜!!!");
				goon = false;
			}
		}
	}
}


8.求10000以内的完美数

package com.day1017;
/**
 * 求10000以内的完美数
 * 完美数(等于自己因子和的数)
 * 6=1+2+3
 * 28=1+2+4+7+14
 * @author hechao
 *
 */
public class Test08 {
	public static void main(String[] args) {
		System.out.print("10000以内的完美数有:");
		for(int num = 1; num <= 10000; num++){
			int sum = 0;
			for(int facter = 1; facter <= num / 2; facter++){
				if(num % facter == 0){
					 sum += facter;
				}
				else{
					continue;
				}
			}
			if(sum == num){
				System.out.print(num + " ");
			}
		}		
	}
}


9.21根火柴游戏

package com.day1017;

import java.util.Scanner;

/**
 * 21根火柴游戏
 * 人先抽,每次只能抽1到4根,谁最后抽谁输。
 * @author hechao
 *
 */
public class Test09 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//int counter = 0;
		int sum = 21;
		int sum1 = 21;
		for (; sum1 > 0 && sum > 0; ) {			
			System.out.print("请输入你抽取的火柴数:");
			int myNum = sc.nextInt();			
			sum1 = 21;
			sum1 -= myNum;			
			int computerNum = 5 - myNum;
			System.out.printf("计算机抽取了%d根火柴\n", computerNum);
			sum -= 5;
			if(sum1 == 1){
				System.out.println("玩家胜!!!");
				break;
			}
			if(sum == 1){
				System.out.println("电脑胜!!!");
				break;
			}
		}

		sc.close();
	}
}


10.谁是小偷

package com.day1017;
/**
 * 谁是小偷
 * a说:b没偷,d偷的;
 * b说:我没偷,c偷得;
 * c说:a没偷,b偷的;
 * d说:我没偷。谁是小偷?
 * @author hechao
 * 
 */
public class Test10 {
	public static void main(String[] args) {
		int a, b, c, d;
		for(int i = 0; i <= 1; i++){
			for(int j = 0; j <= 1; j++){
				if(i == 0){//i==0表示a说谎,对应的d说的是事实。
					b = 1; d = 0;	//偷用1表示,没偷用0表; 				
				}
				else{//a说的是事实,则d说谎。
					b = 0; d = 1;
				}
				if(j == 0){//j==0表示b说谎,对应的c说的是事实。
					a = 0; b = 1; c = 0; 
				}
				else{//b说的是事实,则c说谎;
					a = 1; b = 0; c = 1;
				}
				if(a==1&&b==0&&c==0&&d==0){
					System.out.println("a是小偷!!!");
					break;
				}
				if(b==1&&a==0&&c==0&&d==0){
					System.out.println("b是小偷!!!");
					break;
				}
				if(c==1&&b==0&&a==0&&d==0){
					System.out.println("c是小偷!!!");
					break;
				}
				if(d==1&&b==0&&c==0&&a==0){
					System.out.println("d是小偷!!!");
					break;
				}
			}
		}		
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值