if(x%i==0){
returnfalse;
}
}
returntrue;
}
//显示1到x之间的所有素数
publicvoiddisp(intx)
{
for(inti=1;i<=x;i++){
if(is_sushu(i)){
System.out.println(i);
}
}
}
//程序入口
publicstaticvoidmain(String[]args)
{
code12c12=newcode12();
c12.disp(1000);
}
}
9.传简单参数的函数
/*
*使用简单数据类型做为参数时的函数传值方式
*当参数为简单数据类型时,参数传值使用复制传值方式,也就是说作为
*参数传进函数的值只是传进来值的一个副本
*/
classcode09
{
publicstaticvoidmain(String[]args)
{
System.out.println("HelloWorld!
");
intj=10;
//本例中calculate函数传递的是简单数据类型参数j,所以使用复制
//传递方式,函数中并没有改变原来j的内容
calculate(j);
System.out.println("====================================");
System.out.println("执行完calculate函数后j的值为:
"+j);
}
staticvoidcalculate(intj)//静态函数属于类,无需声明对象,可直接调用
{
for(inti=0;i<10;i++){
j++;
}
System.out.println("calculate函数中j的值为:
"+j);
}
}
10.传复杂参数的函数
/*
*使用复杂数据类型做为参数时的函数传值方式
*
*当参数为复杂数据类型时,参数传值使用引用传值方式,也就是说作为参数传进函数*的值只是传进来值的一个别名,直接效果就是函数中对参数做的修改直接就影响到原*始的值
*
*/
classcode10
{
publicstaticvoidmain(String[]args)
{
Stringa="hello";
System.out.println("初始阶段数组a的值为:
"+a);
calculate(a);
System.out.println("调用calculate函数后,数组a的值为:
"+a);
System.out.println("由此可知,在本函数中,参数是作为复杂变量的别名传进函数的,证据就是在函数中确实改变了函数外部变量a的值");
}
staticvoidcalculate(Stringj)
{
j="world";
System.out.println("calculate函数中参数数组j的值为:
"+j);
}
}
11.类与对象
//类是面向对象程序的处理单元,由(public和private)属性和方法构成。
//对象是类的实例化,通过new声明引入。
类封装数据和方法、可以继承,
//方法和算符可以多态重载。
类有构造和析构函数,用于初始化和收尾。
classcode11//基础类
{
Aa=newA();//类的属性,这里还含有初始化
intm=20;//另一个属性
publicbase09()//构造函数,与类同名
{
System.out.println("A.i="+a.i);
System.out.println("m="+m);
System.out.println("=============================");
change(a);
change(m);
System.out.println("A.i="+a.i);
System.out.println("m="+m);
}
publicstaticvoidmain(String[]args)//入口函数
{
newbase09();//实例化一个对象,引发其构造方法执行
}
voidchange(Aaa)//基础类的一个方法(函数)
{
aa.i+=100;
}
voidchange(intx)//基础类的另一个方法(函数)
{
x+=100;
System.out.println("函数内x="+x);
}
classA//嵌套的另一个类,恰巧只有属性,没有方法
{
inti=10;
};
}
12.函数返回值
/*函数的返回值
*
*return语句的作用是把一个确定的值返回给调用它的语句,这个值可以是
*简单变量,也可以是对象。
如果返回值是对象,则返回的实际上是对象的
*地址。
这个情况和参数传递相同
*/
classcode12
{
//求x*x+2(y+1)+3的函数
staticfloatmathF1(floatx,floaty)
{
floatz=x*x+2*++y+3;
//我们可以这样写返回值
returnz;
}
staticfloatmathF2(floatx,floaty)
{
//我们也可以这样写返回值(直接返回一个运算表达式的结果)
returnx*x+2*++y+3;
}
publicstaticvoidmain(String[]args)
{
//System.out.println(a);
//我们可以这样获得返回值
floatz=mathF2(4f,5f);
System.out.println("运算结果="+z);
//我们也可以这样获得返回值
System.out.println("运算结果="+mathF1(4f,5f));
}
}
13.函数嵌套
/*
*函数的嵌套
*
*函数自己调用自己叫做嵌套
*/
classcode13
{
staticinti=0;
//这是一种不好的嵌套,在函数体中无条件的调用了自己。
导致的结果就是
//无限的循环
staticvoidcout_line()
{
System.out.println("=============================");
cout_line();
}
//一种安全的嵌套,有条件的调用自己,不会导致死循环
staticvoidcout_line2()
{
System.out.println("=============================");
i++;
if(i<10)
{
cout_line2();
}
}
publicstaticvoidmain(String[]args)
{
//请注释掉这个函数,因为它将导致死循环,函数永远不结束
cout_line();
cout_line2();
}
}
14.输入输出
/***********************************************************
*Java中输入用BufferedReader对象,输出用System.out.println
************************************************************/
importjava.io.*;
classbase14{
publicstaticvoidmain(Stringargs[]){
Strings;
//创建一个BufferedReader对象从键盘逐行读入数据
InputStreamReaderisr=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(isr);
//try/catch是异常处理,catch部分处理try部分的异常(错误)
try{//每读入一行后向显示器输出
s=br.readLine();
while(!
s.equals("")){//String的equal方法用于字串比较
System.out.println("Read:
"+s);
s=br.readLine();
}
br.close();//关闭输入流
}catch(IOExceptione){//捕获可能的IOException.
e.printStackTrace();
}
}
}
15.文件操作
/***************************************************************
*Java中用BufferedReader读文件,用PrintWriter写文件
****************************************************************/
importjava.io.*;
publicclassbase15{
publicstaticvoidmain(String[]args){
//Part1:
Fileread
Stringfname="base15.java";
Filef=newFile(fname);
try{
FileReaderfr=newFileReader(f);
BufferedReaderbr=newBufferedReader(fr);
Strings=br.readLine();
while(s!
=null){
System.out.println("读入:
"+s);
s=br.readLine();
}
br.close();//关闭缓冲读入流及文件读入流的连接.
}catch(FileNotFoundExceptione1){
System.err.println("Filenotfound:
"+fname);
}catch(IOExceptione2){
e2.printStackTrace();
}
//Part2:
Filewrite
Filefile=newFile("tt.txt");
try{
InputStreamReaderisr=newInputStreamReader(System.in);
BufferedReaderin=newBufferedReader(isr);
FileWriterfw=newFileWriter(file);
PrintWriterout=newPrintWriter(fw);
//从键盘逐行读入数据输出到文件
Strings=in.readLine();
while(!
s.equals("")){
out.println(s);
s=in.readLine();
}
in.close();//关闭BufferedReader输入流.
out.close();//关闭连接文件的PrintWriter输出流.
}catch(IOExceptione){
System.out.println(e);
}
}
}
16.综合:
Record操作
importjava.io.*;
/**
*ADriverfortestingtheRecordclass
*@authorPingFeng
*@versionJune2012
*/
publicclassbase16
{
publicstaticvoidmain(Stringargs[])
{
charchoice;
RecordtheRecord;
theRecord=newRecord();
Strings="0";
InputStreamReaderisr=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(isr);
try{
do{
System.out.print("\n"+"RecordDriver"
+"\n"+"============"
+"\n"+"1.PopulatetheRecord"
+"\n"+"2.DeletetheRecord"
+"\n"+"3.Showas