229k views
1 vote
Write code that prints: Ready! firstNumber … 2 1 Go!

Your code should contain a for loop. Print a newline after each number and after each line of text.
Ex: If the input is:
3
the output is:
Ready!
3
2
1
Go! Java Please

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int firstNumber = input.nextInt();

System.out.println("Ready!");

for (int i = firstNumber; i >= 1; i--) {

System.out.println(i);

}

System.out.println("Go!");

}

}

Step-by-step explanation:

  1. We import the Scanner class to get input from the user.
  2. We declare a Scanner object called input.
  3. We use input.nextInt() to read the first number input by the user and assign it to the variable firstNumber.
  4. We print "Ready!" using System.out.println().
  5. We use a for loop that starts from firstNumber and decrements by 1 until it reaches 1.
  6. Inside the for loop, we print each number using System.out.println().
  7. After the for loop, we print "Go!" using System.out.println().

User Muhammed Kashif
by
8.4k points