Write Java for 2 questions. (Zybook6.4) 1 Write a while loop that prints 1 to us
ID: 3579183 • Letter: W
Question
Write Java for 2 questions. (Zybook6.4)
1
Write a while loop that prints 1 to userNum, using the variable i. Follow each number (even the last one) by a space. Assume userNum is positive. Ex: userNum = 4 prints:
import java.util.Scanner;
public class CountDown {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
userNum = 4; // Assume positive
/* Your solution goes here */
System.out.println("");
return;
}
}
2
Re-type the following and run, note incorrect behavior. Then fix errors in the code, which should print numStars asterisks.
import java.util.Scanner;
public class StarPrinter {
public static void main (String [] args) {
int numStars = 0;
int numPrinted = 0;
numStars = 12;
numPrinted = 1;
/* Your solution goes here */
System.out.println("");
return;
}
}
Explanation / Answer
CountDown.java
import java.util.Scanner;
public class CountDown {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
userNum = 4; // Assume positive
/* Your solution goes here */
while(i<userNum){
i = i + 1;
System.out.print(i+" ");
}
System.out.println("");
return;
}
}
Output:
1 2 3 4
StarPrinter.java
import java.util.Scanner;
public class StarPrinter {
public static void main (String [] args) {
int numStars = 0;
int numPrinted = 0;
numStars = 12;
numPrinted = 1;
/* Your solution goes here */
while (numPrinted != numStars+1) {
System.out.print("*");
numPrinted++;
}
System.out.println("");
return;
}
}
Output:
************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.