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

下载本文档

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

文档简介

1、最新Java运行程序写结果(学生版)Java练习题二、运行程序写结果1. 当在命令提示符后输入java SumTest 4 5”之后,以下程序的输出结果为: public class SumTest public static void main(String args) int a, b, sum; a=Integer.parseInt(args0); b=Integer.parseInt(args1); sum=a+b; System.out.println(a + + + b + = + sum); 2. 当在命令提示符后输入java PrintStar 8”之后,以下程序的输出结果为:

2、 public class PrintStar public static void main(String args) int n = Integer.parseInt(args0);int i;for( i=1; i=n; i+)System.out.print(*);System.out.println();3. 当在命令提示符后输入java CalculateTest 5.5”之后,以下程序的输出结果为: public class CalculateTestpublic static void main(String args)float x,y; x = Float.parseFlo

3、at(args0); if(x1)y = x;else if(1=x&x10)y = 2*x-1;elsey = 3*x-11;System.out.println(y);4. 当在命令提示符后输入java Factorial3 3”之后,以下程序的输出结果为: public class Factorial3 public static void main(String args) int num = Integer.parseInt(args0);int result = 1;if(num1)System.out.println(应输入大于1的整数!);elseint i;for( i=1;

4、 i=num; i+)result = result*i;System.out.println(结果是:+result);5. 以下程序的输出结果为: public class X public static void main(String args) String names=new String5; for(int x=0;xargs.length;x+) namesx=argsx; System.out.println(names2); 6. 以下程序的输出结果为: public class Testpublic static void main(String args) System

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

6、rintln(a+2+ = +a2+ ); System.out.print(a+3+ = +a3+ ); System.out.print(a+4+ = +a4); 8. 以下程序的输出结果为: public class array1D_1 public static void main(String args) int i;int a ; a = new int5; for(i=0;i=0;i-) System.out.print(“a“ +i + = + ai + ,t);9. 运行结果是: public class Test1 String str = new String(Hi !)

7、;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 public static void main(String args) int i;for (i = 3; i = 20; i +) if (i % 2) = 0)System.out.print(i + );11. 运行结果是: public class Test4

8、 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.println(sum);13. 运行结果是: c

9、lass Test6 public static void main(String args) int i; for(i=0;i10;i+) System.out.print(i+ ); if (i%2!=0) continue; System.out.println( ); 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(double) i);for (j = 2; j =

10、m + 1) System.out.print(i + );if (n % 5 = 0)System.out.print(n);n+;15. 以下程序的输出结果为: public class Test1 public static void main(String args) int y, x=1, total=0; while(x=10) y=x*x; System.out.print(y+ ); total+=y; +x; System.out.print(total is +total); 16. 以下程序的输出结果为: public class WhileExamplepublic s

11、tatic void main(String args)String copyFromMe=Copy every letter until you encounter u.; StringBuffer copyToMe=new StringBuffer(); /创立一个空的串变量int i=0;char c=copyFromMe.charAt(i); while(c!=u) copyToMe.append(c); c=copyFromMe.charAt(+i); System.out.println(copyToMe);17. 以下程序的输出结果为: public class BreakExa

