How to do this for loop properly?

20 Views Asked by At

How do I do this properly for loop properly?

Ask the user for their name. Ask the user for the number of times to print their name and save this to an int variable called n. Create a for loop that loops from 0 to n. Within the loop, print the user’s name

This is what I did. I can't seem to get it to save it to the variable n and print out the user's name the amount of times that I input.

import java.util.Scanner;

public class MoreTesting {

public static void main(String[] args) {

   Scanner keyboard = new Scanner(System.in);

   int n;
   String name;

    System.out.println("What is your name?");
    name = keyboard.next();

    System.out.println("How many times do you want to print " +
                       "your name? Enter the value 1-20");
    n = keyboard.nextInt();



   for (n = 1; n < 20; n++)
       System.out.println(name + n);
1

There are 1 best solutions below

0
Niko1x On

Corrected for loop -->

for (i = 0; i<n; i++) System.out.println(name);