JAVA · ОСНОВЫ · LESSON 25

Final project: points

Array, loop, condition and method · about 20 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Gather familiar pieces

Create an array of glasses. The sum method adds all the numbers in a loop and returns the answer. In main, show the results.

int[] scores = {2, 3, 4};
System.out.println(sum(scores));

✦ You already know the array, the loop and the return.

STEP 2

Add a solution

After the amount, check the condition: if the points are enough, show a message of success. Construct a program of familiar little steps.

if (total >= 8) {
    System.out.println("Победа");
}
Outcome programme
MassiveglassesMethodsumifresult

✦ Here it is important to combine the parts into one program.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

public class Main {
    public static void main(String[] args) {
        int[] scores = {2, 3, 4};
        int total = sum(scores);
        System.out.println(total);
        if (total >= 8) {
            System.out.println("Победа");
        }
    }
    static int sum(int[] scores) {
        int total = 0;
        for (int score : scores) {
            total += score;
        }
        return total;
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Do the final program: int[] scores of 1, 2, 5; static int sum(Zint[] scores) folds them through for-each and returns the amount. In main, output total and, if total is at least 8, output «Готово».

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework