Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re-type the following and run, note incorrect behavior. Then fix errors in the c

ID: 3923755 • Letter: R

Question

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 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

Explanation / Answer

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: ************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote