|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net网页编程的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。
Java是一种面向对象的言语,是完成面向对象编程的壮大工具。但怎样在编程中实践使用并发扬其最年夜效能呢?本文经由过程一个实践Java程序的开辟历程,具体申明了怎样利用面向对象完成Java编程。
我们要完成的Java使用程序是:当用户输出一个球体的半径,程序将显现该球体的体积与外表积。在您浏览下文之前,请您本人思索一分钟,您将怎样计划该Java使用程序。
1、一般完成办法
我信任年夜多半程序员要完成上述功效的程序,十分敏捷地、自傲地将接纳上面的完成代码:
classSphere
{
publicstaticvoidmain(String[]args)
{
EasyReaderconsole=newEasyReader();
System.out.print("Entertheradius:");
doubleradius=console.readDouble();
System.out.println("Radius="+radius);
doublevolume=4.0/3.0*Math.PI*radius*radius*radius;
System.out.println("Volume="+volume);
doublesurfArea=4.0*Math.PI*radius*radius;
System.out.println("Surfacearea="+surfArea);
}
}
EasyReader类代码以下:
importjava.io.*;
publicclassEasyReader
{
protectedStringmyFileName;
protectedBufferedReadermyInFile;
protectedintmyErrorFlags=0;
protectedstaticfinalintOPENERROR=0x0001;
protectedstaticfinalintCLOSEERROR=0x0002;
protectedstaticfinalintREADERROR=0x0004;
protectedstaticfinalintEOF=0x0100;
/**
*Constructor.Preparesconsole(System.in)forreading
*/
publicEasyReader()
{
myFileName=null;
myErrorFlags=0;
myInFile=newBufferedReader(
newInputStreamReader(System.in),128);
}
/**
*Constructor.opensafileforreading
*@paramfileNamethenameorpathnameofthefile
*/
publicEasyReader(StringfileName)
{
myFileName=fileName;
myErrorFlags=0;
try
{
myInFile=newBufferedReader(newFileReader(fileName),1024);
}
catch(FileNotFoundExceptione)
{
myErrorFlags|=OPENERROR;
myFileName=null;
}
}
/**
*Closesthefile
*/
publicvoidclose()
{
if(myFileName==null)
return;
try
{
myInFile.close();
}
catch(IOExceptione)
{
System.err.println("Errorclosing"+myFileName+"
");
myErrorFlags|=CLOSEERROR;
}
}
/**
*Checksthestatusofthefile
*@returntrueifenerroroccurredopeningorreadingthefile,
*falseotherwise
*/
publicbooleanbad()
{
returnmyErrorFlags!=0;
}
/**
*CheckstheEOFstatusofthefile
*@returntrueifEOFwasencounteredinthepreviousread
*operation,falseotherwise
*/
publicbooleaneof()
{
return(myErrorFlags&EOF)!=0;
}
privatebooleanready()throwsIOException
{
returnmyFileName==null||myInFile.ready();
}
/**
*Readsthenextcharacterfromafile(anycharacterincluding
*aspaceoranewlinecharacter).
*@returncharacterreador<code>null</code>character
*(Unicode0)iftryingtoreadbeyondtheEOF
*/
publiccharreadChar()
{
charch=u0000;
try
{
if(ready())
{
ch=(char)myInFile.read();
}
}
catch(IOExceptione)
{
if(myFileName!=null)
System.err.println("Errorreading"+myFileName+"
");
myErrorFlags|=READERROR;
}
if(ch==u0000)
myErrorFlags|=EOF;
returnch;
}
/**
*Readsfromthecurrentpositioninthefileuptoandincluding
*thenextnewlinecharacter.Thenewlinecharacteristhrownaway
*@returnthereadstring(excludingthenewlinecharacter)or
*nulliftryingtoreadbeyondtheEOF
*/
publicStringreadLine()
{
Strings=null;
try
{
s=myInFile.readLine();
}
catch(IOExceptione)
{
if(myFileName!=null)
System.err.println("Errorreading"+myFileName+"
");
myErrorFlags|=READERROR;
}
if(s==null)
myErrorFlags|=EOF;
returns;
}
/**
*Skipswhitespaceandreadsthenextword(astringofconsecutive
*non-whitespacecharacters(uptobutexcludingthenextspace,
*newline,etc.)
*@returnthereadstringornulliftryingtoreadbeyondtheEOF
*/
publicStringreadWord()
{
StringBufferbuffer=newStringBuffer(128);
charch=;
intcount=0;
Strings=null;
try
{
while(ready()&&Character.isWhitespace(ch))
ch=(char)myInFile.read();
while(ready()&&!Character.isWhitespace(ch))
{
count++;
buffer.append(ch);
myInFile.mark(1);
ch=(char)myInFile.read();
};
if(count>0)
{
myInFile.reset();
s=buffer.toString();
}
else
{
myErrorFlags|=EOF;
}
}
catch(IOExceptione)
{
if(myFileName!=null)
System.err.println("Errorreading"+myFileName+"
");
myErrorFlags|=READERROR;
}
returns;
}
/**
*Readsthenextinteger(withoutvalidatingitsformat)
*@returntheintegerreador0iftryingtoreadbeyondtheEOF
*/
publicintreadInt()
{
Strings=readWord();
if(s!=null)
returnInteger.parseInt(s);
else
return0;
}
/**
*Readsthenextdouble(withoutvalidatingitsformat)
*@returnthenumberreador0iftryingtoreadbeyondtheEOF
*/
publicdoublereadDouble()
{
Strings=readWord();
if(s!=null)
returnDouble.parseDouble(s);
//inJava1,use:returnDouble.valueOf(s).doubleValue();
else
return0.0;
}
}
<p>
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。 |
|