Loop Assignment Create a program called Loops.java. My program would be ConwayLo
ID: 3853396 • Letter: L
Question
Loop Assignment Create a program called Loops.java. My program would be ConwayLoops.java and my class name would be ConwayLoops. This program will contain two loops. All the input from the user should be from the command line (use the Scanner object). You only need to create one Scanner object to handle all the input. Do not use JOptionPane. Create a program that asks the user for a positive number. The program should continually loop until the user enters a positive number. Once the user enters a positive number (number greater than 0), the program will continue. (While loop would work well here!) The program then will ask the user for their name. The program should loop the number of times the user has entered and print their name this number of times. (For loop would work well here!) Example program run: > run ConwayLoops Enter a positive number: -343 Enter a positive number: 0 Enter a positive number: 5 Enter your name: Rascal RascalExplanation / Answer
ConveyLoops.java
import java.util.Scanner;
public class ConveyLoops {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = scan.nextInt();
while(n <= 0){
System.out.print("Enter a positive number: ");
n = scan.nextInt();
}
System.out.print("Enter your name: ");
String name = scan.next();
for(int i=0;i<n;i++){
System.out.println(name);
}
}
}
Output:
Enter a positive number: -343
Enter a positive number: 0
Enter a positive number: 5
Enter your name: Rascal
Rascal
Rascal
Rascal
Rascal
Rascal
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.