首页 > 解决方案 > 如何调用字符串类型的方法?

问题描述

如何在main方法中调用这个方法?我无法在主方法中调用我的用户定义方法。怎么做?

public class Concat {

    public static void main(String[] args) {


    }

    public static String UserInfo (String name, String email, String address) { 
        name="vgfhk";
        email="dbdkjb";
        address="jbkug";
        String code="Thanks for running the code\n\n";
        String below="Your information is given below:\n";

        String thanksMsg = "Hello, "+name+code+below;

        String userInformation="Name: "+name+"email: "+email+"Address: "+address;

        return thanksMsg.concat(userInformation);   
    } 
}

标签: java

解决方案


您可以简单地调用已将其定义为静态的函数。我已将输入值作为 Null 传递,您可以根据需要进行替换。

public class Concat {

    public static void main(String[] args) {

        String result = userInfo(null, null, null);

    }

    public static String userInfo (String name, String email, String address) { 
        name="Farhan";
        email="dbdkjb";
        address="mirpur";
        String code="Thanks for running the code\n\n";
        String below="Your information is given below:\n";

        String thanksMsg = "Hello, "+name+code+below;

        String userInformation="Name: "+name+"email: "+email+"Address: "+address;

        return thanksMsg.concat(userInformation);   
    } 
}

注意 :您的方法名称不遵循 java 编码约定。您应该遵循有关它的 Oracle 指南。https://www.oracle.com/technetwork/java/codeconventions-135099.html


推荐阅读