下载此文档

JAVA编程题--答案.doc


文档分类:IT计算机 | 页数:约6页 举报非法文档有奖
1/6
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/6 下载此文档
文档列表 文档介绍
"HelloWorld!"的程序。参考答案1、lassHelloWorld{ publicvoidsayHello()  {      (“HelloWorld!”)  }publicstaticvoidmain(Stringargs[])  {      HelloWorldhello=newHelloWorld();  }}},根据下列函数编写一个程序,当键盘输入x值时,求出并输出y的值。x(x<=1)y=3x-2(1<x<10)4x(x>=10)参考答案1、import.*;lassX3_4_1{publicstaticvoidmain(String[]args)throwsIOException{ InputStreamReaderisr=newInputStreamReader(); BufferedReaderbr=newBufferedReader(isr); intx,y; x=(()); if(x<=1)y=x; elseif(x<10)y=3*x-2; elsey=4*x; ("x="+x+"\ty="+y);}}参考答案2、import;lassTest1{ publicstaticvoidmain(String[]args){ Scannerread=newScanner(); intx,y; x=(); if(x<=1)y=x; elseif(x<10)y=3*x-2; elsey=4*x; ("x="+x+"\ty="+y); }} 3、编写程序,输出1到1000之间所有可以被3整除又可以被7整除的数。lassExercises{ publicstaticvoidmain(String[]args){ intn=1,i; for(i=1;i<=1000;i++) {if((i%3==0)&&(i%7==0)) (i+""); } }} 4、求出100以内的素数,其中最小的素数是2。lassExercises{ publicstaticvoidmain(String[]args){ intn=1,m,j,i; for(i=2;i<=100;i++){ m=(int)((double)i); for(j=2;j<=m;j++) if((i%j)==0) break; if(j>=m+1){ (i+""); if(n%5==0) ("\n"); n++; } } }           }5、编写一个JavaApplication,要求从键盘输入的10个整数存放到一维数组中,并输出其中的最大、最小以及平均值。import.*;lassExercises{ publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbr=newBufferedReader(newInputStreamReader()); intmax,min,i;inta[]=newint[10];doubleave,sum=0; for(i=0;i<10;i++){("请输入第"+(i+1)+"个数:"); a[i]=(());sum=sum+a[i];}ave=sum/;max=min=a[0];for(i=1;i<;i++){

JAVA编程题--答案 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数6
  • 收藏数0 收藏
  • 顶次数0
  • 上传人梅花书斋
  • 文件大小40 KB
  • 时间2020-02-12