Java运行程序写结果(学生版)_第1页
Java运行程序写结果(学生版)_第2页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java 练习题、运行程序写结果1.当在命令提示符后输入 ”ava SumT est 4 5 ”之后,下列程序的输出结果为:public class SumTestpublic static void main( Stri ng args)int a, b, sum;a=ln teger.parse In t(argsO);b=ln teger.parse In t(args1);sum=a+b;System.out.pri ntln(a + + + b + = + sum);2.当在命令提示符后输入”ava PrintStar 8 ”之后,下列程序的输出结果为:public class Pr

2、in tStar public static void main(String args) int n = In teger.parse In t(args0);int i;for( i=1; i=n; i+) System.out.pri nt(*);System.out.pri ntl n();3.当在命令提示符后输入 java CalculateTest 5.5 之后,下列程序的输出结果为:public class CalculateTestpublic static void main(String args) float x,y;x = Float.parseFloat(args0)

3、; if(x1)y = x;else if(1=x&x10)y = 2*x-1;2elsey = 3*x-11;System.out.pri ntl n( y);4.当在命令提示符后输入” java Factorial 3”之后,下列程序的输出结果为:public class Factorial3 public static void main( Stri ng args) int num = In teger.parse In t(args0);int result = 1;if(nu m1)System.out.pri ntl n(” 应输入大于 1 的整数!); elseint

4、i;for( i=1; i=num; i+)result = result*i;System.out.pri ntln (结果是:+result);5._下歹 y 程序的输出结果为:_public class Xpublic static void main( Stri ng args)String names=new String5;for(i nt x=0;xargsen gth;x+)n amesx=argsx;System.out.pri ntln(n ames2);6._下歹 u 程序的输出结果为:public class Testpublic static void main(St

5、ring args) System.out.pri nt ( 3.5+1/2+56%10+);System.out.pri nt ( 3.5+1.0/2+56%10+);int a = 4%3*7+1;3System.out.pri ntln( a );7. 下歹 y 程序的输出结果为:_public class A1 public static void main(String args) int a= new int 5;a0=1;a1=2;a2=3;a3=4;a4=5;System.out.print(a+0+ = +a0+);System.out.pri nt(a+1+ = +a1+)

6、;System.out.pri ntl n( a+2+ = +a2+);System.out.pri nt(a+3+ = +a3+);System.out.pri nt(a+4+ = +a4);8. 下歹 y 程序的输出结果为: _public class array1D_1 public static void main(String args) int i;int a;a = new in t5;for(i=0;i=O;i_)System.out.print(“ a “ +i + ”= t + i +”,9. 运行结果是:_public class Test1 Stri ng str =

7、new Stri ng(Hi !);char ch = L, i, k, e ;public static void main(String args) Test1 ex = new Test1();System. out .print(ex.str + );System. out .print(ex.ch);10._运行结果是:public class Test3 4public static void main( Stri ng args) int i;for (i = 3; i = 20; i +) if (i % 2) = 0)System. out .print(i + );11._

8、 运行结果是:public class Test4 public static void main(String args) int i,sum=0;for (i = 3; i = 20; i +)if (i % 2) = 0) sum=sum+i;System. out.print(i=+i+,sum=+sum);12._ 运行结果是:public class Test5 public static void main(String args) int i,sum=0;for (i = 1; i = 100; i +) if (i % 2) = 1) sum=sum+i;System.out

9、.pri ntln( sum);13.运行结果是class Test6 public static void main( Stri ng args) int i;5for(i=0;i10;i+)System.out.pri nt(i+);if (i%2!=0) continue;System.out.pri ntln(” );14、运行结果是: public class Test14 public static void main(String args) int n = 1, m, j, i;for (i = 3; i = 30; i += 2) m = (int) Math.sqrt(do

10、uble) i);for (j = 2; j = m + 1) System.out.pri nt(i + );if (n % 5 = 0)System.out.pri nt(n);n+;15. 下歹 y 程序的输出结果为: _public class Testi public static void main(String args) int y, x=1, total=0;while(x=10) y=x*x;System.out.pri nt(y+);total+=y;+x;System.out.pri nt(total is +total);616. 下歹 y 程序的输出结果为: _pu

