如何回到Java中的特定while循环

问题描述 投票:-3回答:1

有人可以帮助我确定问题出在哪里吗?无论我最后在“课程/学生/年级”菜单中选择了什么选项,程序始终会返回到“年级”循环。

对不起,我的程序比较杂乱,我还是新手,如果您有时间,请帮助我修改代码。

我学校的每门课程都必须这样做,所以时间太长了。

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char crs = 'C';
        char yrr = 'Y';
        char Std = 'S';
        char inv = 'Y';
        int yrlvl, sections, students;
        ArrayList < String > yearlvl = new ArrayList < String > ();
        yearlvl.add("First Year");
        yearlvl.add("Second Year");
        yearlvl.add("Third Year");
        yearlvl.add("Fourth Year");
        yearlvl.add("Fifth Year");
        ArrayList < String > Sections = new ArrayList < String > ();
        String[] alphabet = {
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
        };
        ArrayList < String > SectionsName = new ArrayList < String > ();
        ArrayList < String > Students = new ArrayList < String > ();
        ArrayList < String > StudentsName = new ArrayList < String > ();
        while ((crs == 'C' || crs == 'c')) {
            System.out.println("========================================================================================");
            System.out.println("\nPamantasan ng Lungsod ng Marikina\n");
            System.out.println("Please choose a Course\n");
            System.out.println("Professional Programs \nA.Bachelor in Elementary Education (BEEd");
            System.out.println("\nEnter your letter of your choice:");
            char course = input.next().charAt(0);

            switch (course) {
                case 'A':
                case 'a':
                    System.out.println("\nWelcome to College of Bachelor in Elementary Education!\nDo you want to get major in Special education? (Y for yes)");
                    char se = input.next().charAt(0);
                    if ((se == 'y' || se == 'Y')) {
                        System.out.println("\nYou've chosen to get the major in Special Education!");
                        while ((yrr == 'Y' || yrr == 'y')) {
                            System.out.println("\nChoose the year level you want to create new section:\n1.First Year\n2.Second Year\n3.Third Year\n4.Fourth Year\n5.Fifth Year\nEnter the number of your choice:");
                            yrlvl = input.nextInt();
                            while ((Std == 'S' || Std == 's')) {
                                System.out.println("\nHow many Section you want to create for " + yearlvl.get(yrlvl - 1) + "? \nEnter 0 if none");
                                sections = input.nextInt();
                                for (int i = 1; i <= sections; i++) {
                                    String xx;
                                    System.out.print("\nSection #" + i + "\nEnter The Section Name No Spaces: ");
                                    xx = input.next();
                                    SectionsName.add(xx);
                                } //for for (sections-i)
                                System.out.println("\nThe following are section/s for " + yearlvl.get(yrlvl - 1) + ".");
                                for (int i = 0; i < SectionsName.size(); i++) {
                                    System.out.println(alphabet[i] + "." + SectionsName.get(i));
                                } //for section names display
                                System.out.println("\nDo you want to add students or view students? ADD or DISPLAY:\nA. Add\n\nEnter your choice");
                                char choice = input.next().charAt(0);
                                if (choice == 'A' || choice == 'a') {
                                    System.out.println("\nThe list of sections");
                                    for (int i = 0; i < SectionsName.size(); i++) {
                                        System.out.println((i + 1) + "." + SectionsName.get(i));
                                    }
                                    System.out.println("\nEnter the number of section in which you want to insert students");
                                    int i = input.nextInt();
                                    System.out.println("\nYou've chosen " + SectionsName.get(i - 1));
                                    System.out.println("\nEnter the number of students you want in " + SectionsName.get(i - 1) + "\nEnter 0 if none");
                                    int q = input.nextInt();
                                    ArrayList < String > StudNum = new ArrayList < String > (q);
                                    input.nextLine();
                                    for (int w = 0; w < q; w++) {
                                        System.out.println("Student #" + (w + 1) + " Name: ");
                                        String xxx = input.nextLine();
                                        StudNum.add(xxx);
                                    } //StudNum
                                    inv = 'Y';
                                    while (inv == 'Y') {
                                        System.out.println("\nReturn to A.Section Menu | B.Year Level Menu | C.Course Menu | D.Exit\nEnter the letter of your choice:");



//PROBLEM: When I Input letter B, I'm hoping it would go to Year level But it's going directly to Section Menu Same with Letter C



                                        char chs = input.next().charAt(0);
                                        switch (chs) {
                                            case 'a':
                                            case 'A':
                                                crs = 'x';
                                                yrr = 'x';
                                                Std = 's';
                                                Std = 'S';
                                                inv = 'x';
                                                break;
                                            case 'b':
                                            case 'B':
                                                crs = 'x';
                                                yrr = 'y';
                                                yrr = 'Y';
                                                Std = 's';
                                                inv = 'x';
                                                break;
                                            case 'c':
                                            case 'C':
                                                crs = 'c';
                                                crs = 'C';
                                                yrr = 'y';
                                                Std = 's';
                                                inv = 'x';
                                                break;
                                            case 'd':
                                            case 'D':
                                                System.exit(0);
                                            default:
                                                System.out.println("Invalid Choice");
                                                crs = 'c';
                                                yrr = 'y';
                                                Std = 'S';
                                                break;
                                        } //for switch(Chs)
                                    } //while(inv)
                                } //for while (Std)
                            } //for while (yrr)
                        } //if (choiceA)
                    } //for if (se)
            } // For while (crs)
        } // For Switch (course)

    }
}
java arrays loops for-loop switch-statement
1个回答
0
投票

