How to number each line in the output using a for loop?

Mona Ramesh
import java.util.Scanner;

public class LoveCS
{
    public static void main(String[] args) 
    {
        int noTimesPrinted;

        Scanner scan = new Scanner(System.in);

        System.out.print("How many times should the message be printed: ");
        noTimesPrinted = scan.nextInt();

        int count = 1;
        while (count <= noTimesPrinted) 
        {
            System.out.println(" I love Computer Science!!");
            count++;
        }
    }
}
  1. Instead of using constant LIMIT, ask the user how many times the message should be printed. You will need to declare a variable to store the user’s response and use that variable to control the loop. (Remember that all caps is used only for constants!)

I HAVE COMPLETED PART1. I AM STUCK ON PART2. How do I get the number sequence??

  1. Number each line in the output, and add a message at the end of the loop that says how many times the message was printed. So if the user enters 3, your program should print this:

     1 I love Computer Science!!
     2 I love Computer Science!!
     3 I love Computer Science!!
     Printed this message 3 times.
    
  2. If the message is printed N times, compute and print the sum of the numbers from 1 to N. So for the example above, the last line would now read:

    Printed this message 3 times. The sum of the numbers from 1 to 3 is 6.
    

    Note that you will need to add a variable to hold the sum.

Elliott Frisch

You have a few choices. You could use String concatenation,

int count = 1;
while (count <= noTimesPrinted) 
{
    System.out.println(Integer.toString(count) + " I love Computer Science!!");
    count++;
}
System.out.println("Printed this message " + noTimesPrinted + " times");

Or with printf and (since you said you wanted a for loop) something like

for (int count = 1; count <= noTimesPrinted; count++) {
    System.out.printf("%d I love Computer Science!!%n", count);
}
System.out.printf("Printed this message %d times%n", noTimesPrinted);       

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to loop and keep the output of each loop in r

From Dev

Read lines from a file and output each line with a line number

From Dev

Read lines from a file and output each line with a line number

From Dev

How to add line number into each line?

From Dev

How to pass each line of an output file as an argument to a for loop in the same bash script?

From Dev

Minimum number and average number using for loop and not for each loop

From Dev

How to create an output with the number of observations in each row?

From Dev

loop through array, output each item to new line

From Dev

How to output a variable after each loop?

From Dev

grep loop: I'm using each line of one file as query to find matches another file. Why is my output inconsistent?

From Dev

How to replace line number delimiter in grep output

From Dev

How to replace line number delimiter in grep output

From Dev

How to add number to beginning of each line?

From Dev

How to specify number of characters in each line in python?

From Dev

How to count the number of characters on each line in vim?

From Dev

How to count the number of appearances of a word in each line

From Dev

How to append number at the beginning of each line [PHP]?

From Dev

How to count the number of a specific character in each line?

From Dev

How to show only the last output line of a loop

From Dev

How to get output line by line using xlsWriterr

From Dev

How to add a new line after each loop?

From Dev

Python - For Loop That Prints Each Number And Its Square on a Separate Line

From Dev

How to modify grep output: get each result in each line

From Dev

How to assign line number to a variable in a while loop

From Dev

How to Populate List of Starting Positions of Each Line Using For-loop and Tell Function?

From Dev

How to Populate List of Starting Positions of Each Line Using For-loop and Tell Function?

From Dev

How do I print the line number of each line in a string?

From Dev

How do I print the line number with each matched line?

From Dev

How do I print the line number with each matched line?

Related Related

  1. 1

    How to loop and keep the output of each loop in r

  2. 2

    Read lines from a file and output each line with a line number

  3. 3

    Read lines from a file and output each line with a line number

  4. 4

    How to add line number into each line?

  5. 5

    How to pass each line of an output file as an argument to a for loop in the same bash script?

  6. 6

    Minimum number and average number using for loop and not for each loop

  7. 7

    How to create an output with the number of observations in each row?

  8. 8

    loop through array, output each item to new line

  9. 9

    How to output a variable after each loop?

  10. 10

    grep loop: I'm using each line of one file as query to find matches another file. Why is my output inconsistent?

  11. 11

    How to replace line number delimiter in grep output

  12. 12

    How to replace line number delimiter in grep output

  13. 13

    How to add number to beginning of each line?

  14. 14

    How to specify number of characters in each line in python?

  15. 15

    How to count the number of characters on each line in vim?

  16. 16

    How to count the number of appearances of a word in each line

  17. 17

    How to append number at the beginning of each line [PHP]?

  18. 18

    How to count the number of a specific character in each line?

  19. 19

    How to show only the last output line of a loop

  20. 20

    How to get output line by line using xlsWriterr

  21. 21

    How to add a new line after each loop?

  22. 22

    Python - For Loop That Prints Each Number And Its Square on a Separate Line

  23. 23

    How to modify grep output: get each result in each line

  24. 24

    How to assign line number to a variable in a while loop

  25. 25

    How to Populate List of Starting Positions of Each Line Using For-loop and Tell Function?

  26. 26

    How to Populate List of Starting Positions of Each Line Using For-loop and Tell Function?

  27. 27

    How do I print the line number of each line in a string?

  28. 28

    How do I print the line number with each matched line?

  29. 29

    How do I print the line number with each matched line?

HotTag

Archive