对于我的java编程类,我正在尝试编写一个程序来判断类是否冲突。到目前为止,我有一个名为Course
的对象。 public void init
将初始化上面提到的所有成员的课程,然后public boolean conflict(Scanner scan)
是我们发现与时间表冲突的地方。 public String toString
返回课程的名称和时间。我的问题是,由于我在附图中注明的编译错误,如何设置其他课程来比较时间并得到结束时间考虑其他课程是一个Course [](课程数组)指针?
到目前为止,我已经尝试过othercourse[i].end_time = time + length / class_period * per + length % class_period;
并且发回一个符号无法找到编译错误。
if语句if (othercourse[i].length course.time && course.time < othercourse[i].length)
我想知道如何设置othercourse.time
和othercourse.length
以适当地遵循指针。提前谢谢。
import java.util.*;
public class Course {
public static final int class_period = 60;
public static final double per = 100;
public String name; // ie: CS142, CS141, ENG 101
public String instructor; // ie: John Mill, Bob the Fish
public String room; // ie: 127A, 18-218, the bathroom
public int time; // in military notation. ie: 1030, 1600 etc
public int length; // length in minutes. 50 means the course
// lasts 50 minutes.
public double end_time;
// name: init
// desc: reads in entries for the members from the user
// params: Scanner scan
// return: void
// 1) for each member do steps 2-3.
// 2) prompt the user to enter a value
// 3) read in that value and store in the member
public void init(Scanner scan) {
//Steps 1-3
System.out.print("Please enter in a course name: ");
name = scan.next();
System.out.print("Please enter in the name of an instructor: ");
instructor = scan.next();
System.out.print("Please enter in the room number: ");
room = scan.next();
System.out.print("Please enter in the start time: ");
time = scan.nextInt();
System.out.print("Please enter in the length of the course: ");
length = scan.nextInt();
}
// name: conflict
// desc: returns true if this course conflicts with the parameter
// params: Course othercourse
// return: boolean - true if the courses conflict
// 1) if this course and othercourse start at the same time, return true
// 2) calculate the end time of this course ( start time + length/60*100 +
// length % 60 )
// 3) calculate the end of the othercourse
// 4) if this course starts after other course, and starts before
// the other course ends, return true
// 5) if the other course starts after this course, and starts before
// this course ends, return true
// 6) return false
public boolean conflict(Course[] othercourse, Course course) {
//Step 1
if (this.time == othercourse.time)
return true;
//Step 2
course.end_time = time + length / class_period * per + length % class_period;
//Step 3
othercourse.end_time = time + length / class_period * per + length % class_period;
//Step 4
if (this.time > othercourse.length && course.length < othercourse.time)
return true;
//Step 5
if (othercourse.length course.time && course.time < othercourse.length)
return true;
//Step 6
return false;
}
// name: print
// desc: returns a string that represents some basic info about the course
// params: none
// return: String
// 1) return a string of the format "Course " + name + " with start time " +
// time
public String toString() {
return name + time;
}
}
答案 0 :(得分:2)
首先是第一件事。你的方法(根据评论甚至)应该只采用一个参数,比较课程。您已拥有全球范围内的当前课程信息。所以你的方法签名如下:
public boolean conflict(Course othercourse) {
对于第3步,您需要使用其他信息。
//Step 3
othercourse.end_time = othercourse.time + othercourse.length / othercourse.class_period
* othercourse.per + othercourse.length % othercourse.class_period;
现在,当你调用方法时,它就是
othercourse[i].conflict(course);
此外,您引用course
的任何地方都会将其替换为this
,或者只是将其删除(即course.time
变为this.time
或time
)。