欢迎来到天天文库
浏览记录
ID:41033339
大小:32.50 KB
页数:3页
时间:2019-08-14
《Java编程练习》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C:ProgramDataOracleJavajavapath;1输入一行字符串,统计其中数字的个数。importjava.util.Scanner;publicclasstongji{privatestaticScannersc;privatestaticinta=0;publicstaticvoidmain(String[]args){System.out.print("请输入字符串:");sc=newScanner(System.in);char[]ch=sc.next().toCharArray();System.out.println(ch);System.out.pri
2、ntln("长度为:"+ch.length);}}2、36选7。从1-36共36个整数中随机产生7个整数。classtest{publicstaticvoidmain(String[]args){int[]a=newint[7];for(inti=0;i3、sstest{privatestaticintgetTestScore()throwsInvalidScroreException{Scannerscanner=newScanner(System.in);intscore=scanner.nextInt();if(score>1004、5、score<0){thrownewInvalidScroreException("有效的分数在0~100之内。");}returnscore;}publicstaticvoidmain(String[]args){try{getTestScore();}catch(InvalidScroreExceptio6、nex){System.out.println(ex.getMessage());}}}classInvalidScroreExceptionextendsException{Stringmessage;publicInvalidScroreException(Stringerrormessage){message=errormessage;}publicStringgetMessage(){returnmessage;}}4、计算的值。classtest{publicstaticvoidmain(String[]args){inti;doublesum=1;for(i=2;i<=100;7、i++)sum=sum+1.0/i;System.out.println(sum);}}5、编写一个Pointer类,包含横坐标x和纵坐标y两个成员变量,再给pointer定义两个构造方法(无参数和带有两个参数)和一个打印点坐标的方法Show。classPointer{intx;inty;publicPointer(){x=0;y=0;}publicPointer(inta,intb){x=a;y=b;}publicintGetX(){returnx;}publicintGetY(){returny;}publicvoidShow(){System.out.println(x+""+y);8、}}
3、sstest{privatestaticintgetTestScore()throwsInvalidScroreException{Scannerscanner=newScanner(System.in);intscore=scanner.nextInt();if(score>100
4、
5、score<0){thrownewInvalidScroreException("有效的分数在0~100之内。");}returnscore;}publicstaticvoidmain(String[]args){try{getTestScore();}catch(InvalidScroreExceptio
6、nex){System.out.println(ex.getMessage());}}}classInvalidScroreExceptionextendsException{Stringmessage;publicInvalidScroreException(Stringerrormessage){message=errormessage;}publicStringgetMessage(){returnmessage;}}4、计算的值。classtest{publicstaticvoidmain(String[]args){inti;doublesum=1;for(i=2;i<=100;
7、i++)sum=sum+1.0/i;System.out.println(sum);}}5、编写一个Pointer类,包含横坐标x和纵坐标y两个成员变量,再给pointer定义两个构造方法(无参数和带有两个参数)和一个打印点坐标的方法Show。classPointer{intx;inty;publicPointer(){x=0;y=0;}publicPointer(inta,intb){x=a;y=b;}publicintGetX(){returnx;}publicintGetY(){returny;}publicvoidShow(){System.out.println(x+""+y);
8、}}
此文档下载收益归作者所有