所以,我有两个类:Java的级联通过CMD输入
class ConcatTesting{
public static void main(String args[]) throws java.io.IOException{
char inLetter;
String input="";
//This loops takes line of cmd and makes the input variable into that string
for(;;){
inLetter=(char) System.in.read(); //get next char
//if the line hasn't ended then add that char to input
if(inLetter!='\n'){
input+=String.valueOf(inLetter);
}else{
//other wise line has ended so input is finished
break;
}
}
//removes extra white-spaces
input.trim();
//test what input is to make sure it is working correctly
System.out.println(input);
//test concat function
UseConcat.ask(input);
UseConcat.ask("pie");
}
}
class UseConcat{
public static void ask(String str){
System.out.println("What does " + str +" mean?");
}
}
在节目中我调用静态方法UseConcat.ask(String str)
两次。
当UseConcat.ask(String str)
中的参数是input
变量时,连接似乎失败。但是,当我调用UseConcat.ask(String str)
并且参数是一个随机字符串时,连接起作用。
input
变量是cmd转换为字符串的第一行写入的行。
这是一个示例图像。
作为图像中所示,输入变量被设置为WOA。
然而UseConcat.ask(input);
打印出意思?OES WOA的 intsead是什么WOA是什么意思?
当输入打印时:System.out.println(input);
它正常打印WOA。
另一方面当我打电话UseConcat.ask("pie");
它的工作和打印:馅饼是什么意思?在Windows
为什么不使用'Scanner'来捕获用户输入? – 2014-12-19 00:06:46
@RyanJ似乎与扫描仪一起工作,但为什么它目前如何做呢? – Zachooz 2014-12-19 00:13:50
我可以告诉你它与终端的行为有关,而不是特定的代码。它使用带终端仿真的IDE工作。我怀疑你正在被什么[这篇文章](http://stackoverflow.com/questions/4007534/why-cant-we-read-one-character-at-a-time-from-system-在)中描述。尽管如此,我建议切换到“Scanner”,因为它更强健一些,更符合当前的实践。 – 2014-12-19 00:24:24