用java模拟一场篮球赛
❶ java多线程--模拟接力赛跑
import java.util.*;
public class Main {
public static void main(String[] args) {
RunThread run = new RunThread();
for (int i = 1; i <= 5; ++i) {
new Thread(run, i + "号选手").start();
}
}
}
class RunThread implements Runnable {
int distance;
@Override
public synchronized void run() {
String name = Thread.currentThread().getName();
System.out.println(name + "拿到接力棒!");
for (int i = 0; i < 10; ++i) {
distance += 10;
System.out.println(name + "跑到了" + distance + "米!");
}
}
}
❷ java课程设计模拟接力赛赛跑
package hh;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Player implements Runnable{
private String name;
private CyclicBarrier barrier;
private Player next;//下一棒
private int time;//用时
private boolean run;//第一棒
public Player(String name, CyclicBarrier barrier, boolean run) {
super();
this.name = name;
this.setBarrier(barrier);
this.run = run;
}
public void run() {
try {
synchronized (this) {
while(!run){//等待队员
wait();
}
}
Random r = new Random();
TimeUnit.MILLISECONDS.sleep(r.nextInt(2000));
next(next,11 + r.nextInt(2));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void next(Player next, int time) {
System.out.println(name + " 用时:" + time + ",交接棒");
if(next != null){
next.setTime(this.time + time);
synchronized (next) {
next.setRun(true);
next.notify();
}
}else{
System.out.println("跑完,总用时:" + (this.time + time));
}
}
public void setTime(int time) {
this.time = time;
}
public int getTime(){
return this.time;
}
public void setNext(Player next) {
this.next = next;
}
public void setRun(boolean run) {
this.run = run;
}
public void setBarrier(CyclicBarrier barrier) {
this.barrier = barrier;
}
public CyclicBarrier getBarrier() {
return barrier;
}
}
public class RelayRace {
public static void main(String[] args) throws InterruptedException {
final Player[] players = new Player[4];
ExecutorService exec = Executors.newCachedThreadPool();
CyclicBarrier barrier = new CyclicBarrier(4, new Runnable() {
public void run() {
System.out.println("结束,总用时:" + players[3].getTime());
}
});
for(int i = 0; i < 4; i++){
players[i] = new Player("队员" + ( i + 1), barrier, i == 0);
}
for(int i = 0; i < 4; i++){
if( i < 3){
players[i].setNext(players[i + 1]);
exec.execute(players[i]);
}else{
exec.execute(players[3]);
break;
}
}
}
}
❸ java课程设计 模拟游乐场的投篮游戏 求帮忙
没有玩过游乐场的投篮游戏
❹ 用java设计一个具有时钟和适用于篮球比赛倒计时的界面,并不要求有实际功能。~~~大大们求代码~~~
一分不给,太抠门了这也
❺ java项目类似篮球的比赛投屏要使用socket
socket只是一个协议,简历连接以后,一端把流发出去,另一端的接收端口就会收到,所以你知道简单学习一下socket怎么发送就可以了,而且你这个项目不需要多线程,所以不会有大问题
❻ JAVA编程题中,四个学生小李,小张,小赵,和小王在打篮球,现在编写一个程序,模拟他们抢篮球的过程,
ご参照してください。
public class People {
private String name;
public int num;
public People(String name) {
this.name = name;
this.num = 0;
}
public String getName() {
return this.name;
}
}
--------------------------------------------------------------------------------------------------
public class Play {
public static void main(String[] args) {
Play play = new Play();
List<People> pl = new ArrayList<People>();
pl.add(new People("小王"));
pl.add(new People("小张"));
pl.add(new People("小赵"));
pl.add(new People("小李"));
while(pl.size() > 0) {
play.challenge(pl);
}
}
private void challenge(List<People> pl) {
Random rd = new Random();
// この人はリストにのインデックス。リストは人がひとつある场合、ゼロです。
int nowPeople = pl.size() > 0 ? rd.nextInt(pl.size()) : 0;
int total = pl.get(nowPeople).num + 1;
pl.get(nowPeople).num = total;
System.out.println(pl.get(nowPeople).getName() + "抢到了,第" + total + "次球");
// 何番目でアウトする
if (total == 7) {
System.out.println(pl.get(nowPeople).getName() + "不想玩了");
pl.remove(nowPeople);
}
}
}
❼ 用JAVA写一个有关世界杯淘汰赛赛程的程序
付费还差不多- -。。。。。
❽ 模拟队伍比赛 JAVA
不想写代码 给你个思路,首先要确定怎么来确定晋级,比多少次 应该是n-1次,
确定淘汰 就是用取0--1之间的随机数 如甲乙实力比是1/2 那么产生随机数 0-1之间的 分为3份 可以当产生0-1/3 时 甲晋级 否则乙晋级 可以满足 淘汰实力对比,
其余的应该都不是难点啊
随机函数在 Math类中
具体自己研究吧
❾ 用Java语言建立一个学生打篮球的模型
二语言可以干很多事情,你可以直接下载一个作业帮,然后输入你的问题就可以出来解释了