[英]how to take a value main method and use it in different method
基本上,我想使用将值放入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" ));
}
您需要在所需的方法中使用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();
}
通过执行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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.