如何采用值main方法并在不同方法中使用它

时间:2013-11-20 22:55:49

标签: java

基本上,我想用一个值放入main并进行设置,这样我就可以使用输入main的单词/单词,这样我就可以使用代码了。

public static  String L33TLanguageSupport(String s) {
    Scanner scan =new Scanner (s);
    char o = 0;
    char O=0;
    char e=0, E=0, a=0, A= 0;
    return s
        .replace(o, (char) 0)
        .replace(O,(char) 0)
        .replace(e, (char) 3)
        .replace(E,(char) 3)
        .replace(a, (char)4)
        .replace(A, (char)4);
}

public static void main (String[] arg) {
    System.out.println(L33TLanguageSupport("cow life" ));
}

2 个答案:

答案 0 :(得分:1)

您需要使用所需方法中的Scanner读取用户输入,然后将结果检索到变量并将其发送到另一个方法。

改编自您发布的代码:

public static  String L33TLanguageSupport(String s) {
    //remove this from here
    //Scanner scan =new Scanner (s);
    //do what it must do...
}

public static void main (String[] arg) {
    //System.out.println(L33TLanguageSupport("cow life" ));
    //creating the scanner to read user input
    Scanner scanner = new Scanner(System.in);
    //showing a nice message to user
    System.out.print("Enter a word: ");
    //reading the user input (the whole line until user press Enter key)
    String input = scanner.readLine();
    //applying the method to user input
    String output = L33TLanguageSupport(input);
    //showing to user the result of the processing
    System.out.println("Result: " + output);
    //closing the scanner resources
    scanner.close();
}

答案 1 :(得分:0)

您可以通过执行java main cow life然后将这些参数传递给连接在一起的L33tLanguagesupport对象来获得相同的结果。

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for(String arg : args) sb.append(arg).append(" ");
    System.out.println(sb.toString().trim());
}