當前位置:首頁 » 編程語言 » java賽馬

java賽馬

發布時間: 2022-10-01 02:05:55

java:利用多線程編寫賽馬程序

上傳限制,追問,發其餘代碼
import java.util.Date;
import java.util.Random;

public class Horse implements Runnable {

public Horse(String name) {
this.name = name;
}

private String name;
private int speed = new Random().nextInt(10) + 10;
private float length = 500;
private float current = 0;
private long time;
private boolean flag = true;
private long _time = 0l;
private onGameOverLisenter overLisenter;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isFlag() {
return flag;
}

public long getTime() {
return time;
}

public void setTime(long time) {
this.time = time;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setOverLisenter(onGameOverLisenter overLisenter) {
this.overLisenter = overLisenter;
}

@Override
public void run() {
while (flag) {
synchronized (this) {
float len = (current += speed);
System.out.println(getName() + " 的當前速度 :" + speed
+ "m/s 已完成距離 : "
+ (current > length ? length : current));
if (len >= length) {
float dis = Math.abs(len - length);
_time = (long) (dis / length * 1000);
time = new Date().getTime() - _time;
flag = false;
System.out.println(name + " : 結束!");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (overLisenter != null) {
overLisenter.gameOver();
}
}

public interface onGameOverLisenter {
public void gameOver();
}
}

⑵ 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 Comparator<Map.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經典編程案例有哪些

  1. java編程的記事本:

    import java.util.*;
    public class JieChengExample
    {
    public static void main(String args[])
    {
    Scanner input=new Scanner(System.in);
    int n,sum;
    Jiecheng jie=new Jiecheng();
    System.out.print("輸入n的值:");//輸入有幾個階乘相加
    n=input.nextInt();
    sum=0;
    for(int i=1;i<=n;i++)
    {
    sum=sum+jie.jiecheng(i);//這是n個階乘相加
    }
    System.out.println("1!+2!+3!+....+n!的和是:"+sum);
    }
    }
    class Jiecheng
    {
    public int jiecheng(int temp)//算階乘的方法
    {
    int sum=1;
    for(int i=1;i<=temp;i++)
    {
    sum=sum*i; //計算階乘
    }
    return sum;//將一個階乘返回
    }
    }

2.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;a<500;a++){
for(int j = 0;j<2;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(red<green){
S...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;a<500;a++){
for(int j = 0;j<2;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(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/

JAVA的介紹:

Java是一種可以撰寫跨平台應用程序的面向對象的程序設計語言。Java技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。

⑷ 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 多線程 賽馬游戲設計

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;a<500;a++){
for(int j = 0;j<2;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(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/

}
}//給你個思路自己用swing畫出來,這個小程序是把速度設為2紅綠兩只馬先超過500這一線的獲勝.如果要多人比賽開啟線程就好

⑹ 用java寫了一個賽馬的程序後,想改用以繪圖的方式呈現馬所在位置,要如何寫呢

所謂動畫,就是每次把圖像清空,重新畫跑道,再畫馬。定時這么做就可以了。速度快時,根本看不出重畫的過程的。

⑺ 用JAVA語言模擬賽馬

你這個要求得從零開始做。你得給出具體要求。

比如你需不需要後台控制哪匹馬贏得比賽,一個賽馬過程大概使用時間,還有其他什麼什麼……

⑻ 我想用JAVA編寫個賽馬程序大神有什麼建議嗎如何做界面。結果如何去顯示出來你的輸贏,金幣的獲得扣除

你這個問題就像是這個問題。

我想造火箭,如何做推進器,用什麼燃料,如何保證高溫下火箭內部不損壞

⑼ Netbeans java賽馬程序

你要別人給你寫這個程序?

估計你這個問題要沉了!~

⑽ java課設,模擬賽馬比賽。我建了一個窗體,想在窗體中畫幾道線作為跑道

你直接弄個背景圖作為跑道不就完了?要這麼麻煩自己畫?

熱點內容
想換個安卓手機什麼機子好 發布:2024-12-25 14:01:38 瀏覽:846
python不是內部或外部 發布:2024-12-25 13:36:14 瀏覽:638
如何看計算機配置信息 發布:2024-12-25 13:18:59 瀏覽:850
安卓手機如何轉到apple手機 發布:2024-12-25 13:06:42 瀏覽:621
linux盤符 發布:2024-12-25 13:05:56 瀏覽:441
資料庫表大小 發布:2024-12-25 13:05:49 瀏覽:209
oppo手機在哪裡找到身份證密碼 發布:2024-12-25 13:02:24 瀏覽:985
911黑武士哪個配置值得入手 發布:2024-12-25 13:00:41 瀏覽:792
如何不用編譯器運行web項目 發布:2024-12-25 13:00:40 瀏覽:847
私密存儲公司 發布:2024-12-25 12:58:31 瀏覽:838