【问题标题】:Method with one parameter to draw a cross用一个参数绘制十字的方法
【发布时间】:2014-11-11 18:40:01
【问题描述】:

所以我需要使用带有一个参数的方法来绘制一个十字,其大小由该参数决定。所以 drawCross(5) 将是:

  *
  *
*****
  *
  *

我似乎无法让它工作。我的代码会要求输入一个数字,但是什么也没有。我敢肯定,这可能只是我愚蠢,但我终生无法弄清楚它是什么。这是我所拥有的:

import java.util.Scanner;

public class Lab9E1CrossTest {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        System.out.print("Number: ");
        int n = kbd.nextInt();

        drawCross(n);

        kbd.close();
    }

    public static void drawCross(int n) {
        int lineCounter = 1, charCounter = 1;
        if (n % 2 == 1) {
            while (lineCounter <= n) {
                if (lineCounter == ((n / 2) + 1)) { // determines if middle line
                                                    // by dividing n by two then
                                                    // adding 1 (ex. middle of 5
                                                    // is 3, so 5/2=2, +1=3)
                    charCounter = 1;
                    while (charCounter <= n) { // prints out n number of stars
                                               // on one line
                        System.out.print("*");
                        charCounter++;
                    }
                } else {
                    charCounter = 1;
                    while (lineCounter != ((n / 2) + 1)) {
                        if (charCounter == ((n / 2) + 1)) { // if middle char of
                                                            // line
                            System.out.print("*");
                        } else {
                            System.out.print(" ");
                        }
                        charCounter++;
                    }
                }
                System.out.println(); // makes sure prints on new line
                lineCounter++;
            }
        } else {
            System.out.println("Error: Number is even.");
        }
    }
}

【问题讨论】:

  • 你试过调试你的程序吗?你尝试过哪些价值观?
  • 刚刚尝试调试,没有运气。我尝试了一些价值观。它会要求我输入一个数字,然后在屏幕上闪烁一个 *,然后继续运行但什么也不打印。
  • 你知道我说的调试是什么意思吗?放置一个断点并单步执行代码行以查看它的位置。这应该真的可以帮助您了解问题出在哪里。
  • 抓住了它。在第 30 行有一个无限循环,while(lineCounter != ((n/2)+1))。将其更改为 if 并在其下方添加 while(charCounter
  • 是的,我也发现了,实际上调试了你的代码:)

标签: java loops methods


【解决方案1】:

我将首先在您收到用户输入时验证它(例如,确保它甚至在您调用 drawCross 之前)。

public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);
    System.out.print("Number: ");
    if (kbd.hasNextInt()) {
        int n = kbd.nextInt();
        if (n % 2 != 0) {
            drawCross(n);
        } else {
            System.out.printf("Please enter an odd #, %d is even%n", n);
        }
    }
}

接下来,我建议您创建一个实用方法来重复特定字符n 次。然后你可以用重复的String(s) 来画你的十字架

private static String repeat(char ch, int n) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n; i++) {
        sb.append(ch);
    }
    return sb.toString();
}

类似的,

public static void drawCross(int n) {
    int mid = (n / 2);
    String spaces = repeat(' ', mid);
    for (int i = 0; i < mid; i++) {
        System.out.println(spaces + '*');
    }
    System.out.println(repeat('*', n));
    for (int i = 0; i < mid; i++) {
        System.out.println(spaces + '*');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多