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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
577
  • · Replies 28 ·
Replies
28
Views
30K