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

时间:2019-02-25 14:57:42

标签: java

我正在尝试编写一个有条件的程序,并在3门课程中询问以下问题:

  1. 课程名称是什么?
  2. 该课程招收了多少学生?
  3. 这门课程的学生能力是多少?
  4. 第三门课程的期末考试时间是什么时候?

条件是课程2的最终考试日期不能与课程1的最终考试日期相同,课程3的最终考试日期不能与课程1或课程2相同。它检查课程一个,但在检查课程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

您可以看到第二次更改最终考试日期的尝试符合条件,因此应该说 “第三门课程的期末考试日期不能与第一门课程或第二门课程相同。请按Enter键,然后重试” 但它只是在用户按下Enter键后终止

4 个答案:

答案 0 :(得分:1)

这里有一个问题:

while ( final3 == final1 && final3 == final2);

应该是

while ( final3 == final1 || final3 == final2);

条件应为OR或AND。

话虽这么说,我认为这是针对您所参加的课程。我会给你一些建议。利用您在上面的代码中已经演示的知识,所有这些都可以以一种更简单,更简洁的方式完成。您只需要更多地思考如何更有效地使用循环和类即可实现目标。

答案 1 :(得分:0)

我建议您上Course类。

public class Course {
    private String name;
    private int capacity;
    private int enrollment;
    private Date finalExamDate;

    // You add the rest.
}

您可以添加一个CourseCatalog来保留List<Course>个Course实例。

了解JUnit还为时不早。将您的课程和测试代码分开。

答案 2 :(得分:0)

在Eclipse here中遵循此调试文档。

第1步 在代码中添加断点。

第2步 作为Java应用程序进行调试,并一步一步检查

答案 3 :(得分:0)

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