目前我是java新手,我试图让我的代码从顶部重新启动,所以基本上你可以继续输入句子,直到用户故意取消。目前我一直在收到错误。
import java.util.Scanner;
public class Reverser {
public static void main(String[] args) {
boolean run = true;
while(run) {
for(;;) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello Welcome To The Sentence Reverser !");
System.out.println("Enter a sentence below to reverse.");
String str = scan.nextLine();
scan.close();
String reversedStr = new StringBuilder(str).reverse().toString();
System.out.println("Initial Sentence: " + str);
System.out.println("Reversed Sentence: " + reversedStr);
}
}
}
}
答案 0 :(得分:0)
我曾经做过类似的事情,这是主持人检查特定的用户帐户。您基本上在输入处理后重新调用搜索方法。这样您就可以将程序重置为先前的状态。
/**
* @author Stan van der Bend
*/
public class Scanner {
private static final String CHARACTER_FILE_PATH = "data/characters";
private static ArrayDeque<String> matches = new ArrayDeque<>(200);
public static void main(final String... args) throws IOException {
search();
}
private static void search() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your desired keyword: ");
String keyword = reader.readLine();
System.out.println("Started scanning all files...");
long start = System.currentTimeMillis();
try {
Path path = Paths.get(CHARACTER_FILE_PATH);
Files.walkFileTree(path, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
matches.addAll(Files
.readAllLines(path)
.stream()
.filter(line -> line.contains(keyword))
.collect(Collectors.toList())
);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
System.out.println("Make sure this is in the same directory as "+CHARACTER_FILE_PATH);
}
long duration = System.currentTimeMillis() - start;
System.out.println("Found "+matches.size()+" matches for the keyword "+keyword);
matches.stream().sorted(Comparator.comparing(PlayerLogs::getDate)).forEach(System.out::println);
System.out.println("The scan toke " + duration + "ms to complete.");
System.out.print("For another scan type -r ");
matches.clear();
if(reader.readLine().equalsIgnoreCase("-r"))
search();
reader.close();
}
}