12、mplepublic static void main(String args)int array=32,87,3,589,12,1076,2000,8,622;int search = 1076, i=0, j=0;boolean foundIt = false;for (i=0; i3; i+) for (j=0; j3; j+)if (arrayij = search) foundIt = true; break; /跳出内层循环if(foundIt) break; /跳出外层循环if(foundIt) System.out.println(Found + search + at i =

13、 + i +, j = + j);else System.out.println(search + is not in the array);18. 以下程序的输出结果为: public class SumDemo2 public static void main(String args) int sum = 0, i = 0;doif(i%2=0)sum = sum+i;i+;while(i=20);System.out.println(其最终和是:+sum);19. 以下程序的输出结果为: public class AddTest public static void main(Strin

14、g args) int num1,num2;num1 = 10;num2 = 20;System.out.println(num1 + + + num2 + = + add(num1,num2);static int add(int op1,int op2)return op1+op2;20. 以下程序的输出结果为: class generalFunctionpublic static int add(int x,int y) return x+y; /静态方法public class StaticFunTestpublic static void main(String args)int c

15、=generalFunction.add(7, 8);System.out.print(7 + 8 = + c+ “, “);generalFunction fun=new generalFunction();int d= fun.add(5, 8); System.out.println(5 + 8 = + d);21. 以下程序的输出结果为 class Boxint length,width,height;public void setInfo(int l,int w,int h)length = l;width = w;height = h;public int volumn()retu

16、rn length*width*height;public int area()return (length*width + length*height + width*height) * 2;public String toString()return Length: + length + width: + width + height: + height + volumn: + volumn() + area: + area();public class BoxTest public static void main(String args) Box b = new Box(); b.se

17、tInfo(5,2,4);System.out.println(b.toString();22. 以下程序的输出结果为: public class Example String str=new String(good); charch=a,b,c; public static void main(String args) Example ex=new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str+ and ); System.out.print(ex.ch); public void change(String str,

18、char ch) str=test ok; ch0=g; 23. 以下程序的输出结果为: /arrayParam1.java 以数组为参数的方法调用public class arrayParam1 public static void main(String args )int a = 8, 3, 7, 88, 9, 23; LeastNumb MinNumber=new LeastNumb();MinNumber.least(a); class LeastNumbpublic void least(int array ) int temp = array0;for(int i=0;iarra

19、yi) 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;MemberVar() fn = +sn;public class Test10 extends Applet public void paint(Graphics g) MemberVar obj1 = new MemberVar(

20、); MemberVar obj2 = new MemberVar(); g.drawString(obj1.fn= + obj1.fn, 20, 30); g.drawString(obj1.fk= + obj1.fk, 20, 50); g.drawString(obj2.fn= + obj2.fn, 20, 70); g.drawString(obj2.fk= + obj2.fk, 20, 90);25. 以下程序的输出结果为: class A static int y=3; void showy( )System.out.println(y=+y); class testA publi

21、c static void main(String aaa) A a1=new A( ); A.y+=1; a1.y+; a1.showy( ); 26. 运行结果是: public class Test9 static boolean foo(char c) System.out.print(c); return true; public static void main( String argv ) int i =0; for ( foo(B); foo(A)&(i2); foo(C) i+ ; foo(D); 27. 运行结果是: import java.io.*;public clas

22、s Test2 public static void main(String args) Sub sb = new Sub( ); System.out.println(sb.method( ); class Father int a=3 , b=2 ; int method() return a-b; class Sub extends Father int method2( ) return a+b; 28. 以下程序的输出结果为 class Aclass void go() System.out.println(Aclass); public class Bclass extends A

23、class void go() System.out.println(Bclass); public static void main(String args) Aclass a=new Aclass(); Aclass a1=new Bclass(); a.go(); a1.go(); 29. 以下程序的输出结果为: class Ball private double r;public void setR(double x)r = x;public double getR()return r;class Billiards extends Ballprivate String color;p

24、ublic void setCol(String col)color = col;public void show()System.out.println(台球的颜色是+color+,半径是+getR();public class Demopublic static void main(String args)Ball b1 = new Ball();b1.setR(10);System.out.println(球的半径是+b1.getR();Billiards b2 = new Billiards();b2.setR(5);b2.setCol(蓝色);b2.show();30. 运行结果是:

25、 public class Test12 extends TTpublic static void main(String args) Test12 t = new Test12 (Tom);public Test12(String s)super(s);System.out.println(How do you do?); public Test12()this(I am Tom); class TT public TT() System.out.println(What a pleasure!); public TT(String s)this();System.out.println(I

26、 am +s);31. 运行结果是: public class Test13 extends TTpublic static void main(String args) Test13 t = new Test13 ( );public Test13(String s)super(s);System.out.println(How do you do?); public Test13()this(I am Tom); class TT public TT() System.out.println(What a pleasure!); public TT(String s)this();Syst

27、em.out.println(I am +s);32. 以下程序的输出结果为: interface IShapepublic double area();class square implements IShapedouble length ;public square(double l)length = l;public double area()return length*length;public class Test public static void main(String args) square s = new square(5); System.out.println(s.a

28、rea();33. 运行结果是: class Test11class Dog private String name;private int age;public int step;Dog(String s, int a) name = s;age = a;step = 0;public void run(Dog fast) fast.step+;public static void main(String args) Test11 a = new Test11();Dog d = a.new Dog(Kate, 5);d.step = 35;d.run(d);System.out.print

29、ln( + d.step);34. 运行结果是: public class Test15 private static int count;private String name;public class Student private int count;private String name;public void Output(int count) count+;this.count+;Test15.count+;Test15.this.count+;System.out.println(count + + this.count + + Test15.count + + Test15.t

30、his.count);public Student aStu() return new Student();public static void main(String args) Test15 g3 = new Test15();g3.count = 10;Test9.Student s1 = g3.aStu();s1.Output(5); 35. 以下程序的输出结果为: public class Q1 public static void main(String args) trythrow new MyException(); catch(Exception e)System.out.p

31、rintln(“Its caught!);FinallySystem.out.println(“Its finally caught!);class MyException extends Exception36. 运行结果是: public class Test10 public static void aMethod() throws Exceptiontrythrow new Exception();catch(Exception e)System.out.println(exception000);finallySystem.out.println(finally111);public

32、 static void main(String args)tryaMethod();catch(Exception e)System.out.println(exception);System.out.println(finished);37. 运行结果是: public class Test10 public static void aMethod() throws Exceptiontrythrow new Exception();finallySystem.out.println(finally111);public static void main(String args)tryaM

33、ethod();catch(Exception e)System.out.println(exception);System.out.println(finished);38. 运行结果是: public class Test10 public static void aMethod() throws Exceptiontrythrow new Exception();catch(Exception e)System.out.println(exception000);public static void main(String args)tryaMethod();catch(Exception e)System.out.println(exception);System.out.println(finished);39. 运行结果是: public class Test1 public static String output = ;public static void foo(int i)tryif(i=1)throw new Exception();output +=

温馨提示

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

评论

0/150

提交评论