用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

实现隋朝农民起义的过程:线程的yield与方法random判别的区别

2015-07-23 作者: 锋客举报

[java]代码库

package fengke.game.thread;

import java.util.PriorityQueue;

import javax.swing.plaf.SliderUI;

/**
 * 使用线程的yield()方式实现隋朝农民起义
 * @author 锋客
 *内容:1、创建军队线程===ruannable
 *    2、创建类,存放军队信息
 *    3、演义军队战争
 *    4.演出创建
 *
 */

public class Stage {
	
	public static void main(String[] args) {
		
		//创建军队========》线程
		 ArmyRunnable armyRunnable_Sui = new ArmyRunnable();
		 ArmyRunnable armyRunnable_Revolt = new ArmyRunnable();
		 Thread thread_armyRunnable_Sui=new Thread(armyRunnable_Sui,"隋朝军队");
		 Thread thread_armyRunnable_Revolt=new Thread(armyRunnable_Revolt,"起义军");
		//定义各个军队人数和军队名
		int number_armyRunnable_Sui=100;//隋朝军队人数
		int number_armyRunnable_Revolt=80;//起义军人数
		//将军队信息存放入类中信息
		Information infor=new Information();
		infor.number_army=number_armyRunnable_Sui;
		infor.number_revolt=number_armyRunnable_Revolt;
		armyRunnable_Sui.infor=infor;
		armyRunnable_Revolt.infor=infor;
		/*
		 * 开始作战
		 * 使用start()与run()有区别
		 *    前者进程交替使用,后者先执行完当前的run()犯法,再执行下一个代码;
		 */
		System.out.println("演出即将开始:请大家带好眼镜,做好准备!!!!");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//战争模拟
		thread_armyRunnable_Sui.start();
		thread_armyRunnable_Revolt.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("欢迎大家下次再来!!!");
	}

}

package fengke.game.thread;

/**
 * 军队线程
 * 
 * @author 锋客 内容:1.实现军队线程的创建 2.模拟战争情况
 */

public class ArmyRunnable implements Runnable {
	Information infor;

