📄 menu.java
字号:
// menu class displays a menu of choices to a user// and performs the chosen task. It will keep asking a user to// enter the next choice until the choice of 'Q' (Quit) is// entered.import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class menu{ public static void main(String[] args) { char input1='\0'; String inputInfo = new String(); int operation2; String line = new String(); //create a linked list to be used in this method. LinkedList list1 = new LinkedList(); try { // print out the menu printMenu(); // create a BufferedReader object to read input from a keyboard InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr); do { do { System.out.print("What action would you like to perform?\n"); line = stdin.readLine().trim(); //read a line if (line.length() > 0) { input1 = line.charAt(0); input1 = Character.toUpperCase(input1); } } while (line.length() == 0); if (line.length() == 1) //check if a user entered only one character { switch (input1) { case'A': //Add String System.out.print("Please enter a string to add:\n"); String str1 = stdin.readLine().trim(); System.out.print("Please enter an index to add:\n"); inputInfo = stdin.readLine().trim(); int addIndex = Integer.parseInt(inputInfo); list1.addElement(addIndex, str1); break; case'D': //Search for the Index of a String System.out.print("Please enter a string to search:\n"); inputInfo = stdin.readLine().trim(); operation2 = list1.searchElement(inputInfo); if (operation2 > -1) System.out.print("string found at " + operation2 + "\n"); else System.out.print("string not found\n"); break; case'L': //List Strings System.out.print(list1.toString()); break; case'O': // List Current Size System.out.print("The current size is " + list1.size() + "\n"); break; case'Q': //Quit break; case'R': //Remove String System.out.print("Please enter the index of a string to remove:\n"); inputInfo = stdin.readLine().trim(); int removeIndex = Integer.parseInt(inputInfo); System.out.println(list1.removeElement(removeIndex) + " was removed"); break; case'T': //Find the smallest if (!list1.isEmpty()) System.out.print("The smallest is: " + list1.findSmallest() + "\n"); else System.out.print("The list is empty\n"); break; case'U': //Remove all the occurence of a given string System.out.print("Please enter a string to remove:\n"); inputInfo = stdin.readLine().trim(); int index = list1.searchElement(inputInfo); if (index > -1) { list1.removeAllOccurrences(inputInfo); System.out.print("All occurences of " + inputInfo + " removed\n"); } else System.out.print("string not found\n"); break; case'?': //Display Menu printMenu(); break; default: System.out.print("Unknown action\n"); break; } } else { System.out.print("Unknown action\n"); } } while (input1 != 'Q' || line.length() != 1); } catch (IOException exception) { System.out.print("IO Exception\n"); } } /** * The method printMenu displays the menu to a user * */ public static void printMenu() { System.out.print( "Choice\t\tAction\n" + "------\t\t------\n" + "A\t\tAdd String\n" + "D\t\tSearch for Index\n" + "L\t\tList Strings\n" + "O\t\tList Current Size\n" + "Q\t\tQuit\n" + "R\t\tRemove String\n" + "T\t\tFind Smallest\n" + "U\t\tRemove All Occurrences\n" + "?\t\tDisplay Help\n\n"); } //end of printMenu()}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -