class FriendlyData() 改成 class FriendlyData这是一个类名,不是方法
我也觉得蹊跷。但书上的确是这么写的,不过没说怎么编译的。这可是十分权威的书,java类全球销量第一
to ashes: 的确是这个小问题,大哥果然眼力过人,小弟自叹不如!!!!!!!!! 真是太感谢了!!!!
那这个呢?也是这种文件。又出错了,15个错(哭)public class Time { private int hour;//0-23 private int minute;//0-59 private int second;//0-59
//Time constructor initializes each instance variable //to zero.Ensures that each Time object starts in a //consistent state. public Time(){setTime(0,0,0);}
//Set a new Time value using military time .Perform //validity checks on the data.Set invalid values //to zero. public void setTime(int h,int m,int s) { hour=((h>=0&&h<24)?h:0); minute=((m>=0&&m<60)?m:0); second=((s>=0&&s<60)?s:0); } //Convert Time to String in military-time format public String toMilitaryString() { return (hour<10?"0":"")+hour+ (minute<10?"0":"")+minute; } //Convert Time to String in standard-time format public String toString() { return((hour==12||hour==0)?12:hour%12)+ ":"+(minute<10?"0":"")+minute+ ":"+(second<10?"0":"")+second+ (hour<12?"AM":"PM"); } }//FileName=TimeTest.java //Class TimeTest to exercise class Time import java.awt.Graphics; import java.applet.Applet;public class TimeTest extends Applet { private Time 1; public void init() { t=new Time(); } public void paint(Graphics g) {
g.drawString("The initial military time is :"+ t.toMilitaryString(),25,25); g.drawString("The initial standard time is :"+ t.toString(),25,40);
t.setTime(13,27,6); g.drawString("Military time after setTime is :"+ t.toMilitaryString,25,70); g.drawString("Standard time after setTime is :"+ t.toString,25,85); t.setTime(99,99,99); g.drawString("After attempting invalid settings:",25,115); g.drawString("Military time:"+ t.toMilitaryString(),25,130); g.drawString("Standard time:"+t.toString(),25,145); } } //<APPLET CODE=TimeTest.class WIDTH=300 HEIGHT=200> </APPLET>输出如下================================================ ---------- javac ---------- TimeTest.java:43: 'class' or 'interface' expected import java.awt.Graphics; ^ TimeTest.java:44: 'class' or 'interface' expected import java.applet.Applet; ^ TimeTest.java:48: <identifier> expected private Time 1; ^ TimeTest.java:3: class Time is public, should be declared in a file named Time.java public class Time ^ TimeTest.java:46: cannot resolve symbol symbol : class Applet location: class TimeTest public class TimeTest extends Applet ^ TimeTest.java:55: cannot resolve symbol symbol : class Graphics location: class TimeTest public void paint(Graphics g) ^ TimeTest.java:52: cannot resolve symbol symbol : variable t location: class TimeTest t=new Time(); ^ TimeTest.java:59: cannot resolve symbol symbol : variable t location: class TimeTest t.toMilitaryString(),25,25); ^ TimeTest.java:61: cannot resolve symbol symbol : variable t location: class TimeTest t.toString(),25,40); ^ TimeTest.java:63: cannot resolve symbol symbol : variable t location: class TimeTest t.setTime(13,27,6); ^ TimeTest.java:65: cannot resolve symbol symbol : variable t location: class TimeTest t.toMilitaryString,25,70); ^ TimeTest.java:67: cannot resolve symbol symbol : variable t location: class TimeTest t.toString,25,85); ^ TimeTest.java:69: cannot resolve symbol symbol : variable t location: class TimeTest t.setTime(99,99,99); ^ TimeTest.java:72: cannot resolve symbol symbol : variable t location: class TimeTest t.toMilitaryString(),25,130); ^ TimeTest.java:73: cannot resolve symbol symbol : variable t location: class TimeTest g.drawString("Standard time:"+t.toString(),25,145); ^ 15 errors输出完成 (耗时 48 秒) - 正常终止
class FriendlyData()这么简单的错误,类是这样定义的吗
public class Time 改成 class Time或者把 public class Time { ... } 另存为Time.java一个.java文件里只能有一个public class
//FileName=TimeTest.java //Class TimeTest to exercise class Time import java.awt.Graphics; import java.applet.Applet; //import 放在开始,然后再编码。 //在一个类里只有一个public类型的类public class TimeTest extends Applet { private Time t; public void init() { t=new Time(); } public void paint(Graphics g) {
g.drawString("The initial military time is :"+ t.toMilitaryString(),25,25); g.drawString("The initial standard time is :"+ t.toString(),25,40);
t.setTime(13,27,6); g.drawString("Military time after setTime is :"+ t.toMilitaryString(),25,70); g.drawString("Standard time after setTime is :"+ t.toString(),25,85); t.setTime(99,99,99); g.drawString("After attempting invalid settings:",25,115); g.drawString("Military time:"+ t.toMilitaryString(),25,130); g.drawString("Standard time:"+t.toString(),25,145); } } //<APPLET CODE=TimeTest.class WIDTH=300 HEIGHT=200> </APPLET> class Time { private int hour;//0-23 private int minute;//0-59 private int second;//0-59
//Time constructor initializes each instance variable //to zero.Ensures that each Time object starts in a //consistent state. public Time(){setTime(0,0,0);}
//Set a new Time value using military time .Perform //validity checks on the data.Set invalid values //to zero. public void setTime(int h,int m,int s) { hour=((h>=0&&h<24)?h:0); minute=((m>=0&&m<60)?m:0); second=((s>=0&&s<60)?s:0); } //Convert Time to String in military-time format public String toMilitaryString() { return (hour<10?"0":"")+hour+ (minute<10?"0":"")+minute; } //Convert Time to String in standard-time format public String toString() { return((hour==12||hour==0)?12:hour%12)+ ":"+(minute<10?"0":"")+minute+ ":"+(second<10?"0":"")+second+ (hour<12?"AM":"PM"); } }
改成
class FriendlyData这是一个类名,不是方法
的确是这个小问题,大哥果然眼力过人,小弟自叹不如!!!!!!!!!
真是太感谢了!!!!
{
private int hour;//0-23
private int minute;//0-59
private int second;//0-59
//Time constructor initializes each instance variable
//to zero.Ensures that each Time object starts in a
//consistent state.
public Time(){setTime(0,0,0);}
//Set a new Time value using military time .Perform
//validity checks on the data.Set invalid values
//to zero.
public void setTime(int h,int m,int s)
{
hour=((h>=0&&h<24)?h:0);
minute=((m>=0&&m<60)?m:0);
second=((s>=0&&s<60)?s:0);
} //Convert Time to String in military-time format
public String toMilitaryString()
{
return (hour<10?"0":"")+hour+
(minute<10?"0":"")+minute;
} //Convert Time to String in standard-time format
public String toString()
{
return((hour==12||hour==0)?12:hour%12)+
":"+(minute<10?"0":"")+minute+
":"+(second<10?"0":"")+second+
(hour<12?"AM":"PM");
}
}//FileName=TimeTest.java
//Class TimeTest to exercise class Time
import java.awt.Graphics;
import java.applet.Applet;public class TimeTest extends Applet
{
private Time 1; public void init()
{
t=new Time();
} public void paint(Graphics g)
{
g.drawString("The initial military time is :"+
t.toMilitaryString(),25,25);
g.drawString("The initial standard time is :"+
t.toString(),25,40);
t.setTime(13,27,6);
g.drawString("Military time after setTime is :"+
t.toMilitaryString,25,70);
g.drawString("Standard time after setTime is :"+
t.toString,25,85); t.setTime(99,99,99);
g.drawString("After attempting invalid settings:",25,115);
g.drawString("Military time:"+
t.toMilitaryString(),25,130);
g.drawString("Standard time:"+t.toString(),25,145);
}
}
//<APPLET CODE=TimeTest.class WIDTH=300 HEIGHT=200> </APPLET>输出如下================================================
---------- javac ----------
TimeTest.java:43: 'class' or 'interface' expected
import java.awt.Graphics;
^
TimeTest.java:44: 'class' or 'interface' expected
import java.applet.Applet;
^
TimeTest.java:48: <identifier> expected
private Time 1;
^
TimeTest.java:3: class Time is public, should be declared in a file named Time.java
public class Time
^
TimeTest.java:46: cannot resolve symbol
symbol : class Applet
location: class TimeTest
public class TimeTest extends Applet
^
TimeTest.java:55: cannot resolve symbol
symbol : class Graphics
location: class TimeTest
public void paint(Graphics g)
^
TimeTest.java:52: cannot resolve symbol
symbol : variable t
location: class TimeTest
t=new Time();
^
TimeTest.java:59: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.toMilitaryString(),25,25);
^
TimeTest.java:61: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.toString(),25,40);
^
TimeTest.java:63: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.setTime(13,27,6);
^
TimeTest.java:65: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.toMilitaryString,25,70);
^
TimeTest.java:67: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.toString,25,85);
^
TimeTest.java:69: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.setTime(99,99,99);
^
TimeTest.java:72: cannot resolve symbol
symbol : variable t
location: class TimeTest
t.toMilitaryString(),25,130);
^
TimeTest.java:73: cannot resolve symbol
symbol : variable t
location: class TimeTest
g.drawString("Standard time:"+t.toString(),25,145);
^
15 errors输出完成 (耗时 48 秒) - 正常终止
改成
class Time或者把
public class Time
{
...
}
另存为Time.java一个.java文件里只能有一个public class
//Class TimeTest to exercise class Time
import java.awt.Graphics;
import java.applet.Applet;
//import 放在开始,然后再编码。
//在一个类里只有一个public类型的类public class TimeTest extends Applet
{
private Time t; public void init()
{
t=new Time();
} public void paint(Graphics g)
{
g.drawString("The initial military time is :"+
t.toMilitaryString(),25,25);
g.drawString("The initial standard time is :"+
t.toString(),25,40);
t.setTime(13,27,6);
g.drawString("Military time after setTime is :"+
t.toMilitaryString(),25,70);
g.drawString("Standard time after setTime is :"+
t.toString(),25,85); t.setTime(99,99,99);
g.drawString("After attempting invalid settings:",25,115);
g.drawString("Military time:"+
t.toMilitaryString(),25,130);
g.drawString("Standard time:"+t.toString(),25,145);
}
}
//<APPLET CODE=TimeTest.class WIDTH=300 HEIGHT=200> </APPLET>
class Time
{
private int hour;//0-23
private int minute;//0-59
private int second;//0-59
//Time constructor initializes each instance variable
//to zero.Ensures that each Time object starts in a
//consistent state.
public Time(){setTime(0,0,0);}
//Set a new Time value using military time .Perform
//validity checks on the data.Set invalid values
//to zero.
public void setTime(int h,int m,int s)
{
hour=((h>=0&&h<24)?h:0);
minute=((m>=0&&m<60)?m:0);
second=((s>=0&&s<60)?s:0);
} //Convert Time to String in military-time format
public String toMilitaryString()
{
return (hour<10?"0":"")+hour+
(minute<10?"0":"")+minute;
} //Convert Time to String in standard-time format
public String toString()
{
return((hour==12||hour==0)?12:hour%12)+
":"+(minute<10?"0":"")+minute+
":"+(second<10?"0":"")+second+
(hour<12?"AM":"PM");
}
}
不过书上的确是那么写的,可能是bug吧