从另一个类调用类方法时出错

问题描述 投票:0回答:1

我正在尝试调用一个名为 drawPyramid 的类方法,该方法位于另一个名为 Main 的类中名为 Drawer 的类中,但出现以下错误:Main.java:38: error: cannot find symbol Drawer.drawPyramid(value);

主课

package dsaa.lab01;

import java.util.Scanner;

public class Main {

    static Scanner scan;

    /***
     * commands:
     * py size
     *   draw a pyramid with size
     * ct size
     *   draw a christmas tree with size
     * ld documentName
     *   load document from standard input line by line. Last line consists of only sequence "eod",
     *   which means end of document
     * ha
     *   halt program and finish execution
     * @param args
     */
    

    public static void main(String[] args) {
        System.out.println("START");
        scan=new Scanner(System.in);
        boolean halt=false;
        while(!halt) {
            String line=scan.nextLine();
            // empty line and comment line - read next line
            if(line.length()==0 || line.charAt(0)=='#')
                continue;
            // copy line to output (it is easier to find a place of a mistake)
            System.out.println("!"+line);
            String word[]=line.split(" ");
            if(word[0].equalsIgnoreCase("py") && word.length==2) {
                int value=Integer.parseInt(word[1]);
                Drawer.drawPyramid(value);
                continue;
            }
            if(word[0].equalsIgnoreCase("ct") && word.length==2) {
                int value=Integer.parseInt(word[1]);
                Drawer.drawChristmassTree(value);
                continue;
            }
            // ld documentName
            if(word[0].equalsIgnoreCase("ld") && word.length==2) {
                Document.loadDocument(word[1],scan);
                continue;
            }
            // ha
            if(word[0].equalsIgnoreCase("ha") && word.length==1) {
                halt=true;
                continue;
            }
            System.out.println("Wrong command");            
        }
        System.out.println("END OF EXECUTION");
        scan.close();

    }




}

抽屉类

package dsaa.lab01;

public class Drawer {
    private static void drawLine(int n, char ch) {
        
        for(int i = 1; i <= n; i++)
        {
            System.out.print(ch);
        }
    }


    public static void drawPyramid(int n) {

        for(int i = 1; i <= n; i++)
        {
            drawLine(n-i, '.');
            drawLine(2*i-1, 'X');
            drawLine(n-i, '.');

            System.out.println();
        }
    }

    public static void drawChristmassTree(int n) {
        System.out.println("Christmas Tree");
    }

}

我尝试了不同的方法,但它就是行不通,我希望 Main 类能够正确调用该方法。

java methods
1个回答
0
投票

请在主类中添加导入语句:

import dsaa.lab01.Drawer;
© www.soinside.com 2019 - 2024. All rights reserved.