输入错误的输出

时间:2016-06-28 18:50:08

标签: java

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,给定字母H的总高度为4或更大的整数值,它将值向上舍入到最接近的3的多数,然后计算并返回一个整数,即该舍入高度的三分之一< / p>

2)printTopAndBottom,给出圆形高度值的三分之一打印h的顶部,其中左边,中间和右边的部分的宽度为哦,每个都是由firdThird确定的H的高度的三分之一方法。这再次用于打印底部

3)printMidH给出了圆角高度值的三分之一,打印出H的中间部分,与圆形高度一样宽,高达圆角高度的三分之一

输入10输出:(在我的程序中,midH与顶端和底端匹配,而不是在stackoverflow上)

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

然而,当我输入13例如,我得到以下输出:(看起来不正确)

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

2 个答案:

答案 0 :(得分:0)

你在这里做错了:for (int j = 0; j < spacingH; j++)

而不是迭代heightThird,只迭代三次。

public static void printTopAndBottom(int spacingH) {
        String line = "";

        for (int j = 0; j < 3; j++) {
            String currentChar = j % 2 == 0 ? "h" : " ";
            for (int i = 0; i < spacingH; i++) {
                line += currentChar;
            }
        }
        System.out.print(line + "\n");
    }

printMidH方法还有一个更改:

调用方法heightThird次:

for (int j = 0; j < heightThird; j++) {
            printMidH(height, heightThird);
        }

并打印&#39; H&#39; finalHeight*3

public static int printMidH(int wide, int finalHeight) {

    for (int i = 0; i < finalHeight * 3; i++) {
        System.out.print("h");
    }
    System.out.println();
    return wide;
}

答案 1 :(得分:0)

关于右侧额外块的问题在于printTopAndBottom()方法的这些行。

for (int j = 0; j < spacingH; j++)
    String currentChar = j % 2 == 0 ? "h" : " ";

这是为每个偶数从0到每行的宽度绘制一条'h'。这意味着如果宽度为3,它将输出'h'的块。

[0] 1 [2]
[0][1][2]
[0] 1 [2]

但如果输出宽度为5,那么它将产生你得到的东西。

[0] 1 [2] 3 [4]
[0][1][2]
[0] 1 [2] 3 [4]

要解决此问题,只需替换此行

即可
for (int j = 0; j < spacingH; j++)

用这个

for (int j = 0; j < 3; j++)

经过一些测试,看起来您的代码也不正确地输出了水平线。在printMidH方法中,不应该在此行上方打印“h”

for(int i = 0; i<= wide; i++) {

另外,使用 wide 变量来绘制'h'的水平线是不准确的,因为实际宽度已被修改为最接近的3的倍数。用这个替换该行。

for (int i = 0; i < finalHeight * 3; i++) {
相关问题