求java 搬砖程序.

求java 搬砖程序.,第1张

有意思 呵呵 :

你参考一下吧:

功能是实现了 逗姿脊,但不知道册碰合不合你胃口:

package main

import java.util.ArrayList

import java.util.List

public class MoveBlock {

/**

* @param args

*/

public static void main(String[] args) {

List<RubPerson>workers=new ArrayList<RubPerson>()

RubPerson rubPerson1 = new RubPerson()

rubPerson1.setName("Person-1")

RubPerson rubPerson2 = new RubPerson()

rubPerson2.setName("Person-2")

RubPerson rubPerson3 = new RubPerson()

rubPerson3.setName("Person-3")

RubPerson rubPerson4 = new RubPerson()

rubPerson4.setName("Person-4")

RubPerson rubPerson5 = new RubPerson()

rubPerson5.setName("Person-5")

rubPerson1.setPoint(0)

rubPerson1.setNextRubPerson(rubPerson2)

rubPerson2.setPoint(1)

rubPerson2.setUpRubPerson(rubPerson1)

rubPerson2.setNextRubPerson(rubPerson3)

rubPerson3.setPoint(1)

rubPerson3.setUpRubPerson(rubPerson2)

rubPerson3.setNextRubPerson(rubPerson4)

rubPerson4.setPoint(1)

rubPerson4.setUpRubPerson(rubPerson3)

rubPerson4.setNextRubPerson(rubPerson5)

rubPerson5.setPoint(2)

rubPerson5.setUpRubPerson(rubPerson4)

rubPerson5.setNextRubPerson(rubPerson1)

workers.add(rubPerson1)

workers.add(rubPerson2)

workers.add(rubPerson3)

workers.add(rubPerson4)

workers.add(rubPerson5)

MyJob job=new MyJob(workers, rubPerson1)

job.setBlockCount(100)

new Thread(job).start()

}

}

class MyJob implements Runnable{

List<RubPerson>山渗 workers=new ArrayList<RubPerson>()

private int blockCount=0

private RubPerson leaderPerson

public List<RubPerson>getWorkers() {

return workers

}

public void setWorkers(List<RubPerson>workers) {

this.workers = workers

}

public int getBlockCount() {

return blockCount

}

public void setBlockCount(int blockCount) {

this.blockCount = blockCount

}

public RubPerson getLeaderPerson() {

return leaderPerson

}

public void setLeaderPerson(RubPerson leaderPerson) {

this.leaderPerson = leaderPerson

}

public MyJob(List<RubPerson>workers, RubPerson leaderPerson) {

super()

this.workers = workers

this.leaderPerson = leaderPerson

}

public void run() {

while(this.getBlockCount()>=1){

leaderPerson.run()

this.setBlockCount(this.getBlockCount()-1)

try {

Thread.currentThread().sleep(1000)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}

class RubPerson implements Runnable{

private String block = null

private boolean isRest = true

private RubPerson nextRubPerson = null

private RubPerson upRubPerson = null

private int point = 0// 0 is start, 1 is middle, 2 is end

private String name

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public void run() {

switch (this.getPoint()) {

case 0:

if(this.isRest()){

this.setBlock("Block")

printState(this.getPoint(), 1)

this.setRest(false)

}

try {

Thread.currentThread().sleep(100)

} catch (InterruptedException e) {

e.printStackTrace()

}

if (nextRubPerson != null) {

printState(this.getPoint(), 2)

nextRubPerson.run()

}

break

case 1:

if(this.isRest()){

if(this.getUpRubPerson().isRest()){

this.run()

break

}else {

this.setBlock(this.getUpRubPerson().getBlock())

printState(this.getPoint(), 1)

this.setRest(false)

this.getUpRubPerson().setRest(true)

}

}

try {

Thread.currentThread().sleep(100)

} catch (InterruptedException e) {

e.printStackTrace()

}

printState(this.getPoint(), 2)

this.nextRubPerson.run()

break

case 2:

if(this.isRest()){

if(this.getUpRubPerson().isRest()){

this.run()

break

}else {

this.setBlock(this.getUpRubPerson().getBlock())

printState(this.getPoint(), 1)

this.setRest(false)

this.getUpRubPerson().setRest(true)

}

}

try {

Thread.currentThread().sleep(100)

} catch (InterruptedException e) {

e.printStackTrace()

}

printState(this.getPoint(), 2)

this.setRest(true)

break

default:

break

}

}

private synchronized String printState(int role, int step) {

String result = ""

switch (role) {

case 0:

switch (step) {

case 1:

result = getName() + " 获得砖块."

break

case 2:

result = getName() + " 转递砖块给" + nextRubPerson.getName() + "."

break

default:

break

}

break

case 1:

switch (step) {

case 1:

result = this.getName() + " 得到砖块从" + upRubPerson.getName()

+ "."

break

case 2:

result = getName() + " 转递砖块给" + nextRubPerson.getName() + "."

break

default:

break

}

break

case 2:

switch (step) {

case 1:

result = getName() + " 得到砖块从" + upRubPerson.getName() + "."

break

case 2:

result = getName() + " 卸掉砖块."

break

default:

break

}

break

default:

break

}

System.out.println(result)

return result

}

public String getBlock() {

return block

}

public synchronized void setBlock(String block) {

this.block = block

}

public boolean isRest() {

return isRest

}

public void setRest(boolean isRest) {

this.isRest = isRest

}

public RubPerson getNextRubPerson() {

return nextRubPerson

}

public void setNextRubPerson(RubPerson nextRubPerson) {

this.nextRubPerson = nextRubPerson

}

public RubPerson getUpRubPerson() {

return upRubPerson

}

public void setUpRubPerson(RubPerson upRubPerson) {

this.upRubPerson = upRubPerson

}

public int getPoint() {

return point

}

public void setPoint(int point) {

this.point = point

}

}

1、打开微信,点击页面下方的发现。

2、进入发现页面后,点击最下方的小程序选项。

3、进入小衡罩程序页面后,如果我们确定想添加搬砖小程序,可以点击页面右上方的搜索图标游拦梁。

4、选中自己想要添加的并长按。然后出现两个选项,添加到我的小程序和删除,点击添加到我的小程序。

5、加完小程序后,在最近使用中我们会发现这个小程神运序旁边会出现五角星标志。

6、然后我们点击我的小程序进入,就可以发现已经添加了这个小程序。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12446507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存