退出切换回主方法



我有一个方法,它使用switch语句为用户提供要选择的选项,一旦用户选择了一个选项并且执行了case中的代码,我如何返回到提供另一个菜单的主方法?

我的部分代码:

  static void modifyStudent() {
    System.out.println("Wish student would you like to change?");
    for(int i=0;i<10;i++){
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    System.out.println("1: Change name.");
    ....
    int detailChange = input.nextInt();
    switch (detailChange) {
      case 1:
        String newName = input.next();
        studentNamesArray[studentChoice] = newName;
      break;
      ....
     }

  public static void main(String[] args) {
    while (1 == 1) {
    System.out.println("Please select an option:");
    System.out.println("1: Add a student.");
    ....
    int choice = input.nextInt();
      switch (choice) {
        case 1:
        ....
   }

编辑(要求完整代码):

/**
 * User: Colin Shewell
 * Date: 26/11/13
 * Time: 10:32
 */
import java.util.Scanner;
import java.util.Arrays;      //REMOVE THIS!!!
public class StudentMarks {
  static Scanner input = new Scanner(System.in);
  static String[] studentNamesArray = new String[10];
  static int[][] studentMarksArray = new int[10][3];
  static int nameArrayCount, markArrayCount = 0;
  static int markOne, markTwo, markThree;
  static String studentName;
  static void printArrays(){
      System.out.println(Arrays.toString(studentNamesArray));
      for (int index=0;index<10;index++)
      {
          System.out.println(studentMarksArray[index][0]);
          System.out.println(studentMarksArray[index][1]);
          System.out.println(studentMarksArray[index][2]);
      }
  }
  static void addStudent() {
    if (nameArrayCount < 10) {
      System.out.println("Enter the student's name in the following format - surname, forename: ");
      studentName = input.next();
      studentNamesArray[nameArrayCount] = studentName;
      nameArrayCount = nameArrayCount + 1;
    }
    else if (nameArrayCount == 10) {
      System.out.println("******Array is full, please delete a student before adding another.*****");
    }
    if (markArrayCount < 10){
    System.out.println("Enter the first mark: ");
          markOne = input.nextInt();
          System.out.println("Enter the second mark: ");
          markTwo = input.nextInt();
          System.out.println("Enter the third mark: ");
          markThree = input.nextInt();
          studentMarksArray[markArrayCount][0] = markOne;
          studentMarksArray[markArrayCount][1] = markTwo;
          studentMarksArray[markArrayCount][2] = markThree;
          markArrayCount = markArrayCount + 1;
    }
  }
  static void modifyStudent() {
    System.out.println("Wish student would you like to change?");
    for(int i=0;i<10;i++){
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    System.out.println("1: Change name.");
    System.out.println("2: Change first mark.");
    System.out.println("3: Change second mark.");
    System.out.println("4: Change third mark.");
    System.out.println("5: Change all marks.");
    int detailChange = input.nextInt();
    switch (detailChange) {
      case 1:
        System.out.println("Enter the new student name.");
        String newName = input.next();
        studentNamesArray[studentChoice] = newName;
        return;
      case 2:
        System.out.println("Enter the new mark for mark one.");
        int newMarkOne = input.nextInt();
        studentMarksArray[studentChoice][0] = newMarkOne;
      return;
      case 3:
        //two
      break;
      case 4:
        //three
      break;
      case 5:
        //all
      break;
      default:
        System.exit(0);
      break;
    }
  }
  public static void main(String[] args) {
    while (true) {
      System.out.println("Please select an option:");
      System.out.println("1: Add a student.");
      System.out.println("2: Modify the details of an existing student.");
      System.out.println("3: Delete an existing student.");
      System.out.println("4: Sort in alphabetical order by name.");
      System.out.println("5: Output the student name and corresponding marks in ascending name order.");
      System.out.println("6: Output the student name and corresponding marks in descending name order.");
      System.out.println("7: Display the student with the highest average mark.");
      System.out.println("8: Display the student with the lowest average mark.");
      System.out.println("9: Display the average score of all students recorded.");
      System.out.println("10: Exit.");
      int choice = input.nextInt();
      switch (choice) {
        case 1:
          addStudent();
          System.out.println(Arrays.toString(studentNamesArray));
        break;
        case 2:
          modifyStudent();
        break;
        case 3:
          printArrays();
        break;
       /* case 4:
          sortAlphabetical();
        break;
        case 5:
          outputNameMarksAsc();
        break;
        case 6:
          outputNameMarksDsc();
        break;
        case 7:
          highestStudentAvgMark();
        break;
        case 8:
          lowestStudentAvgMark();
        break;
        case 9:
          displayAvgScore();
        break;         */
        case 10:
          System.exit(0);
        break;
        default:
          System.exit(0);
        break;
      }
    }
  }
}

只需return到主方法。return只是退出该方法,并从调用它的行继续执行代码。

  case 1:
    String newName = input.next();
    studentNamesArray[studentChoice] = newName;
    return;  //exits this method
  //break; <-- not needed after a return!

您的代码将在无限循环中运行,您可以使用类似的条件标志来运行它

boolean isRunning = true;
while (isRunning) {
    System.out.println("Please select an option:");
    System.out.println("1: Add a student.");
    ....
    int choice = input.nextInt();
      switch (choice) {
        case 1:
        isRunning = false;
        //your code for case 1
        ....
   }
//rest of the code in main executes now

这将退出while循环,返回主方法

相关内容

  • 没有找到相关文章

最新更新