11、blic class WhileExamplepublic static void main(String args)String copyFromMe=Copy every letter un til you encoun ter u.;Stri ngBufer copyToMe=new Stri ngBuffer();/ 创建一个空的串变量int i=0;char c=copyFromMe.charAt(i);while(c!=u) copyToMe.appe nd(c);c=copyFromMe.charAt(+i); System.out.pri ntl n( copyToMe);17

12、. 下歹 y 程序的输出结果为: _public class BreakExamplepublic static void main(String args)in t array=32,87,3,589,12,1076,2000,8,622;int search = 1076, i=0, j=0;boolea n foun dIt = false;for (i=0; i3; i+) for (j=0; j3; j+)if (arrayij = search) foun dIt = true; break; / 跳出内层循环if(foundlt) break; / 跳出外层循环if(fou nd

13、lt)System.out.println(Found + search + at i = + i +, j = + j); elseSystem.out.println(search + is not in the array);18. 下歹 y 程序的输出结果为: _public class SumDemo2 public static void main( Stri ng args) int sum = 0, i = 0;doif(i%2=0)sum = sum+i;i+;while(i=20);System.out.pri ntl n(其最终和是:+sum);719. 下歹 u 程序的

14、输出结果为:_public class AddTest public static void main(String args) int nu m1, nu m2;num1 = 10;num2 = 20;System.out.pri ntln(n um1 + + + num2 + = + add( num1, num2); static int add(i nt op1,i nt op2)retur n op1+op2;20._ 下歹 y 程序的输出结果为: _ class gen eralFunctionpublic static int add(int x,int y) return x+

15、y; 静态方法public class StaticF un Testpublic static void main(String args)int c=ge neral Function. add(7, 8);System.out.pri nt(7 + 8 = + c+“,“);gen eralF unction fun=new generalF un cti on();int d= fun.add(5, 8);System.out.pri ntln(5 + 8 = + d);21. 下歹 y 程序的输出结果为 _class Boxint len gth,width,height;publi

16、c void set In fo(i nt l,i nt w,i nt h)len gth = l;width = w;height = h;public int volu mn()retur n len gth*width*height;public int area()return (le ngth*width + len gth*height + width*height) * 2;8public Stri ng toStri ng()return Le ngth: + len gth + width: + width + height: + height+ volum n: + vol

17、u mn() + area: + area();public class BoxTest public static void main(String args) Box b = new Box();b.setl nfo(5,2,4);System.out.pri ntl n( b.toStri ng();22. 下歹 y 程序的输出结果为: _public class ExampleString str=new Strin g(good);charch=a,b,c;public static void main(String args)Example ex=new Example。;ex.c

18、ha nge(ex.str,ex.ch);9System.out.pri nt(ex.str+ and ”);System.out.pri nt(ex.ch);public void change(String str,char ch)str=test ok;ch0=g:23. 下歹 y 程序的输出结果为: _arrayParam1.java以数组为参数的方法调用public class arrayParam1public static void mai n(Stri ng args )int a = 8, 3, 7, 88, 9, 23;LeastNumb MinNu mber= new L

19、eastNumb();MinNu mber.least(a);class LeastNumbpublic void least(i nt array )int temp = array0;for(i nt i=0;iarrayi) temp = arrayi;System.out.println(” 最小的数为:+ temp);24. 运行结果是:(B6)(p50 例 4.11)import java.awt.*; import java.applet.*; class MemberVar static int sn = 30; final int fn; final int fk = 40;

20、 MemberVar() 10fn = + sn;public class TestIO exte nds Applet public void pain t(Graphics g) MemberVar obj1 = new MemberVar();MemberVar obj2 = new MemberVar();g.drawString(obj1.fn= + obj1.fn, 20, 30);g.drawStri ng(obj1.fk= + obj1.fk, 20, 50);g.drawStri ng(obj2.fn= + obj2.fn, 20, 70);g.drawStri ng(obj

21、2.fk= + obj2.fk, 20, 90);25. 下歹 y 程序的输出结果为: _class Astatic int y=3;void showy( )System.out.pri ntln (y=+y); class testApublic static void main(String aaa) A a1= new A();A.y+=1;a1.y+;a1.showy();26. 运行结果是:_public class Test9 static boolea n foo(char c) System.out.pri nt(c);return true;public static vo

22、id main( String argv ) int i =0;for ( foo(B); foo(A)&(i2); foo(C)i+ ;11foo(D);27._运行结果是:import java.io.*;public class Test2public staticvoid main( Stri ng args) Sub sb = new Sub();System.out.pri ntln( sb.method( );class Fatherint a=3 , b=2 ;int method() return a-b; class Subexte ndsFatherint met

23、hod2( ) return a+b; 28._下歹 y 程序的输出结果为 _class Aclassvoid go()System.out.pri ntl n( Aclass);public class Bclass exte nds Aclassvoid go()System.out.pri ntl n( Bclass);public static void main(String args) Aclass a=new Aclass(); Aclass a1= newBclass();12a.go();a1.go();29. 下歹 u 程序的输出结果为:_class Ball privat

24、e double r;public void setR(double x)r = x;public double getR()return r;class Billiards exte nds Ballprivate String color;public void setCol(Stri ng col)color = col;public void show()System.out.println(” 台球的颜色是+color+,半径是+getR();public class Demopublic static void main(String args)Ball b1 = new Ball

25、();b1.setR(10);System.out.println(” 球的半径是+b1.getR();Billiards b2 = new Billiards();b2.setR(5);b2.setCol(蓝色);b2.show();30. 运行结果是:public class Test12 exte nds TTpublic static void main(String args) 13Test12 t = new Test12 (Tom);public Test12(Stri ng s)super(s);System.out.pri ntln (How do you do?);publ

26、ic Test12()this(I am Tom);class TTpublic TT()System.out.pri ntln (What a pleasure!);public TT(Stri ng s)this();System.out.println(I am +s);31.运行结果是: public class Test13 exte nds TTpublic static void main(String args) Test13 t = new Test13 ();public Test13(Stri ng s)super(s);System.out.pri ntln( How

27、do you do?);public Test13()this(I am Tom);class TT14public TT()System.out.pri ntln (What a pleasure!);public TT(Stri ng s)this();System.out.println(I am +s);32. 下列程序的输出结果为 :_in terface IShapepublic double area();class square impleme nts IShapedouble len gth ;public square(double l)len gth = l;public

28、 double area()retur n len gth*le ngth;public class Test public static void main(String args) square s = new square(5);System.out.pri ntl n( s.area();33. 运行结果是:_class Test11class Dog private String name;private int age;public int step;Dog(Stri ng s, i nt a) n ame = s;age = a;step = 0;public void run

29、(Dog fast) 15fast.step+;public static void main(String args) Test11 a = new Test11();Dog d = a. new Dog(Kate, 5);d.step = 35;d.ru n(d);System. out .println( + d.step);34._运行结果是:public class Test15 private static int count;private String name;public class Stude nt private int count;private String nam

30、e;public void Output(i nt count) coun t+;this.co un t+;Test15.co un t+;Test15.this.co un t+;System.out.pri ntl n(count + + this.co unt + +Test15.cou nt + + Test15.this.cou nt);public Student aStu() return new Stude nt();public static void main(String args) Test15 g3 = new Test15();g3.cou nt = 10;Tes

31、t9.Stude nt s1 = g3.aStu();16s1.0utput(5);35. 下歹 y 程序的输出结果为: _public class Q1public static void main(String args)trythrow new MyExceptio n();catch(Excepti on e)System.out.println(It s caught! ” );Fi nallySystem.out.pri ntl n(It s fin ally caught! ” );class MyExcepti on exte nds Excepti on36. 运行结果是:p

32、ublic class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();catch(Excepti on e)System.out.pri ntln (exceptio nOOO);fin allySystem.out.pri ntln (fi nally111);public static void main(String args)tryaMethod();catch(Excepti on e)17System.out.pri ntl n(exceptio n);System.o

33、ut.pri ntln (fi ni shed);37. 运行结果是:public class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();fin allySystem.out.pri ntln (fi nally111);public static void main(String args)tryaMethod();catch(Excepti on e)System.out.pri ntl n(excepti on);System.out.pri ntln (fi ni sh

34、ed);38. 运行结果是:public class Test10 public static void aMethod() throws Excepti on trythrow new Excepti on();18catch(Excepti on e)System.out.pri ntln (exceptio nOOO);public static void main(String args)tryaMethod();catch(Excepti on e)System.out.pri ntl n(exceptio n); System.out.pri ntln (fi ni shed);39._运行结果是:public class Test1 public static String output =;public static void foo(i nt i)tryif(i=1)throw new Exception(); output += 1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论