gpt4 book ai didi

java - 为什么 course[] 对象不能跟随指向 double 的指针?

转载 作者:行者123 更新时间:2023-12-02 04:37:44 26 4
gpt4 key购买 nike

对于我的 java 编程类(class),我正在尝试编写一个程序来判断类是否冲突。到目前为止,我有一个名为 Course 的对象。 public void init将初始化上述所有成员的类(class),然后 public boolean conflict(Scanner scan)这是我们发现与日程发生冲突的地方。和public String toString返回类(class)的名称和时间。我的问题是,由于我在附图中指出的编译错误,考虑到 othercourse 是 Course[] (类(class)数组)指针,我将如何设置 othercourse 来比较时间并获取结束时间?

到目前为止,我已经尝试过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.timeothercourse.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;
}

}

最佳答案

所以第一件事是第一件事。您的方法(甚至根据评论)应该只采用一个参数,即要进行比较的类(class)。您已经拥有全局范围内的当前类(class)信息。所以你的方法签名如下:

public boolean conflict(Course othercourse) {

对于第 3 步,您需要使用其他类(class)的信息。

//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.timetime )。

关于java - 为什么 course[] 对象不能跟随指向 double 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com