試圖讓我的輸出看起來像下面的循環:UserInputViaCommand: match1, match2
鋼骨對於造成問題
但它顯示:UserInputViaCommand: match1, UserInputViaCommand: match2
我知道這是由於第二個for循環是第一個裏面,但我不確定要去得到我想要的結果。
我的計劃是在命令行這樣運行:java program name1 name2 < names.txt
我讀了文件,規範其內部名稱,然後讀取用戶輸入,做同樣的,如果他們匹配打印出來。
try {
InputReader = new BufferedReader(new InputStreamReader(System.in));
while ((nameFile = InputReader.readLine()) != null) {
//Normalising the input to find matches
nameFromFile = normalize(nameFile);
//Looping through each argument
for (int j = 0; j < ags.length; j++) {
// Appending text to the string builder
//Output.append(ags[j] + ":" + " ");
//Normalising the input to find matches
String result = normalize(ags[j]);
if (nameFromFile.equalsIgnoreCase(result)) {
Output.append(ags[j] + ":" + " " + nameFile + ", ");
//Output.append(ags[j] + ":" + " ");
//Output.append(nameFile + ", ");
}
}
}
System.out.println(Output);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
這是一個有趣的for循環。你爲什麼不使用'while'循環?你基本上已經把'for'變成了'while'。 – crush
@crush我做了瘋狂的變化,我認爲這是一個階段的while循環。我會改變它,看看我是否可以玩它。 :)更改爲while循環:) –
您的示例輸出似乎不符合您的代碼。看起來輸出應該更類似於:'Name1:name1,name2:name2'。所以無論你在此期間改變了一些東西,還是你遺漏的代碼都是重要的一部分。 –