Java Solving a Java Problem: Countdown Rocket

  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Java Rocket
AI Thread Summary
The discussion revolves around a Java programming problem where the task is to write code that prints a countdown from a user-defined number down to one, followed by "Blastoff!". The original code provided by the user contained errors in the loop structure and logic. The correct approach involves using a for loop that counts down from the user-defined number to one, printing each number on a new line, followed by "Blastoff!" after the countdown. The solution emphasizes that the return statement at the end of the main method is unnecessary. The user expresses gratitude for the assistance received in resolving the issue.
lypena35
Messages
18
Reaction score
0
Hello,
I am stuck on a Java problem in my textbook. Can anyone help me? I have tried many variations to no avail thank you.

Question:Write code that prints: userNum ... 2 1 Blastoff! Your code should contain a for loop. Print a newline after each number and after Blastoff!. Ex: userNum = 3 outputs:
3
2
1
Blastoff!

My code:import java.util.Scanner;

public class CountdownRocket {
public static void main (String [] args) {
int userNum = 0;
int i = 0;

userNum = 3;

for(i=0;i<=userNum;--i){
System.out.println(userNum);
if(i==0){
System.out.println("Blastoff!");
}
}

return;
}
}
 
Technology news on Phys.org
The loop should be like this.

Code:
    for (i = userNum; i > 0; --i){
      System.out.println(i);
      if (i == 1) {
        System.out.println("Blastoff!");
      }
    }

Having [m]return;[/m] at the end of [m]main()[/m] is not necessary.
 
That worked thank you very much! I really appreciate the help!
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top