[英]same input calling two different methods in java
我正在嘗試為 KWIC 實現 java 中的主要方法。 我遇到的問題是我必須詢問用戶是否要從控制台/文件寫入輸入並將 output 寫入控制台/文件。 當我必須閱讀時,第一次向用戶詢問控制台/文件工作正常,但是當我再次向他們詢問 output 時,我相信它會回到第一個 If 條件。 這是供參考的代碼。
try {
System.out.println("Please enter FILE to input from file or CONSOLE to input from console:");
String userInput = "";
while ((userInput = scannerWrapper.nextLine()) != "-1") {
if (userInput.equals("CONSOLE")) {
System.out.println("Please enter FILE to output from file or CONSOLE to output from console:");
List<String> cshiftConsole = circularShifter.shiftLines(inputFromConsole.read());
if (userInput.equals("CONSOLE")) {
System.out.println("Please enter lines to add, then enter -1 to finish:");
// Console
cshiftConsole = alphabetizer.sort(cshiftConsole);
outputToConsole.write(cshiftConsole);
for (String element : cshiftConsole) {
System.out.println(element);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
這是我的控制台 output
Please enter FILE to input from file or CONSOLE to input from console:
CONSOLE
Please enter FILE to output from file or CONSOLE to output from console:
CONSOLE
Software Architecture
-1
Please enter lines to add, then enter -1 to finish:
Architecture Software
CONSOLE
Software Architecture
在第二個CONSOLE(userInput)
之后,我應該被要求輸入行,但這是以CONSOLE
作為我想要循環移動的輸入。 任何幫助都會非常感謝。
您的代碼中有一些問題,如果我理解正確,您想決定從哪里輸入,從哪里到 OUTPUT 並將幾個Strings
放入List
。
(userInput = scannerWrapper.nextLine()) != "-1"
您使用.equals(...)
比較 Java 中的字符串,就像您在下面的代碼中所做的那樣。
每次你問你的用戶他想從哪里輸入時,你只需要讀一次,所以不要在一段while
內把它放在if
上。
您在每次迭代時創建列表,將其作為實例成員。
在您打印對象的每次迭代中,等到用戶首先停止添加項目(他們輸入-1
)
更好的方法可能是這樣的:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InputCycle {
private Scanner scanner;
private static final String CONSOLE = "CONSOLE";
private static final String EXIT_CODE = "-1";
private List<String> list;
public static void main(String[] args) {
new InputCycle().readAndWrite();
}
private void readAndWrite() {
scanner = new Scanner(System.in);
list = new ArrayList<String>();
System.out.println("INPUT FROM CONSOLE / FILE");
String input = scanner.nextLine(); // Read where to input from
if (input.equals(CONSOLE)) {
System.out.println("OUTPUT TO CONSOLE / FILE");
String output = scanner.nextLine(); // Read where to output to
if (output.equals(CONSOLE)) {
System.out.println("WRITE LINES (" + EXIT_CODE + " TO EXIT)");
String line = "";
do {
line = scanner.nextLine(); // Read every line
if (!line.equals(EXIT_CODE)) {
list.add(line);
}
} while (!line.equals(EXIT_CODE));
} else { // Write to a file with your own code
System.out.println("Writing to a file");
}
} else { //Read from a file
System.out.println("Reading from a file");
}
System.out.println("ALL LINES: ");
list.forEach(line -> System.out.println(line)); //Print all the lines user wrote
}
}
那有這個output:
INPUT FROM CONSOLE / FILE
CONSOLE
OUTPUT TO CONSOLE / FILE
CONSOLE
WRITE LINES (-1 TO EXIT)
FOO
BAR
BANANA
-1
ALL LINES:
FOO
BAR
BANANA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.