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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top