「java赛马」田忌赛马java代码
本篇文章给大家谈谈java赛马,以及田忌赛马java代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
用JAVA语言模拟赛马
你这个要求得从零开始做。你得给出具体要求。
比如你需不需要后台控制哪匹马赢得比赛,一个赛马过程大概使用时间,还有其他什么什么……
java利用多线程编写赛马程序
package ThreadTest;
public class HorseRacing
{
public static void main(String[] args) {
Herose black = new Herose("黑马" , 19 , 1000); // new Herose(名称,最大速度 , 赛道总长)
Herose white = new Herose("白马" , 18 , 1000);
Herose red = new Herose("红马",20 , 1000);
black.race();
white.race();
red.race();
}
}
package ThreadTest;
import java.util.Timer;
public class Herose{
private String name;
private int maxSpeed;
private int distance;
public Timer timer = new Timer();
public void race(){
Runnable runnable = new Runnable(){
public void run(){
int Min = 1;
int Max = getMaxSpeed();
int i = 0;
int myDistance = getDistance();
int position = 0;
int nowSpeed = Min + (int)(Math.random() * ((Max - Min) + 1));
while( 0 myDistance ){
if( i 0 i % 10 == 0 ){
nowSpeed = Min + (int)(Math.random() * ((Max - Min) + 1));
position = getDistance()- myDistance;
System.out.println( i + "秒 : " + getName() + " 位置为 " + position + "m" + " 当前速度为 " + nowSpeed +"m/s");
}
++i;
myDistance = myDistance - nowSpeed;
if(myDistance = 0){
System.out.println( i + "秒 : " + getName() + "到达终点!");
}
try
{
Thread.sleep( 1000); //间隔1000毫秒 = 1秒
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
}
public Herose(String name, int maxSpeed , int distance)
{
this.name = name;
this.maxSpeed = maxSpeed;
this.distance = distance;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getMaxSpeed()
{
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed)
{
this.maxSpeed = maxSpeed;
}
public int getDistance()
{
return distance;
}
public void setDistance(int distance)
{
this.distance = distance;
}
}
java 多线程 赛马
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import sun.util.logging.resources.logging;
/**
* 2010-1-17 下午03:08:52 Piaolj
*/
public class Racing implements Runnable {
String str;
static Map horses = new TreeMap();// 定义一个全局的Map存放5个马的名字和时间
static int count = 0;// 用于判断县城是否全部结束
static boolean flag = true;// 后面用while(flag)判断count是否为0,如果有兴趣,可以起另外一个线程完成此任务
public Racing(String string) {
// TODO Auto-generated constructor stub
this.str = string;
}
public static void main(String[] args) {
Racing ra1 = new Racing("No.1 Horse");
Racing ra2 = new Racing("No.2 Horse");
Racing ra3 = new Racing("No.3 Horse");
Racing ra4 = new Racing("No.4 Horse");
Racing ra5 = new Racing("No.5 Horse");
Racing checkingThread = new Racing("checkingThread");
Thread t1 = new Thread(ra1);
Thread t2 = new Thread(ra2);
Thread t3 = new Thread(ra3);
Thread t4 = new Thread(ra4);
Thread t5 = new Thread(ra5);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
while (flag) {
if (count == 0)// 所有线程结束
{
flag = false;
}
}
// 排序
List infoIds = new ArrayList(horses.entrySet());
Collections.sort(infoIds, new ComparatorMap.Entry() {
public int compare(Map.Entry o1, Map.Entry o2) {// 定义了比较的规则,因为这里是按map的value排序的
Long tmp = Long.parseLong(o1.getValue().toString())
- Long.parseLong(o2.getValue().toString());
return tmp.intValue();
}
});
System.out.println("输出马的名次:");
System.out.println();
for (int i = 0; i infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
}
public void run() {
// TODO Auto-generated method stub
int CircuitLength = 1000;
int breakpoint = 200;
int tmpint = 200;
long Withtime;
count = count + 1;// 执行了一个线程,正在执行的线程数加1
// System.out.println(Thread.currentThread().getId());
for (int i = 0; i CircuitLength + 1; i++) {
long start = System.currentTimeMillis();
if (i == breakpoint) {
int sleeping = (int) (Math.random() * 5000);
try {
Thread.sleep(sleeping);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
breakpoint = breakpoint + tmpint;
}
if (i == CircuitLength) {
System.out.print(str + "\t" + "\t");
long end = System.currentTimeMillis();
Withtime = (end - start);
System.out.println("With time is:\t" + Withtime);
// 当每匹马跑完将马的时间的马的名称放入map中
horses.put(str, Withtime);
}
}
count = count - 1;// 执行完了一个线程,线程数减1
}
}
时间比较少,写的比较草,呵呵 加了点注释,要是有问题可以发短消息
输出结果:
No.4 Horse With time is: 888
No.3 Horse With time is: 3042
No.5 Horse With time is: 1921
No.2 Horse With time is: 4346
No.1 Horse With time is: 2831
输出马的名次:
No.4 Horse=888
No.5 Horse=1921
No.1 Horse=2831
No.3 Horse=3042
No.2 Horse=4346
java 多线程 赛马游戏设计
import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a500;a++){
for(int j = 0;j2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度为"+red);
System.out.println("green的速度为"+green);
while(red =500 || green=500){
if(red =500){
System.out.println("red先抵达终点线");
}
if(green = 500){
System.out.println("green先抵达终点线");
}
if(green ==500 red ==500 ){
System.out.println("两个同时到达");
}
return;
}
}
/* if(red green){
System.out.println("Redwin"+red);
}
if(redgreen){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/
}
}//给你个思路自己用swing画出来,这个小程序是把速度设为2红绿两只马先超过500这一线的获胜.如果要多人比赛开启线程就好
java赛马的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于田忌赛马java代码、java赛马的信息别忘了在本站进行查找喔。
发布于:2022-12-28,除非注明,否则均为
原创文章,转载请注明出处。