java程序复习题.doc
《java程序复习题.doc》由会员分享,可在线阅读,更多相关《java程序复习题.doc(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1./程序China.java:public class China public static void main(String args) man zhangsan=new man(); zhangsan.say(); class man void say() System.out.println() 2/CopyFile.javaimport java.io.*; class CopyFile public static void main(String args) throws IOException int i; FileInputStream fin; FileOutputStre
2、am fout; fin=new FileInputStream(myfile.txt); fout=new FileOutputStream(yourfile.txt); Do i=fin.read(); if(i!=-1) fout.write(i); while(i!=-1); fin.close(); fout.close(); System.out.println(myfile.txt内容已被复制到yourfile.txt文件中); 3.class Avoid f() int u=(int)(Math.random()*100); int v=0 ,p; if (u50) v=9;
3、p=v+u;4./App.javaclass Person private String name;private int age;/定义一个私有整型属性agepublic Person(String name, int age)this.name=name;this.age=age;public void show( )System.out.println(姓名:+name+年龄+age); public class App public static void main(String args) Person per;per=new Person1;per0=new Person(张三,1
4、9);per0.show(); 5./App.javaclass Person String name;private int age;/定义一个私有整型属性agepublic int getAge( )return age;/返回age属性public void setAge(int age)if(age=5&age=5&age=5&s1.getAge()=20)System.out.println(s1.name+的年龄是:+s1.getAge();elseSystem.out.println(年龄错误); 7.public class App9_1 public static void
5、main(String args) int i; int a=1,2,3,4; for(i=0;i5;i+) System.out.println( a+i+=+ai); System.out.println(5/0+(5/0); 8.public class App9_2 public static void main(String args) int i; int a=1,2,3,4; for(i=0;i5;i+) try System.out.println( a+i+=+ai); catch() System.out.println(5/0+(5/0); 9/C1.javaclass
6、PersonA private String name; public void setName(String newName) name=newName; public String getName() return name; class StudentA extends PersonA /继承PersonA类 private String department; public void setDepartment(String newDepartment) department=newDepartment; public String getDepartment() return dep
7、artment; class C1 /定义一个C1类,测试类 public static void main(String args) StudentA s1=new StudentA(); s1.setName(张三) ; s1.setDepartment(计算机系); System.out.println(你好,我是+s1.getName(); /显示s1的姓名 System.out.println(我是+s1.getDepartment()+的学生) ; /显示s1的系别 10./Hello.java class Avoid f() System.out.println(I am A);
8、 class B public class Hello public static void main(String args) System.out.println(hello); A a=new A(); a.f(); 11.public class Ji public static void main(String args) int i,s=0; for(i=1;i100;i=i+2) s=s+i; System.out.println(1+3+.+99=+s); 12.import java.util.*;public class jie public static void mai
9、n(String args) int n,i,s=1; Scanner buf=new Scanner(System.in); do System.out.print (输入正整数:); n=buf.nextInt( ); while (n=0); for(i=1;i=90)System.out.println(优秀);else if(a=80)System.out.println(良);else if(a=70)System.out.println(中);else if(a=60)System.out.println(及格);elseSystem.out.println(不及格);14./f
10、ilename:Li4_2.javapublic class Li4_2 public static void main(String args) int testScore; char grade; Scanner reader=new Scanner(System.in); System.out.print(请输入成绩:); grade=reader.nextInt(); switch(grade) case 90:; break; case B:testScore=80-90; break; case C:testScore=70-80; break; case D:testScore=
11、60-70; break; case EtestScore=0-60; System.out.println(评定成绩为:+grade); 15./filename:Li4_12.javapublic class Li4_12 public static void mian(String args) final int MAX=100; int j,k,n; System.out.println(2+MAX+之间的所有素数为;); System.out.print(2t); n=1; k=3; do j=3; while(jMath.sqrt(k) System.out.print(k+t);
12、 n+; if 16./filename: Max.javaimport java.util.*; /加载java.util类库里的所有类public class Max public static void main(String args) int num1,num2,num3; int max; Scanner reader=new Scanner(System.in); System.out.print(请输入3个整数:); num1= reader.nextInt(); num2= reader.nextInt(); num3= reader.nextInt(); max=num1;
13、 max=maxnum2?max:num2; max=maxnum3?max:num3; System.out.println(max); 17.class MyOver public int A(int a, int b) /定义两个参数的A方法,得到a+b的和 int s; s=a+b; return s; public int A(int a,int b,int c) int s; s=a+b; s=s+c; /要求调用两个参数的A方法,得到a+b+c的和 return s; public int A(int a,int b,int c,int d) int s; s=a+b; s=s+
14、c+d;/要求调用三个参数的A方法,得到a+b+c+d的和 return s; public static void main(String args) MyOver p=new MyOver(); System.out.println(1+2=+p.A(1,2); System.out.println(1+2+3=+p.A(1,2,3); System.out.println(1+2+3+4=+p.A(1,2,3,4); 18.class Student String name; int age; int chinese; int math; int english; public Stud
15、ent() /定义无参构造方法 name=; age=0; chinese=0; math=0; english=0; System.out.println(Student()构造方法被调用); public Student(String newName) /定义一个参数的构造方法 name=newName; public Student(String newName,int newChinese) /定义两个参数的构造方法 name=newName; chinese=newChinese; public Student(String newName,int newAge,int newChi
16、nese,int newMath,int newEnglish) /定义五个参数的构造方法 name=newName;age=newAge;chinese=newChinese;math=newMath;english=newEnglish; int total() return chinese+math+english; int average() return total()/3; public void show() /显示对象各属性的值 System.out.println(你好,我叫+name+,+我今年+age+岁); System.out.println(我的语文成绩是+chin
17、ese); System.out.println(我的数学成绩是+math); System.out.println(我的英语成绩是+english); System.out.println(我的总分是+total(); class Mystudent /主类 public static void main(String args) Student s1=new Student(); /创建Student类的一个对象,不传入参数 s1.show(); Student s2=new Student(张三);/创建Student类的一个对象,传入一个参数:张三 s2.show(); Student
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 程序 复习题
限制150内