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("*"); }
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: ************
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.