Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的程序在检查java中的第二个条件之前终止_Java - Fatal编程技术网

为什么我的程序在检查java中的第二个条件之前终止

为什么我的程序在检查java中的第二个条件之前终止,java,Java,我试图编写一个有条件的程序,并针对3门课程提出以下问题: 这门课叫什么名字 这门课程招收了多少学生 本课程的学生能力是多少 第三门课的期末考试日期是什么时候 条件是课程2的期末考试日期不能与课程1的期末考试日期相同,课程3的期末考试日期不能与课程1或课程2相同。它检查课程1,但在检查课程2之前终止 代码如下: import java.util.Scanner; public class CourseTest2 { public static void main(String[] ar

我试图编写一个有条件的程序,并针对3门课程提出以下问题:

  • 这门课叫什么名字
  • 这门课程招收了多少学生
  • 本课程的学生能力是多少
  • 第三门课的期末考试日期是什么时候
  • 条件是课程2的期末考试日期不能与课程1的期末考试日期相同,课程3的期末考试日期不能与课程1或课程2相同。它检查课程1,但在检查课程2之前终止

    代码如下:

    import java.util.Scanner;
    
    public class CourseTest2 {
    
        public static void main(String[] args) {
            Scanner scanoption = new Scanner(System.in);
            Scanner in = new Scanner(System. in);
            String name1;
            int enroll1;
            int cap1;
            String final1;
            String answer;
    
    
            //This is for the first course
            System.out.println("What is the name of the course?");
            name1 = in. nextLine();
            System.out.println("How many students are enrolled in the course?");
            enroll1 = in. nextInt();
            System.out.println("What is student capacity for this course?");
            cap1 = in. nextInt(); in.nextLine();
        while (enroll1 > cap1)
        {
            System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
            in.nextLine();
            System.out.println("How many students are enrolled in the course?");
            enroll1 = in. nextInt();
            System.out.println("What is student capacity for this course?");
            cap1 = in. nextInt(); in.nextLine();
        }
            System.out.println("When is the final exam date for this course?");
            final1 = in. nextLine();
            Course course1 = new Course (name1);
            System.out.println("Do you want to add another course? (Y/N)"); // this is to repeat the questions so the use can enter information for the next class obejct
            answer = in.nextLine();
            course1.setName(name1);
            course1.setEnrolledStudentNum(enroll1);
            course1.setFinalExamDate(final1);
    
        //2nd Course Object
        String name2;
        int enroll2;
        int cap2;
        String final2;
        switch (answer) 
        {
    
            case "Y":
            case "y": System.out.println("What is the name of the second course?");
                    name2 = in. nextLine();
                    System.out.println("How many students are enrolled in the course?");
                    enroll2 = in. nextInt();
                    System.out.println("What is student capacity for this course?");
                    cap2 = in. nextInt();in.nextLine();
                    while (enroll2 > cap2)
                    {
                        System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
                        in.nextLine();
                        System.out.println("How many students are enrolled in the course?");
                        enroll2 = in. nextInt();
                        System.out.println("What is student capacity for this course?");
                        cap2 = in. nextInt(); in.nextLine();
                    }
                    do 
                    {
                    System.out.println("When is the final exam date for this course?");
                    final2 = in. nextLine();
                    }
                    while ( final2 == final1);
                    {
                        System.out.println("Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again");
                        in.nextLine();
                        System.out.println("When is the final exam date for this course?");
                        final2 = in. nextLine();
                     }
    
                    Course course2 = new Course (name2);
                    System.out.println("Do you want to add another course? (Y/N)");// this is to repeat the questions so the use can enter information for the next class obejct
                    answer = in.nextLine();
                    course2.setName(name2);
                    course2.setEnrolledStudentNum(enroll2);
                    course2.setFinalExamDate(final2);
                     break;
    
            case "N":
            case "n":System.exit(0);
                 break;
    
            default :
                 System.out.println("invalid choice")   ;
                 break;
        }
        //3rd Class Object
    
            String final3 = null;
    
        switch (answer)
        {
            case "Y":
            case "y":       System.out.println("What is the name of the third course?");
            String name3 = in. nextLine();
            System.out.println("How many students are enrolled in the course?");
            int enroll3 = in. nextInt();
            System.out.println("What is student capacity for this course?");
            int cap3 = in. nextInt();in.nextLine();
            while (enroll3 > cap3)
            {
                System.out.println("Amount of Enrolled Students cannot exceed Student Capacity. Press Enter");
                in.nextLine();
                System.out.println("How many students are enrolled in the course?");
                enroll3 = in. nextInt();
                System.out.println("What is student capacity for this course?");
                cap3 = in. nextInt(); in.nextLine();
            }
            do 
            {
            System.out.println("When is the final exam date for the third course?");
            final3 = in. nextLine();
            }
            while ( final3 == final1 && final3 == final2);
            {
                System.out.println("Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again");
                in.nextLine();
                System.out.println("When is the final exam date for this course?");
                final3 = in. nextLine();
                in.nextLine();
             }
            Course course3 = new Course (name3);
            course3.setName(name3);
            course3.setEnrolledStudentNum(enroll3);
            course3.setFinalExamDate(final3); `
    
    这是它打印出来的:

    What is the name of the course? a How many students are enrolled in the course? 5 What is student capacity for this course? 20 When is the final exam date for this course? 05/05 Do you want to add another course? (Y/N) y What is the name of the second course? b How many students are enrolled in the course? 10 What is student capacity for this course? 5 Amount of Enrolled Students cannot exceed Student Capacity.Press Enter How many students are enrolled in the course? 10 What is student capacity for this course? 20 When is the final exam date for this course? 05/05 Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again When is the final exam date for this course? 10/10 Do you want to add another course? (Y/N) y What is the name of the third course? c How many students are enrolled in the course? 15 What is student capacity for this course? 10 Amount of Enrolled Students cannot exceed Student Capacity. Press Enter How many students are enrolled in the course? 15 What is student capacity for this course? 20 When is the final exam date for the third course? 05/05 Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again When is the final exam date for this course? 10/10 这门课叫什么名字? A. 这门课程招收了多少学生? 5. 本课程的学生能力是多少? 20 这门课程的期末考试日期是什么时候? 05/05 是否要添加其他课程?(是/否) Y 第二道菜叫什么名字? B 这门课程招收了多少学生? 10 本课程的学生能力是多少? 5. 注册学生人数不能超过学生人数。按Enter键 这门课程招收了多少学生? 10 本课程的学生能力是多少? 20 这门课程的期末考试日期是什么时候? 05/05 第二门课程的期末考试日期不能与第一门课程相同。按enter键并重试 这门课程的期末考试日期是什么时候? 10/10 是否要添加其他课程?(是/否) Y 第三道菜叫什么名字? C 这门课程招收了多少学生? 15 本课程的学生能力是多少? 10 注册学生人数不能超过学生人数。按回车键 这门课程招收了多少学生? 15 本课程的学生能力是多少? 20 第三门课的期末考试日期是什么时候? 05/05 第三门课程的期末考试日期不能与第一门或第二门课程相同。按enter键并重试 这门课程的期末考试日期是什么时候? 10/10 正如你所见,第二次改变期末考试日期的尝试符合条件,所以应该这样说 “第三门课程的期末考试日期不能与第一门或第二门课程的期末考试日期相同。请按enter键,然后重试”
    但在用户按下enter键后,它就会终止。我建议您开设一门课程

    public class Course {
        private String name;
        private int capacity;
        private int enrollment;
        private Date finalExamDate;
    
        // You add the rest.
    }
    
    您可以添加一个
    CourseCatalog
    ,其中保存一个
    列表
    当然是课程实例

    现在了解这些还为时过早。将类和测试代码分开。

    这里有一个问题:

    while ( final3 == final1 && final3 == final2);
    
    应该是

    while ( final3 == final1 || final3 == final2);
    
    条件应该是或不是和


    也就是说,我想这是为你要上的课准备的。我会给你一些建议。利用上面代码中已经演示的知识,这一切都可以以更简单、更清晰的方式完成。您只需要更多地考虑如何更有效地使用循环和类来实现您的目标。

    请遵循eclipse中的调试文档

    步骤1 在代码中添加断点

    步骤2
    作为java应用程序进行调试并逐个检查

    这是因为java的短路评估。检查相关答案

    学生的经典错误:代码太多,分解不良。编写可以通过向其传递数据进行测试的小方法。不要重复你自己-干燥原则。保持与用户的交互完全分开。嗨。现在是学习如何调试的好时机。如果您没有使用诸如Eclipse之类的IDE(集成开发环境),则应该。这是一个教程。还可以看看我在使用eclipse。此外,第二和第三门课的问题应该重复。我尝试了你给我@Mark的链接中的建议,但这只会使第二门课不起作用,而在它起作用之前,我只是第三门课不起作用。当我按照你的建议做游戏时,这是“预期的”error@Idara对不起,你是对的。把它放回去lol我不记得上次我做了一个do,而代码在课程1和课程2中完美地工作,当我在期末考试日期第二次错误尝试后为课程3做时,它终止了。你遵循调试链接了吗?或者你知道如何添加断点吗?这是我第一次使用eclipse,所以我无法理解它。我想了解有关向下投票的解释,请提供任何关于如何压缩的建议,我们将不胜感激。这是stackoverflow中的第一个问题,所以不要担心。阅读指南在你发布的代码中看不到它。太多东西-一个坏迹象。你看到的代码是驱动程序类而不是类课程。我需要帮助的代码是driver class@duffymo