【问题标题】:Creating a Class, and writing methods for the STARS program创建一个类,并为 STARS 程序编写方法
【发布时间】:2016-02-05 18:25:19
【问题描述】:

我有一个任务来编写介绍“Stars”程序的方法。我已经找到了所有的 for 循环来打印设计,但是用它们编写类是我被绊倒的地方。我对编程还是很陌生,我确信我的代码中有很多愚蠢的错误。

Here's the Document of the Stars output

public class STARS {
    Scanner scan = new Scanner(System.in);
    public int rows;
    public String part1, part2, part3, part4, part5;

    public STARS(int rows){
    }

    private static String part1(int rows){

        for (int i = 0; i < rows; i++) {
            for (int stars = 0; stars < rows; stars++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        return " ";
    }
    private static String part2(int rows){

        System.out.println("");
        for (int i = 1; i <= rows; i++) {
            for (int star = 1; star <= i; star++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        return " ";

    }

【问题讨论】:

  • 不清楚您的实际问题是什么,请您说得更清楚些好吗?

标签: java class for-loop data-structures return-type


【解决方案1】:
public class STARS{

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int rows = scan.nextInt();
    part1(rows);
    part2(rows)
}

private static String part1(int rows){

 for (int i = 0; i < rows; i++) {
    for (int stars = 0; stars < rows; stars++) {
        System.out.print("*");
    }
    System.out.println("");
 }
 return " ";
}

private static String part2(int rows){

    System.out.println("");
    for (int i = 1; i <= rows; i++) {
        for (int star = 1; star <= i; star++) {
            System.out.print("*");
        }
        System.out.println("");
    }
    return " ";
    }
}

您确实不需要方法 part1 和 part2 的返回类型,因为您显然返回的是一个空字符串。

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2021-09-25
    • 2014-07-25
    • 1970-01-01
    相关资源
    最近更新 更多