欢迎使用Stackoverflow。我已经运行了您的代码,并试图将其修复得尽可能简单。但是,如果您可以为部门创建一个类,为一年创建一个类,为学生创建一个类,那么您的程序会更好。面向对象程序将使您的代码更灵活。

import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        char crs = 'C';
        char yrr = 'Y';
        char Std = 'S';
        char inv = 'Y';
        int yrlvl, sections, students;
        ArrayList<String> yearlvl = new ArrayList<String>();
        yearlvl.add("First Year");
        yearlvl.add("Second Year");
        yearlvl.add("Third Year");
        yearlvl.add("Fourth Year");
        yearlvl.add("Fifth Year");
        ArrayList<String> Sections = new ArrayList<String>();
        String[] alphabet = {
                "A",
                "B",
                "C",
                "D",
                "E",
                "F",
                "G",
                "H",
                "I",
                "J",
                "K",
                "L",
                "M",
                "N",
                "O",
                "P",
                "Q",
                "R",
                "S",
                "T",
                "U",
                "V",
                "W",
                "X",
                "Y",
                "Z"
        };
        ArrayList<String> SectionsName = new ArrayList<String>();
        ArrayList<String> Students = new ArrayList<String>();
        ArrayList<String> StudentsName = new ArrayList<String>();
        courseLoop : while ((crs == 'C' || crs == 'c'))
        {
            System.out.println("========================================================================================");
            System.out.println("\nPamantasan ng Lungsod ng Marikina\n");
            System.out.println("Please choose a Course\n");
            System.out.println("Professional Programs \nA.Bachelor in Elementary Education (BEEd");
            System.out.println("Enter your letter of your choice:");
            char course = input.next().charAt(0);

            switch (course)
            {
                case 'A':
                case 'a':
                    System.out.println("\nWelcome to College of Bachelor in Elementary Education!\nDo you want to get major in Special education? (Y for yes)");
                    char se = input.next().charAt(0);
                    if ((se == 'y' || se == 'Y'))
                    {
                        System.out.println("\nYou've chosen to get the major in Special Education!");
                        yearLoop : while ((yrr == 'Y' || yrr == 'y'))
                        {
                            System.out.println("\nChoose the year level you want to create new section:\n1.First Year\n2.Second Year\n3.Third Year\n4.Fourth Year\n5.Fifth Year\nEnter the number of your choice:");
                            yrlvl = input.nextInt();
                            sectionsLoop : while ((Std == 'S' || Std == 's'))
                            {
                                System.out.println("How many Section you want to create for " + yearlvl.get(yrlvl - 1) + "? \nEnter 0 if none");
                                sections = input.nextInt();
                                for (int i = 1; i <= sections; i++)
                                {
                                    String xx;
                                    System.out.print("Section #" + i + "\nEnter The Section Name No Spaces: ");
                                    xx = input.next();
                                    SectionsName.add(xx);
                                } //for for (sections-i)
                                System.out.println("\nThe following are section/s for " + yearlvl.get(yrlvl - 1) + ".");
                                for (int i = 0; i < SectionsName.size(); i++)
                                {
                                    System.out.println(alphabet[i] + "." + SectionsName.get(i));
                                } //for section names display
                                System.out.println("\nDo you want to add students or view students? ADD or DISPLAY:\nA. Add\n\nEnter your choice");
                                char choice = input.next().charAt(0);
                                if (choice == 'A' || choice == 'a')
                                {
                                    System.out.println("The list of sections");
                                    for (int i = 0; i < SectionsName.size(); i++)
                                    {
                                        System.out.println((i + 1) + "." + SectionsName.get(i));
                                    }
                                    System.out.println("Enter the number of section in which you want to insert students");
                                    int i = input.nextInt();
                                    System.out.println("You've chosen " + SectionsName.get(i - 1));
                                    System.out.println("\nEnter the number of students you want in " + SectionsName.get(i - 1) + "\nEnter 0 if none");
                                    int q = input.nextInt();
                                    ArrayList<String> StudNum = new ArrayList<String>(q);
                                    input.nextLine();
                                    for (int w = 0; w < q; w++)
                                    {
                                        System.out.println("Student #" + (w + 1) + " Name: ");
                                        String xxx = input.nextLine();
                                        StudNum.add(xxx);
                                    } //StudNum
                                    inv = 'Y';
                                    while (inv == 'Y')
                                    {
                                        System.out.println("\nReturn to A.Section Menu | B.Year Level Menu | C.Course Menu | D.Exit\nEnter the letter of your choice:");
                                        crs = 'x';
                                        yrr = 'x';
                                        Std = 'x';
                                        char chs = input.next().charAt(0);
                                        switch (chs)
                                        {
                                            case 'a':
                                            case 'A':
                                                crs = 'x';
                                                yrr = 'x';
                                                Std = 's';
                                                Std = 'S';
                                                inv = 'x';
                                                continue sectionsLoop;
                                            case 'b':
                                            case 'B':
                                                crs = 'x';
                                                yrr = 'y';
                                                yrr = 'Y';
                                                Std = 's';
                                                inv = 'x';
                                                continue yearLoop;
                                            case 'c':
                                            case 'C':
                                                crs = 'c';
                                                crs = 'C';
                                                yrr = 'y';
                                                Std = 's';
                                                inv = 'x';
                                                continue courseLoop;
                                            case 'd':
                                            case 'D':
                                                System.exit(0);
                                            default:
                                                System.out.println("Invalid Choice");
                                                crs = 'c';
                                                yrr = 'y';
                                                Std = 'S';
                                                break;
                                        } //for switch(Chs)
                                    } //while(inv)
                                } //for while (Std)
                            } //for while (yrr)
                        } //if (choiceA)
                    } //for if (se)
            } // For while (crs)
        } // For Switch (course)

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.