Итак, мне было поручено создать «Учебник по программированию», который может изменять размер в зависимости от того, на что установлена постоянная SIZE-переменная.
Вот что мне нужно снимать. (ПРИМЕЧАНИЕ. Нормальные книги SIZEd с нечетным числом имеют неловкий отступ к концу).Обратите внимание, что когда SIZE = 8, с каждой стороны текста по одному пробелу, 4 с каждой стороны по SIZE = 10 и т. Д.
Теперь вот что я запрограммировал до сих пор.Я почти закончил, единственное, что я не могу понять, - это алгоритм, который я могу использовать, чтобы поддерживать постоянный интервал для «метки» книги (текст «Построение программ Java»).В закомментированных строках «TO DO» я собираюсь поставить метку книги после того, как выясню интервал
(Примечание: сфокусируйтесь на методе bottomBook. Два других метода хороши)
package bookdrawing;
public class BookDrawing
{
public static final int SIZE = 8;
public static void main(String[] args)
{
topBook();
bottomBook();
}
public static void topBook()
{
//Drawing the VERY top of the book (before starting all the loops)
for(int i = 1; i <= SIZE + 1; i++)
{
System.out.print(" ");
}
drawLine();
System.out.println();
//Main portion. Looping to draw all spaces, '/'s, '__/'s, etc.
for(int i = 1; i <= SIZE; i++)
{
//front whitespaces
for(int j = 1; j <= -1 * i + (SIZE + 1); j++)
{
System.out.print(" ");
}
//Leftmost border
System.out.print("/");
//Inner whitespaces
for(int j = 1; j <= -3 * i + (SIZE * 3); j++)
{
System.out.print(" ");
}
//Additional "_" that starts off every row
System.out.print("_");
//"__/" pattern
for(int j = 1; j <= i; j++)
{
System.out.print("__/");
}
//Right side of book (/'s)
for(int j = 1; j <= i - 1; j++)
{
System.out.print("/");
}
//Move onto the next row of book drawing
System.out.println();
}
}
private static void drawLine()
{
System.out.print("+");
//This loop grows/shrinks line body depending on value of SIZE
for(int i = 0; i <= (SIZE * 3) - 1; i++)
{
System.out.print("-");
}
System.out.print("+");
}
public static void bottomBook()
{
//Dash line on top of the bottom portion of the book
drawLine();
//Printing first set of rightmost "/"'s
for(int i = 1; i <= SIZE; i++)
System.out.print("/");
System.out.println();
for(int i = 1; i <= SIZE / 2; i++)
{
//Leftmost pipe
System.out.print("|");
// TO DO: Code label of book
// for(int j = 1; j <= ; j++)
// {
//
// }
//Rightmost pipe
System.out.print("|");
//"Pages" to right of label
for(int j = 1; j <= -2 * i + (SIZE + 2); j++)
{
System.out.print("/");
}
//Move to draw next row
System.out.println();
}
//Dash line on very bottom of entire drawing
drawLine();
}
}
И вот как выглядит мой текущий вывод. (Алгоритм прямой косой черты правильный, все, что мне нужно вставить, - это метка с правильным интервалом)
ИтакКакой будет хороший алгоритм, чтобы интервал между метками соответствовал всему остальному?