Function write underlined Learn programming Java

Lesson:

Functions


Exercise:

Function write underlined 16


Objetive:

Create a function able to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters) and then underline it (writing several hyphens under that word):

WriteUnderlined("Hello!");


Code:

public class Main
{
	public static void WriteUnder(String text)
	{
		int countSpaces = (80 - text.length()) / 2;
		int i = 0;

		for (; i < countSpaces; i++)
		{
			System.out.print(" ");
		}

		System.out.println(text);

		for (i = 0; i < countSpaces; i++)
		{
			System.out.print(" ");
		}

		for (i = 0; i < text.length(); i++)
		{
			System.out.print("_");
		}
	}

	public static void main(String[] args)
	{
		WriteUnder("Hello");
	}
}