Game add three numbers

The problem

Write a program to generate three single-digit integers and prompt the user to enter the sum of these three integers.

Breaking it down

Initializing a Random class and calling ints will create an infinite stream of values between the range of 0 and 9 per requirements. Stream.limit() will stop the stream at three elements and we will convert the stream to a list. To show the answer to the user we will want to reduce the stream or calculate the sum of the values. Next to pretty up the output, we will join all the elements and separated by a space by passing in Collectors.joining(). Finally we will ask the user for their answer and validate if they have the right answer by comparing values.

public static void main(String[] Strings) {

    Random random = new Random();
    List<Integer> randomValues = random.ints(0, 9).limit(3).boxed()

    int answer =;

    String numberOutput =
            .collect(Collectors.joining(" "));

    Scanner input = new Scanner(;
    System.out.print("What is " + numberOutput + "? ");
    int userAnswer = input.nextInt();

    System.out.println(numberOutput + " = " + userAnswer + " is "
            + (userAnswer == answer));


What is 3 6 3? 12
3 6 3 = 12 is true