如何解决试图弄清楚如何将回车键作为用户输入信号给printMenu
我已经编写了以下应用程序,一切正常。但是,在用户输入与菜单命令相对应的值(1-7)之后,将执行该命令,然后程序终止。我正在尝试找到一种方法,允许我在显示命令结果后要求用户“按Enter”,重新打印菜单并继续输入命令,直到它们准备退出(8)。这意味着按Enter键将重新打印菜单并允许输入更多命令(1-8)。我知道这需要作为一个循环来完成,但是我不确定在这种情况下哪个/如何使用该循环。谢谢。
import java.util.*;
import java.io.*;
public class Rosterapplication {
Scanner in = null;
public static void main(String[] args) {
Roster studentRoster = new Roster();
studentRoster.loadData("assg3_roster.txt");
printMenu();
Scanner keyboard = new Scanner(system.in);
String entry = keyboard.next();
if (entry.equals("1")) { //display the roster
studentRoster.displayRoster();
}
//System.out.println("Press enter to continue:");
//String reload = keyboard.nextLine();
//if (reload.equals("")) {
// printMenu();
//}
if (entry.equals("2")) { //Search for a student by id
System.out.println("Please enter the student id to search: ");
String searchId = keyboard.next();
if ((studentRoster.searchForStudent(searchId)) == null) {
System.out.println("Student not found!");
}
}
if (entry.equals("3")) { //Add a new student
System.out.println("Please enter the student id to add: ");
String addId = keyboard.next();
if (studentRoster.searchForStudent(addId) != null) {
System.out.println("Error: This student already exists! Student's info displayed above." + "\n");
} else {
System.out.println("Please enter the student name: ");
String addName = keyboard.next() + " " + keyboard.next();
System.out.println("Please enter the student standing: ");
String addStanding = keyboard.next();
System.out.println("Please enter the student major: ");
String addMajor = keyboard.next();
studentRoster.addStudent(addId,addName,addStanding,addMajor);
System.out.println("The entered information has succesfully been added as a new student." + "\n");
}
}
if (entry.equals("4")) { //Remove a student
System.out.println("Please enter the student id to remove: ");
String removeId = keyboard.next();
studentRoster.removeStudent(removeId);
}
if (entry.equals("5")) { //Search for student by major
System.out.println("Please enter the major to search: ");
String majorSearch = keyboard.next();
ArrayList < Student > students = studentRoster.getStudentByMajor(majorSearch);
if (students.size() != 0) {
System.out.println("The students with this major are listed above.");
} else {
System.out.println("Error: There are no students with this major!");
}
}
if (entry.equals("6")) { //Sort and save to file
studentRoster.sort();
studentRoster.Save();
}
if (entry.equals("7")) { //Save to file
studentRoster.Save();
}
if (entry.equals("8")) { //Exit
studentRoster.Save();
System.exit(0);
}
keyboard.close();
}
private static void printMenu() {
System.out.println("1. display the roster");
System.out.println("2. Search for a student by id");
System.out.println("3. Add a new student");
System.out.println("4. Remove a student");
System.out.println("5. Search for students by major");
System.out.println("6. Sort and save to file");
System.out.println("7. Save to file");
System.out.println("8. Exit");
}
}
解决方法
如评论中所述,在函数周围添加while
循环将重复输入周期,直到退出。
public static void main(String[] args) {
Roster studentRoster = new Roster();
studentRoster.loadData("assg3_roster.txt");
Scanner keyboard = new Scanner(System.in);
while (true) {
printMenu();
String entry = keyboard.next();
// all the other if-statements
if (entry.equals("8")) { //Exit
studentRoster.Save();
// System.exit(0);
// Killing the program is not advised,better just leave the loop and terminate regularly
break;
}
}
keyboard.close();
}
,
大约一年前,我正在寻找可以收听进入,逃生,另类(alt)等声音的图书馆。由于您的问题,我发现了a nice library。
向pom.xml
添加以下依赖项(使用Maven项目):
<dependency>
<groupId>com.1stleg</groupId>
<artifactId>jnativehook</artifactId>
<version>2.1.0</version>
</dependency>
示例代码:
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class GlobalKeyListenerExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException nativeHookException) {
nativeHookException.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
// Clear previous logging configurations.
LogManager.getLogManager().reset();
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
}
}
使用此库,您可以获得有关单击的Enter,空格,转义等信息。上面的示例已完成,您可以复制并检查其工作方式。 Here是原始示例。
询问您是否不懂;)
P.S。
这里正在检查单击了哪个键。
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException nativeHookException) {
nativeHookException.printStackTrace();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。