Question

using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...

using java

LO: (Analyze) Students will fix a loop that runs forever.


This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly.

starter code :

import java.util.Scanner;

/*
* Counts down to a blastoff, starting from a given number.
*/
public class Countdown {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int countdown = 0;
       countdown = sc.nextInt();
      
       while (countdown != 0) {
           System.out.println(countdown + "...");
           countdown--;
       }
       System.out.println("Blast off!");
       System.exit(0);
   }
}

Homework Answers

Answer #1
import java.util.Scanner;

/*
* Counts down to a blastoff, starting from a given number.
*/
public class Countdown {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int countdown = 0;
       countdown = sc.nextInt();
      
       while (countdown > 0) {
           System.out.println(countdown + "...");
           countdown--;
       }
       System.out.println("Blast off!");
       System.exit(0);
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students...
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students will write loops that iterate as long as a condition is true. Write a program that reads words from standard input and displays them to standard output. When the word "end" is read, print it out and then stop reading input. starter code: import java.util.Scanner; /* * Reads and displays words from standard input until hitting a stop word. */ public class WhileLoop {...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. code below import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ReadingFiles { public static void main(String[] args) throws FileNotFoundException { System.out.println(totalEven); } }
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Complete the program below. The program takes in two positive integers, n and m, and should...
Complete the program below. The program takes in two positive integers, n and m, and should print out all the powers of n that are less than or equal to m, on seperate lines. Assume both n and m are positive with n less than or equal to m. For example, if the input values are n = 2 and m = 40 the output should be: 2 4 8 16 32 Starter code: import java.util.Scanner; public class Powers {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT