Metody w języku Java cd…

Kontynuując, możemy również przypisać zmienną do wyniku metody. W tym celu:

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score= 800;
        int levelCompleted = 5;
        int bonus = 100;

        int highScore = calculateScore(gameOver, score,levelCompleted,bonus);
        System.out.println("Your final score was " + highScore);
        score = 10000;
        levelCompleted=8;
        bonus=200;

        highScore = calculateScore(gameOver, score,levelCompleted,bonus);
        System.out.println("Your final score was " + highScore);

    }

    public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            return finalScore;
        }
        return -1;
    }
}

Przekazujemy Javie, by wartość, która jest zwracana z metody calculateScore została umieszczona w zmiennej zwanej highScore. Rezultatem powyższego kodu jest:

Your final score was 3300
Your final score was 13600.

Wynik poniższej metody został zwrócony automatycznie do metody calculateScore, która została przypisana do zmiennej highScore. A więc wartość, którą wracamy ( w naszym przypadku finalScore) zostaje odesłana i umieszczona w zmiennej highScore.

public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

    if (gameOver) {
        int finalScore = score + (levelCompleted * bonus);
        finalScore+=2000;
        return finalScore;
    }
    return -1;
}

Zadanie.

Utwórz metodę o nazwie displayHighScorePosition z dwoma parametrami:”player name” i  „highScorePosition”. Pierwszy parametr ma się wyświetlać z informacją „managed to get into position” a drugi parametr ma się wyświetlać z informacją „on the high score table”.

Utwórz drugą metodę o nazwie calculateHighScorePosition z jednym parametrem „playerScore” ,która ma zwracać „int” w zależności:

1 jeśli wynik jest >=1000;

2 jeśli wynik jest >= 500   a mniejszy niż 1000;

3 jeśli wynik jest >= 100 a mniejszy niż 500

4 w każdym innym przypadku;

wyświetl wyniki dla:  1500, 900, 400, 50, 1000.

Rozwiązanie:

-tworzenie metody displayHighScorePosition z dwoma parametrami: playerName  ( będzie to jakaś nazwa- ciąg znaków, więc typem danych będzie „String”) i highScorePosition ( będzie to liczba więc typ danych przyjmujemy „int”). Pierwszy parametr ma się wyświetlać z informacją „managed to get into position” a drugi parametr ma się wyświetlać z informacją „on the high score table”. W tym celu:

public static void displayHighScorePosition (String playerName, int highScorePosition){
System.out.println(playerName + "managed to get into position" 
+ highScorePosition + "on the high score table");

}

-tworzenie metody calculateHighScorePosition:

public static int calculateHighScorePosition (int playerScore){
    if (playerScore>=1000){
        return 1;
    }else if (playerScore>=500 && playerScore<1000){
        return 2;
    }else if (playerScore>=100 && playerScore<500){
        return 3;
    }else {
        return  4;
    }

-należy również przywołać metody displayHighScorePosition i calculateHighScorePosition przed ich parametrami. W związku z tym cały kod wygląda następująco:

public class Main {

    public static void main(String[] args) {

        int highScorePosition = calculateHighScorePosition(1500);
        displayHighScorePosition("Tim", highScorePosition);

        highScorePosition = calculateHighScorePosition(900);
        displayHighScorePosition("Bob", highScorePosition);

        highScorePosition = calculateHighScorePosition(400);
        displayHighScorePosition("Peter", highScorePosition);

        highScorePosition = calculateHighScorePosition(50);
        displayHighScorePosition("John", highScorePosition);
        
        highScorePosition = calculateHighScorePosition(1000);
        displayHighScorePosition("George", highScorePosition);

cały kod wygląda następująco:

public class Main {

    public static void main(String[] args) {

        int highScorePosition = calculateHighScorePosition(1500);
        displayHighScorePosition("Tim", highScorePosition);

        highScorePosition = calculateHighScorePosition(900);
        displayHighScorePosition("Bob", highScorePosition);

        highScorePosition = calculateHighScorePosition(400);
        displayHighScorePosition("Peter", highScorePosition);

        highScorePosition = calculateHighScorePosition(50);
        displayHighScorePosition("John", highScorePosition);

        highScorePosition = calculateHighScorePosition(1000);
        displayHighScorePosition("George", highScorePosition);

    }

    public static void displayHighScorePosition (String playerName, int highScorePosition){
        System.out.println(playerName + " managed to get into position "
                + highScorePosition + " on the high score table");

    }
    public static int calculateHighScorePosition (int playerScore){
        if (playerScore>=1000){
            return 1;
        }else if (playerScore>=500 && playerScore<1000){
            return 2;
        }else if (playerScore>=100 && playerScore<500){
            return 3;
        }else {
            return  4;
        }
    }
    }




 

a w oknie konsoli po uruchomieniu powyższego kodu wyświetla się:

Tim managed to get into position 1 on the high score table
Bob managed to get into position 2 on the high score table
Peter managed to get into position 3 on the high score table
John managed to get into position 4 on the high score table
George managed to get into position 1 on the high score table