簡體   English   中英

在java中應用lambda表達式

[英]applying lambda expression in java

這是我的代碼。我還不熟悉 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();

        }

    }

}

我已經重構了一些代碼,並盡可能使用了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);
    }
}

Lambda 表達式的使用如下:

在此處輸入圖像描述 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM