Retype the following and run note incorrect behavior Then fi

Re-type the following and run, note incorrect behavior. Then fix errors in the code, which should print numStars asterisks.
  while (numPrinted != numStars) {     System.out.print(\"*\");      }   


\"Challe

Challe nge 4.4.2: Printing output using a counter Activity 4.4.2: P Re-type the following and run, note incorrect behavior. Then fix errors in the code, which should print numStars asterisks while (numPrinted != numStars) { System.out.print(\"*\"); 1 import java.util.scanner; 3 public class StarPrinter 4 public static void main (String ] args) int numStars 0; int numPrinted-0; 6 7 numStars = 12; numPrinted 1; / Your solution goes here / System.out.println(\"); return; 9 10 12 13 14 15 16 17 Run

Solution

import java.util.Scanner;

class Main {

public static void main(String[] args) {

int numStars = 0;

int numPrinted = 0;

numStars = 12;

numPrinted = 1;

// <= is to make sure that it runs from 1 to 12

while (numPrinted <= numStars) {

System.out.print(\"*\");

// this is to increment numPrinted by 1 each time

// otherwise it runs for infinite times

numPrinted++;

}

System.out.println(\"\");

}

}

// Output: ************

 Re-type the following and run, note incorrect behavior. Then fix errors in the code, which should print numStars asterisks. while (numPrinted != numStars) { Sy
 Re-type the following and run, note incorrect behavior. Then fix errors in the code, which should print numStars asterisks. while (numPrinted != numStars) { Sy

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site