创建一个类,并为STARS程序编写方法

时间:2016-02-05 18:25:19

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

我有一个编码介绍“星星”程序方法的作业。我已经找到所有的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 " ";

    }

1 个答案:

答案 0 :(得分:0)

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的返回类型,因为你显然返回一个空字符串。