Function write centered Learn programming Java

Lesson:

Functions


Exercise:

Function write centered


Objetive:

Create a function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters):

WriteCentered("Hello!");


Code:

public class WriteCentered
{
	public static void WriteCentered(string text)
	{
		int i = 0;

		for (; i < 17; i++)
		{
			System.out.println();
		}

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

		System.out.print(text);

		for (i = 0; i < 14; i++)
		{
			System.out.println();
		}
	}

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

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.