【问题标题】:Issue displaying a letter made of asterisks显示由星号组成的字母的问题
【发布时间】:2015-09-19 02:37:00
【问题描述】:

我想显示由星号组成的字母A。首先,程序会询问字母的大小,并根据给定的大小将字母缩放为该大小。

这是我到目前为止所拥有的,但我不确定我做错了什么。如您所见,它只打印一行。任何帮助都会很棒。

代码:

import java.util.Scanner;
public  class Problem7 {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        int input, size, i;

        System.out.print("Enter a size: ");
        input = kbd.nextInt();

        while (input <= 1) {
            System.out.print("Enter a size: ");
            input = kbd.nextInt();
        }
        for (int col = 0; col < input; col++) {
            for (int row = 0; row < input; row++) {
                if (col == 1 || col == input || row == col)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
        }
    }
}

结果:

Enter a size: 5
*    *****  *     *     *

【问题讨论】:

  • 你认为你的代码应该如何工作,你为什么这么认为?
  • 当您需要在下一行打印时,使用System.out.println() 或在要打印的字符串中添加\n,或在System.out.printf() 中使用%n
  • 看起来你需要切换你的 for 循环的顺序,在你完成行之后你需要 system.out.println()。
  • 所以在我的第一个 for 循环之后我应该有 system.out .print ("*") ?

标签: java letters


【解决方案1】:

如果你想要一个新行,只需使用:

System.out.print("\n");

【讨论】:

  • 这将是两个换行符。
  • ln后缀表示该方法会自动添加行分隔符。使用print("\n")println()
  • 尝试将 ln 放入 println 但它现在只打印在一列中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
相关资源
最近更新 更多