	@Override
	public void run() {
		// 下列定义的变量在各个情况下,只调用一次,不会改变值
		// int number_armykilled = 0;
		// int number_revoltkilled = 0;
		// int number_army = infor.number_army;
		// int number_revolt = infor.number_revolt;

		int number_army = infor.number_army;
		int number_revolt = infor.number_revolt;
		switch (Thread.currentThread().getName()) {
		case "隋朝军队":

			while (true) {
				// infor.number_revolt <= 0 || infor.number_army <= 0判断单方人数是否为0
				if (infor.number_revolt <= 0 || infor.number_army <= 0) {
					if (infor.number_revolt <= 0) {
						System.out.println("隋朝军队胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_revolt)+"人");
					} else {
						System.out.println("起义军胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_army)+"人");
					}
					break;
				}
				int number_temp1 = 0;
				// 模拟军队的战争情况
				number_temp1 = (int) (Math.random() * 10);
				System.out.println(Thread.currentThread().getName() + "发起了强烈的进攻," + "以" + (infor.number_army) + "兵力"
						+ "重伤敌军" + number_temp1 + "人!!!");
				// 可以实现累加功能,累计被杀敌军数
				infor.number_revolt = infor.number_revolt - number_temp1;
				Thread.yield();

			}

			break;
		case "起义军":
			while (true) {
				//记录本次杀敌人数
				int number_temp = 0;
				if (infor.number_revolt <= 0 || infor.number_army <= 0) {
					break;
				}
				number_temp = (int) (Math.random() * 10);
				System.out.println(Thread.currentThread().getName() + "发起了强烈的进攻," + "以" + (infor.number_revolt) + "兵力"
						+ "重伤敌军" + number_temp + "人!!!");
				infor.number_army = infor.number_army - number_temp;
				Thread.yield();

			}

			break;

		default:
			System.out.println("演出结束!!!");
			break;
		}

	}

}

=============================================================================================
package fengke.game.random;

import java.util.PriorityQueue;
import java.util.Random;
import javax.swing.plaf.SliderUI;

/**
 * 通过随机数的方法实现隋唐演义(不使用yield)
 * 
 * @author 锋客 
 * 内容:1、创建军队线程===ruannable 
 *     2、创建类,存放军队信息 
 *     3、演义军队战争 
 *     4、演出创建
 * 异常:线程的创建使用extends Thread的方法时出现异常
 *     java.lang.IllegalThreadStateException===同一个Thread不能重复调用start方法
 *
 */

public class Stage {

	public static void main(String[] args) {

		// 创建军队========》线程
		ArmyRunnable armyRunnable_Sui = new ArmyRunnable();
		ArmyRunnable armyRunnable_Revolt = new ArmyRunnable();
		
	   /*
	    * 该创建的线程有问题,在下面代码中会有提到
		* Thread thread_armyRunnable_Sui = new Thread(armyRunnable_Sui, "隋朝军队");
		* Thread thread_armyRunnable_Revolt = new Thread(armyRunnable_Revolt, "起义军");
		*/
		// 定义各个军队人数和军队名
		int number_armyRunnable_Sui = 100;// 隋朝军队人数
		int number_armyRunnable_Revolt = 80;// 起义军人数
		// 将军队信息存放入类中信息
		Information infor = new Information();//专门存放军队信息
		infor.number_army = number_armyRunnable_Sui;
		infor.number_revolt = number_armyRunnable_Revolt;
		armyRunnable_Sui.infor=infor;
		armyRunnable_Revolt.infor=infor;
		//开始演出
		System.out.println("演出即将开始:请大家带好眼镜,做好准备!!!!");
		System.out.println();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*
		 * 战争模拟 内容:
		 * 使用Random类 产生随机数,確定軍隊紛爭
		 * 战争结束的条件:单方军队数为0
		 */
		Random random = new Random();
		while(infor.number_revolt > 0 && infor.number_army > 0){
			if(random.nextInt(2)>0){
		    Thread thread_armyRunnable_Sui = new Thread(armyRunnable_Sui, "隋朝军队");
		    thread_armyRunnable_Sui.start();
			}else{
				Thread thread_armyRunnable_Revolt = new Thread(armyRunnable_Revolt, "起义军");
				thread_armyRunnable_Revolt.start();
			}
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
/*
 * 不使用下列代码的原因:
 *      对于一个线程来说,他只能被启动一次,不能重复启动;
 *      
 *      while(infor.number_revolt > 0 && infor.number_army > 0){
 *		if (random.nextInt(2) > 0) {
 *			thread_armyRunnable_Sui.start();
 *		} else {
 *			thread_armyRunnable_Revolt.start();
 *		  }
 *     	}
 */     
		System.out.println();
		System.out.println("欢迎大家下次再来!!!");
	}

}

package fengke.game.thread;

public class Information {
	public  int number_army=0;
	public  int number_revolt=0;
	

}


package fengke.game.random;

import java.util.Random;

/**
 * 军队线程
 * @author 锋客
 *  内容:
 *      1.实现军队线程的创建
 *      2.模拟战争情况
 */

public class ArmyRunnable implements Runnable {
	//存放军队信息
	Information infor;
	@Override
	public void run() {
		// 下列定义的变量在各个情况下,只调用一次,不会改变值
		// int number_armykilled = 0;
		// int number_revoltkilled = 0;
		// int number_army = infor.number_army;
		// int number_revolt = infor.number_revolt;

		int number_army = infor.number_army;
		int number_revolt = infor.number_revolt;
		switch (Thread.currentThread().getName()) {
		case "隋朝军队":
				
				int number_temp1 = 0;
				// 模拟军队的战争情况
				//number_temp1为起义军死亡人数,random类产生0----(infor.number_revolt+1)的数,防止双方最后剩一个人,造成死循环
				number_temp1 = new Random().nextInt(infor.number_revolt+1);
				System.out.println(Thread.currentThread().getName() + "发起了强烈的进攻," + "以" + (infor.number_army) + "兵力"
						+ "重伤敌军" + number_temp1 + "人!!!");
				// 可以实现累加功能,累计被杀敌军数
				infor.number_revolt = infor.number_revolt - number_temp1;
				// infor.number_revolt <= 0 || infor.number_army <= 0判断单方人数是否为0
				if (infor.number_revolt <= 0 || infor.number_army <= 0) {
					if (infor.number_revolt <= 0) {
						System.out.println("隋朝军队胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_revolt)+"人");
					} else {
						System.out.println("起义军胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_army)+"人");
					}
				}
	

			break;
		case "起义军":
				//记录本次杀敌人数
				int number_temp = 0;
				//number_temp1为隋朝军队死亡人数,random类产生0----(infor.number_army+1)的数,防止双方最后剩一个人,造成死循环
				Random random=new Random();
				number_temp = random.nextInt(infor.number_army+1);
				System.out.println(Thread.currentThread().getName() + "发起了强烈的进攻," + "以" + (infor.number_revolt) + "兵力"
						+ "重伤敌军" + number_temp + "人!!!");
				infor.number_army = infor.number_army - number_temp;
				if (infor.number_revolt <= 0 || infor.number_army <= 0) {
					if (infor.number_revolt <= 0) {
						System.out.println("隋朝军队胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_revolt)+"人");
					} else {
						System.out.println("起义军胜利!!!");
						System.out.println("误伤平民"+(0-infor.number_army)+"人");
					}
				}
			break;

		default:
			
			break;
		}

	}

}


package fengke.game.random;
/**
 * 存放基本军队信息
 * @author 锋客
 *
 */
public class Information {
	public  int number_army=0;
	public  int number_revolt=0;
	

}




=============================================================================================






[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...