Java Solving a Java Problem: Countdown Rocket

  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Java Rocket
Click For 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:
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 3 ·
Replies
3
Views
10K
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 0 ·
Replies
0
Views
501
  • · Replies 28 ·
Replies
28
Views
30K