为什么当我清楚地定义上面的时钟实例时出现错误"symbol not found"?


public class TestClock {
    public static void main(String[] args){
      /*(1)declare int variables hA, mA, hB, mB, hC, mC
       */
        int hA;
        int mA;
        int hB;
        int mB;
        int hC;
        int mC;
      /*(2)declare String variables milA, milB, milC, civA, civB, civC
       */
        String milA;
        String milB;
        String milC;
        String civA;
        String civB;
        String civC;
      /*(3)declare Clock variables clockA, clockB, clockC */
        Clock clockA;
        Clock clockB;
        Clock clockC;
      /*(4)construct clockA using default constructor */
        clockA = new Clock();
      /*(5)construct clockB using alternate constructor
       *    for military time 2400
       */
        clockB = new Clock();
      /*(6)construct clockC using alternate constructor
       *    with hours=11 and minutes=45
       */
        clockC = new Clock();
      /*(7)print clockA, clockB, clockC on separate lines
       */
        System.out.println(clockA());
        System.out.println(clockB());
        System.out.println(clockC());

编辑:进一步说明。。。

/*(8)assign hours and minutes to hA,mA, hB,mB, hC,mC
   *   using the hours() and minutes() methods
   *   for clockA, clockB, and clockC respectively
   */
    hA = clockA(hours);
    mA = clockA(minutes);
    hB = clockB(hours);
    mB = clockB(minutes);
    hC = clockC(hours);
    mC = clockC(minutes);
  /*(9)assign militaryTime and civilianTime to milA, civA,
   *    milB, civB, milC, civC for clockA,clockB,clockC resp
   *   using the appropriate methods
   */

该程序(TestClock(用于测试程序Clock中的所有方法(单独制作,请参阅下面的代码(。注释是我们必须做什么的说明。那么,为什么在打印语句中,它说clockA、clockB和clockC没有找到,而我在上面明确将它们定义为新的Clock?

这是单独的时钟程序:

public class Clock {
/* instance field:  
 *   totalMinutes is always between 1 and 24*60 
 */
private int totalMinutes;   
/** default constructor sets the clock to represent 12:01 a.m.
 */
public  Clock(){
  totalMinutes = 1;  
}           //end default constructor
/** alternate contructor sets clock to represent time in military
 *  parameter hours - number of hours since midnight previous day
 *  parameter minutes - number of minutes since last hour changed
 *  e.g.  14:03 military is equivalent to 2:03 p.m. civilian
 *  preconditions:  0<=hours<=24, 0<=minutes<=59,
 *                  0<hours*60 + minutes<=24*60
 */
 public Clock(int hours, int minutes){
    totalMinutes = hours*60 + minutes;
    String errMsg = null;
    if ( minutes < 0 || minutes > 59){
        errMsg = "nminutes="+minutes+" not between 0 and 59";
    } else if ( hours < 0 || hours > 24){
        errMsg = "nhours="+hours + " not between 0 and 24";
    }  else if (totalMinutes < 1 || totalMinutes>60*24 ) {
        errMsg = "ntotalMinutes not between 1 and 24*60";
    }
    if (errMsg != null){
        throw new IllegalArgumentException(errMsg);
    }
}              //end alternate constructor
/** returns the number of hours in this Clock's time
*/
public int hours(){
 int hrs = totalMinutes / 60;
 return hrs;
}
/** returns the number of minutes since the last hour change
*/
public int minutes(){
 int min = totalMinutes % 60;
 return min;
}
/** returns a printable version of the time in Military context
*/
public String militaryTime(){
 String mTime = "",hStr="",mStr="";
 int hours = totalMinutes / 60;
 int minutes = totalMinutes % 60;
 if (hours<10){
     hStr = "0"+hours;
 } else {
     hStr = "" + hours;
 }
 if (minutes<10){
     mStr = "0"+minutes;
 } else {
     mStr = "" + minutes;
 }     
 mTime = hStr + "" + mStr;
 return mTime;
}
/** returns a printable version of the time in Civilian context
*/
public String civilianTime(){
 String cTime = "", mStr="", suffix="";
 int hours = totalMinutes / 60;
 int minutes = totalMinutes % 60;
 if ( totalMinutes == 12*60 ){  
     cTime = "12:00 noon";
 }else if ( totalMinutes == 24*60) 
     cTime = "12:00 midnight";
 else {  //neither noon nor midnight
    if (minutes < 10) {
        mStr = "0" + minutes;
    } else {
        mStr = "" + minutes;
    }    
    if (totalMinutes > 12*60){
         hours = hours - 12;
         suffix = " p.m.";
    } else {
        suffix = " a.m.";
    }
    cTime = hours + ":" + mStr + suffix;
 }       //end neithernoon nor midnight
  return cTime;
 }
}

没有什么能比得上System.out.println(clockA());,即clockA()

将其更改为System.out.println(clockA);。覆盖Clock类中的toString()

当我明确定义上面的时钟实例时,为什么会出现"找不到符号"的错误?

上述错误通常发生在类类型无法识别的情况下。所以import your class Clock

/*(7)print clockA, clockB, clockC on separate lines
*/
System.out.println(clockA());
System.out.println(clockB());
System.out.println(clockC());

您使用的是clockC(),因为您添加了(),它指的是一种方法。

如果要打印变量,请使用:

/*(7)print clockA, clockB, clockC on separate lines
*/
System.out.println(clockA);
System.out.println(clockB);
System.out.println(clockC);

行,如:
System.out.println(clockA(((
hA=时钟A(小时(
是错误的。clockA是一个对象,它不是要调用的函数,所以不要使用clockA((。类似地,clockA(hours(是错误的。clockA不是通过传递参数(即"hours"(来调用的方法。相反,通过类的对象正确调用方法,如下所示:

int hA=时钟A.hours((


并在创建类Clock的不同对象时传递参数
参数化构造函数将按如下方式调用:

clockA=新时钟(小时、分钟(


非参数化构造函数将按如下方式调用:

clockA=新时钟((


类似地,为创建的不同对象传递不同的参数

最新更新