Java string method not returning string [duplicate]

非 Y 不嫁゛ 提交于 2020-01-24 19:03:41

问题


I am very new to coding and was just introduced to static methods, so I apologize in advance for the silly mistakes. The method should display a triangle when the method is called under main, but I am getting an empty console and there is no output. However, if I write this under main:

    String triangle = getTriangle(3, 4);    
    System.out.println(triangle);               

then, the triangle will be displayed in the console, but for this assignment, the string/triangle must be called by only using getTriangle(maxRows, maxCols)

public class Triangle {

    public static String getTriangle(int maxRows, int maxCols) {
        String T = "";

        if (maxRows < 1 || maxCols < 1) {
            return null;
        } else {
            for (int row = 1; row <= maxRows; row++) {
                for (int col = 1; col <= row; col++) {
                    T += "*";
                }
                T += "\n";  }   
            }
            return T;
        }
    }


    public static void main(String[] args) {
        getTriangle(3,2);           

    }

}

回答1:


You still need to print the result of getTriangle in your main method. Now you are just ignoring that result.

System.out.println(getTriangle(3,2));



回答2:


Make your method void and print the T at the end of the method.

public static void getTriangle(int maxRows, int maxCols) {    
    if (maxRows < 1 || maxCols < 1) {
        return; 
    }

    String T = "";

    for (int row = 1; row <= maxRows; row++) {
        for (int col = 1; col <= row; col++) {
            T += "*";
        }
        T += "\n";
    }

    System.out.println(T);
}

Since you no longer return a triangle, you can rename the method to printTriangle. Personally, I would throw an exception if the condition maxRows < 1 || maxCols < 1 holds true, but it's a different topic.




回答3:


First of all, you should try to make your code more readable by indenting it properly.
Isn't it a lot easier to read like this?

public class Triangle {

    public static String getTriangle(int maxRows, int maxCols) 
    {
        String T = "";

        if (maxRows < 1 || maxCols < 1) 
        {
            return null;

        } 
        else 
        {
            for (int row = 1; row <= maxRows; row++) 
            {
                for (int col = 1; col <= row; col++) 
                {
                    T += "*";
                }
                T += "\n";  
            }   
        }
        return T;
    }

    public static void main(String[] args) 
    {
        getTriangle(3,2);           
    }
}

And secondly and more important, as stated by others, your main method should go

public static void main(String[] args) 
{
    System.out.println(getTriangle(3,2));
}

You are receiving the string, you are just not outputting it




回答4:


you can add line System.out.println(T); before return T; to do so.




回答5:


You can print it right before your method returns it.

public static String getTriangle(int maxRows, int maxCols) {
    String T = "";

    if (maxRows < 1 || maxCols < 1) {
        return null;

    } else {

        for (int row = 1; row <= maxRows; row++) {
            for (int col = 1; col <= row; col++) {
                T += "*";
            }
            T += "\n";
        }
    }
    System.out.println(T); // Print the triangle
    return T;
}

(A tiny personal opinion)

After that, the method will return and print the triangle hence I would not name the method getTriangle. Maybe something like getAndPrintTriangle...



来源:https://stackoverflow.com/questions/58051454/java-string-method-not-returning-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!