欢迎来到天天文库
浏览记录
ID:37916029
大小:87.00 KB
页数:16页
时间:2019-06-02
《java2经典问题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.百鸡问题;/***@(#)baiji.java***@author*@version1.002010/3/17*/publicclassbaiji{/***Createsanewinstanceofbaiji.*/publicbaiji(){}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereSystem.out.println("百鸡问题"
baiji
2、);intx;inty;intz;for(x=1;x<=100;x++){for(y=1;y<=100;y++){for(z=1;z<=100;z++){if((x+y+z==100)&(5*x+3*y+z/3.0==100)){System.out.print("thenumberofcocksis"+x+";");System.out.print("thenumberofhensis"+y+";");System.out.print("thenumberofchicksis"+z+".");System.out.print
3、ln();}}}}}}2.求自然对数e;/**@(#)E.java1.010/03/17**Youcanmodifythetemplateofthisfileinthe*directory..JCreatorTemplatesTemplate_1Project_Name.java**Youcanalsocreateyourownprojecttemplatebymakinganew*folderinthedirectory..JCreatorTemplate.Usetheother*templatesasexam
4、ples.**/packagemyprojects.e;importjava.awt.*;importjava.awt.event.*;classEextendsFrame{publicE(){addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){dispose();System.exit(0);}});}publicstaticvoidmain(Stringargs[]){doublee=1.0;doublet=1.0;for(i
5、nti=1;i<=20;i++){t/=i;e+=t;}System.out.println("e="+e);/**第二种方法:e=0.0;for(inti=20;i>0;i--){e=(e+1.0)/i;}e++;System.out.println(e);**/}}3.求圆周率pi;/***@(#)pi.java***@author*@version1.002010/3/18*/publicclasspi{/***Createsanewinstanceofpi.*/publicpi(){}/***
pi
6、@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){//TODOcodeapplicationlogicheredoublei;doubles;doublePi;intsign=1;i=1;s=0;Pi=0;while(i<=1000000){s=1/i;i=i+2;s=s*sign;sign=(-1)*sign;Pi=Pi+s;}Pi=Pi*4;System.out.println("Pi="+Pi);}}4.输入字符串;/***@(#)
7、app3_3.java***@author*@version1.002010/3/18*///app3_3.javaimportjava.io.*;publicclassapp3_3{/***Createsanewinstanceofapp3_3.*/publicapp3_3(){}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args)throwsIOException{//TODOcodeapplicatio
app3_3
8、nlogichereBufferedReaderbuf;Stringstr;buf=newBufferedReader(newInputStreamReader(System.in));System.out.print("请输入字符串:");str=buf.readLine(
此文档下载收益归作者所有