Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Building a Server Retry Mechanism | Loops
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
Challenge: Building a Server Retry Mechanism

Tarea

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.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
toggle bottom row

book
Challenge: Building a Server Retry Mechanism

Tarea

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.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Lamentamos que algo salió mal. ¿Qué pasó?
some-alt