如何同时运行扫描仪和其他功能?

时间:2019-07-04 18:12:07

标签: java

我正在制作一款带有“ Dillo”的游戏,随着时间的推移,它会随着饥饿功能逐渐减轻体重。但是,只要能“增加”重量,玩家就可以输入数字。如果重量过高,则死亡。如果小腿的健康状况太差,它也会死亡。问题出在我试图让扫描仪收听,同时还允许“饥饿”功能降低其运行状况时。但是,当到达“ input.nextInt()”行时,它将停止直到得到一个数字。

import java.util.Scanner;

public class Main {

    static Scanner input = new Scanner(System.in);
    public static Dillo arma = new Dillo("Arma", 50, false);

    public static void main(String[] args) {

        while (true) {
            arma.stats();
            arma.hunger();
            int food = input.nextInt();
            arma.feed(food);
            arma.dead();
        }
    }
}
public class Dillo {
    private double weight;
    private boolean isDead;
    private String name;

    public Dillo(String name, double weight, boolean isDead) {
        this.name = name;
        this.weight = weight;
        this.isDead = isDead;
    }

    public double getWeight() {
        return this.weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void stats() {
        System.out.println(name + " is " + weight + " pounds!");
    }

    public void feed(int foodAmnt) {
        this.weight += foodAmnt;
        System.out.println(name + " has grown by " + foodAmnt + " pounds!");
        System.out.println(name + " is now " + weight + " pounds!");
    }

    public void dead() {
        if (this.weight >= 75) {
            isDead = true;
            System.out.println(this.name + " is too fat and died!");
            System.exit(0);
        } else if (this.weight <= 25) {
            isDead = true;
            System.out.println(this.name + " is too skinny and died!");
            System.exit(0);
        }
    }

    public void hunger() {
        this.weight -= 1;
        System.out.println(this.weight);
    }

}

1 个答案:

答案 0 :(得分:1)

看看,我已经使用多线程修改了您的代码,它现在可以按照您的想法运行。


代码如下:

 package exercise.dillo;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    static Scanner input = new Scanner(System.in);
    public static Dillo arma = new Dillo("Arma", 50, false);

    public static void main(String[] args) {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.execute(new DilloTask(arma));
        while (true) {
            // arma.stats();
            // arma.hunger();
            int food = input.nextInt();
            arma.feed(food);
            // arma.dead();
        }
    }
}

class DilloTask implements Runnable {
    private Dillo dillo;
    private Random rand = new Random();

    public DilloTask(Dillo dillo) {
        this.dillo = dillo;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                TimeUnit.MILLISECONDS.sleep(1000 + rand.nextInt(1000));
                dillo.stats();
                dillo.hunger();
                dillo.dead();
            }
        } catch (InterruptedException e) {
            System.out.println("DilloTask is interrupted");
        }

    }

}

和Dillo类一样,只需使用synced关键字进行一些更改。

   package exercise.dillo;

public class Dillo {
    private double weight;
    private boolean isDead;
    private String name;

    public Dillo(String name, double weight, boolean isDead) {
        this.name = name;
        this.weight = weight;
        this.isDead = isDead;
    }

    public double getWeight() {
        return this.weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }

    public synchronized void  stats() {
        System.out.println(name + " is " + weight + " pounds!");
    }

    public synchronized void feed(int foodAmnt) {
        this.weight += foodAmnt;
        System.out.println(name + " has grown by " + foodAmnt + " pounds!");
        System.out.println(name + " is now " + weight + " pounds!");
    }

    public synchronized void dead() {
        if (this.weight >= 75) {
            isDead = true;
            System.out.println(this.name + " is too fat and died!");
            System.exit(0);
        } else if (this.weight <= 25) {
            isDead = true;
            System.out.println(this.name + " is too skinny and died!");
            System.exit(0);
        }
    }

    public synchronized void hunger() {
        this.weight -= 1;
        System.out.println(this.weight);
    }

}

现在的结果是这样的:

  

Arma is 50.0 pounds!
49.0
Arma is 49.0 pounds!
48.0
Arma is 48.0 pounds!
47.0
Arma is 47.0 pounds!
46.0
10Arma is 46.0 pounds!
45.0

Arma has grown by 10 pounds!
Arma is now 55.0 pounds!
Arma is 55.0 pounds!
54.0
....
    Arma is 37.0 pounds!
36.0
-2
Arma has grown by -2 pounds!
Arma is now 34.0 pounds!
Arma is 34.0 pounds!
33.0
-Arma is 33.0 pounds!
32.0
3
Arma has grown by -3 pounds!
Arma is now 29.0 pounds!
Arma is 29.0 pounds!
28.0
Arma is 28.0 pounds!
27.0
Arma is 27.0 pounds!
26.0
Arma is 26.0 pounds!
25.0
Arma is too skinny and died!
  

我使用Random类来无限地增加饥饿时间,您可以取消或重置    饥饿时间。此示例使用两个线程,一个是主线程,另一个是主线程   运行DilloTask,因此当您输入数字时不会阻塞过程。