Java字符串方法不返回字符串 [英] Java string method not returning string

查看:150
本文介绍了Java字符串方法不返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码非常陌生,只是被介绍给静态方法,因此我对这些愚蠢的错误表示歉意.在main下调用该方法时,该方法应显示一个三角形,但是我得到的是一个空控制台,并且没有输出.但是,如果我在main下写这个:

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);               

然后,三角形将显示在控制台中,但是对于此分配,必须仅通过使用来调用字符串/三角形 getTriangle(maxRows, maxCols)

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);           

    }

}

推荐答案

您仍然需要在main方法中打印getTriangle的结果.现在,您只是忽略了该结果.

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));

这篇关于Java字符串方法不返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