import java.util.Scanner ;
public class printH
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in) ;
System.out.print("Please enter the height of H: ") ;
int height = in.nextInt() ;
System.out.println();
System.out.println();
int heightThird = findThird(height);
for (int i = 0; i < heightThird; i++)
{
printTopAndBottom(heightThird);
}
for (int j = 0; j<= 1; j++)
{
printMidH(height, heightThird);
}
for (int i = 0; i < heightThird; i++)
{
printTopAndBottom(heightThird);
}
}
public static int findThird(int height3)
{
if(height3 >= 4)
{
height3 = (height3 + 2)/3 ;
}
return height3 ;
}
public static void printTopAndBottom(int spacingH)
{
String line = "";
for (int j = 0; j < spacingH; j++)
{
String currentChar = j % 2 == 0 ? "h" : " ";
for (int i = 0; i < spacingH; i++)
{
line += currentChar;
}
}
System.out.print(line + "\n");
}
public static int printMidH(int wide, int finalHeight)
{
for (int j = 0; j<= 1; j++)
{
System.out.print("h") ;
for(int i = 0; i<= wide; i++)
{
System.out.print("h") ;
}
System.out.println();
}
return wide ;
}
}
我的代碼執行以下操作:得到錯誤輸出與某些輸入
1)findThird,給定的4個或更多的字母H的總高度的整數值,它舍入值到最接近3,然後計算並返回一個整數,該整數是圓整高度的三分之一
2)printTopAndBottom,它給出三分之一的圓形高度值打印h的頂部,左邊,中間和右邊的寬度oh的右邊部分,每個都是由firdThird方法確定的H的高度的三分之一。這再次用於打印底部
3),該給定的圓形高度值打印的三分之一H的中間部分一樣寬的圓形高度和一樣高圓角高度的三分之一printMidH
與輸入10其輸出(在我的節目MIDH匹配了頂部和底部的結束只是這裏不是計算器)當我輸入13例如,我得到下面的輸出然而
Please enter the height of H: 10
hhhh hhhh
hhhh hhhh
hhhh hhhh
hhhh hhhh
hhhhhhhhhhhh
hhhhhhhhhhhh
hhhhhhhhhhhh
hhhhhhhhhhhh
hhhh hhhh
hhhh hhhh
hhhh hhhh
hhhh hhhh
:(這看起來不正確)
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh
hhhhh hhhhh hhhhh