Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Building a Server Retry Mechanism | Loops
Java Basics
course content

Kurssisisältö

Java Basics

Java Basics

1. Getting Started
2. Basic Types and Operations
3. Loops
4. Arrays
5. String

book
Challenge: Building a Server Retry Mechanism

Tehtävä

Swipe to start coding

Implement a program that attempts to connect to a server up to 5 times. If the connection is successful, the program prints a success message. If it fails after 5 attempts, it prints an error message.

  1. Create a method called tryConnect(int successfulAttempt) that returns a boolean indicating whether the connection succeeded.
  2. Inside this method:
    • Start a do-while loop that runs up to 5 times.
    • On each iteration, increment the retry counter.
    • If the retry count equals the given successfulAttempt, print a success message and return true.
    • If not, print "Server unavailable, retrying...".
  3. If the loop finishes without success, return false.
  4. In the main method, call tryConnect(...) with a test value and print "Failed to connect to the server after 5 attempts." if the result is false.

Ratkaisu

java

solution

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
toggle bottom row

book
Challenge: Building a Server Retry Mechanism

Tehtävä

Swipe to start coding

Implement a program that attempts to connect to a server up to 5 times. If the connection is successful, the program prints a success message. If it fails after 5 attempts, it prints an error message.

  1. Create a method called tryConnect(int successfulAttempt) that returns a boolean indicating whether the connection succeeded.
  2. Inside this method:
    • Start a do-while loop that runs up to 5 times.
    • On each iteration, increment the retry counter.
    • If the retry count equals the given successfulAttempt, print a success message and return true.
    • If not, print "Server unavailable, retrying...".
  3. If the loop finishes without success, return false.
  4. In the main method, call tryConnect(...) with a test value and print "Failed to connect to the server after 5 attempts." if the result is false.

Ratkaisu

java

solution

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt