在java中应用lambda表达式

时间:2016-05-17 02:57:31

标签: java multithreading lambda

这是我的代码。我还不熟悉java 8中的lambda表达式。

我想在这里应用一个lambda表达式来随机生成健康和不健康的马。 然后,我将打印并仅运行健康的马匹。我怎么能这样做?

import java.util.Scanner;
import java.util.Random;


public class HorseRace {
    static int numHorse = 0;
    static int healthyHorse = 0;

    public static void main(String[] args) {
        //int unhealthyHorse = 0;
        Random randomGenerator = new Random();
        Scanner input = new Scanner(System.in);
        int counter = 0;

        do {
            System.out.print("Enter number of horses: ");
            while (!input.hasNextInt()) {
                input.next();
            }
            numHorse = input.nextInt();
        } while (numHorse < 2);

        input.nextLine();

        Horse[] horseArray = new Horse[numHorse];

        while (counter < horseArray.length) {

            System.out.print("Name of horse " + (counter + 1) + ": ");
            String horseName = input.nextLine();
            String warCry = "*****************" + horseName + " says Yahoo! Finished!";

            int healthCondition = randomGenerator.nextInt(2);
            if (healthCondition == 1) {
                horseArray[counter] = new Horse(warCry);
                horseArray[counter].setName(horseName);
                System.out.println(horseArray[counter]);

                System.out.println(this);
                System.out.println(healthyHorse);
                //unhealthyHorse++;
            }
            counter++;
        }

        System.out.println(horseArray.length);
        System.out.println("...Barn to Gate...");

        for (int i = 0; i < healthyHorse; i++) {
            horseArray[i].start();

        }

    }

}

1 个答案:

答案 0 :(得分:0)

我重构了一些代码并尽可能使用了Lambda表达式。

public class HorseRace {

    public static void main(String[] args) {
        //int unhealthyHorse = 0;
        Random randomGenerator = new Random();
        Scanner input = new Scanner(System.in);
        int counter = 0;

        int numHorse;
        do {
            System.out.print("Enter number of horses: ");
            while (!input.hasNextInt()) {
                input.next();
            }
            numHorse = input.nextInt();
        } while (numHorse < 2);

        input.nextLine();

        List<Horse> horses = new ArrayList<>();

        while (counter < numHorse) {
            System.out.print("Name of horse " + (counter + 1) + ": ");
            String horseName = input.nextLine();
            String warCry = "*****************" + horseName + " says Yahoo! Finished!";
            int healthCondition = randomGenerator.nextInt(2);
            if (healthCondition == 1) {
                Horse horse = new Horse(warCry);
                horse.setName(horseName);
                horses.add(horse);
            }
            counter++;
        }

        horses.forEach(horse -> {
            System.out.println(horse);
            System.out.println(0);
        });

        System.out.println(horses.size());
        System.out.println("...Barn to Gate...");

        horses.forEach(Horse::start);
    }
}