所以我有这个代码我正在为我的AP计算机科学课做准备,当我编译时我遇到了很多错误,即使我看了它,它看起来很好,一切都井然有序。所有错误都来自myClock类和RepairShop类。
public class APCS_104_Time {
public static void main(String[] args) {
tester tester = new tester();
}
}
class myClock {
private int minute;
private int hour;
public myClock() {
hour = 2;
minute = 3;
}
public myClock(int minute, int hour) {
public int getHour() {
hour = IO.getInt("Enter the hour");
return hour;
}
public int getMinute() {
minute = IO.getInt("Enter the minute");
return minute;
}
public void int setMinute(int minute) {
this.minute = minute;
}
public void int setHour(int hour) {
this.hour = hour;
}
public String toString() {
if (minute < 10) {
return (hour + ":0" + minute);
}
else {
return (hour + ":" + minute);
}
}
}
}
class RepairShop {
public void int springForward(myClock time) {
hour++;
}
public void int resetClock(myClock time) {
hour = 2;
minute = 3;
}
public int cloneClock(myClock time) {
myClock copy = myClock myClock.clone();
return myClock;
}
}
class Tester {
Tester() {
myClock time = new myClock();
System.out.printf("The time is: " + myClock(2, 20));
}
}
错误消息包括:
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:38: error: illegal start of expression
public void int setMinute(int minute) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:38: error: illegal start of expression
public void int setMinute(int minute) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:38: error: ';' expected
public void int setMinute(int minute) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:38: error: '.class' expected
public void int setMinute(int minute) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:38: error: ';' expected
public void int setMinute(int minute) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:42: error: illegal start of expression
public void int setHour(int hour) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:42: error: illegal start of expression
public void int setHour(int hour) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:42: error: ';' expected
public void int setHour(int hour) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:42: error: '.class' expected
public void int setHour(int hour) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:42: error: ';' expected
public void int setHour(int hour) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:46: error: illegal start of expression
public String toString() {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:46: error: ';' expected
public String toString() {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:58: error: <identifier> expected
public void int springForward(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:58: error: '(' expected
public void int springForward(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:58: error: invalid method declaration; return type required
public void int springForward(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:62: error: <identifier> expected
public void int resetClock(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:62: error: '(' expected
public void int resetClock(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:62: error: invalid method declaration; return type required
public void int resetClock(myClock time) {
^
C:\Users\Tom\Dropbox\Auriemma, Thomas\AP Comp Sci\Unit 1\APCS_104_Time.java:69: error: ';' expected
myClock copy = myClock myClock.clone();
答案 0 :(得分:2)
您需要解决许多问题:
首先
//This myClock constructor can't contain other method!
public myClock(int minute, int hour) {
public int getHour() {
hour = IO.getInt("Enter the hour");
return hour;
}
...
}
第二件事是,设置者是void
,不能是void
和int
,请从这些方法中移除int
:
public void int setMinute(int minute) {
this.minute = minute;
}
第三件事是,小时是myClock
中你不能在其他课程中使用的属性:
private int hour;
第四点是你需要遵循惯例。类名必须是大写首字母。例如myClock
- &gt; MyClock
请参阅此内容以了解有关Java中的类的更多信息: https://docs.oracle.com/javase/tutorial/java/concepts/class.html
在确定以上所有要点后,我相信您可以理解并修复您的代码。如果没有,只要ping我。
希望这有帮助!