A. Moore A. Moore - 13 days ago 5
Java Question

For loop to print consecutive numbers?

I am supposed to build a code that asks for the users name, asks for a number between one and ten until I get number within that range, and then prints the numbers from 1 to the number the user entered except every third number should be the user's name that was entered at the beginning of the program. For example if the number was 4 and the name is Tom the output will be:

1
2
Tom
4


I'm struggling to figure out how to do the third part. I would prefer to use a for loop. Can someone explain how I could do this?
Here's what (little) I have so far.

import java.util.Scanner;

/**
*
* @author Home
*/
public class NewClass {
public static void main(String[] args) {
Scanner scan = new Scanner( System.in);

System.out.print( "Enter your name: ");
String name = scan.nextLine();

System.out.print( "Please enter a number between 1 and 10: ");
number = scan.nextInt();

while ( number >= 1 && number <= 10)

Answer
    Scanner scan = new Scanner( System.in);

    System.out.print( "Enter your name: ");
    String name = scan.nextLine();

    System.out.print( "Please enter a number between 1 and 10: ");
    int number = scan.nextInt();

    while (number < 1 || number > 10) {
        System.out.print( "Please enter a number between 1 and 10: ");
        number = scan.nextInt();
    }

    for (int i = 1; i <= number; i++) {
        if (i % 3 == 0) {
            System.out.print(name + " ");
        } else {
            System.out.print(i + " ");
        }
    }