《Java编程 (2).pdf》由会员分享,可在线阅读,更多相关《Java编程 (2).pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/求两个数的平均数,平方根,平方packagepackage shixun;importimport java.util.Scanner;publicpublic classclass Shixun1 publicpublic staticstatic voidvoid main(String args)Scanner sc=newnew Scanner(System.in);doubledouble a=sc.nextDouble();doubledouble b=sc.nextDouble();doubledouble c=(a+b)/2;doubledouble d1=Math.sqrt
2、(a);doubledouble d2=Math.sqrt(b);doubledouble c0=a*a;System.out.println(c+t+d1+t+d2+t+c0);packagepackage shixun;/输出a,b及其ASCIIpublicpublic classclass Shixun2 publicpublic staticstatic voidvoid main(String args)charchar x1=a;intint x2=97;intint y2=x1+1;charchar y1=(charchar)y2;System.out.println(x1+t+
3、x2+t+y1+t+y2);/判断三个数的最大数packagepackage shixun;importimport java.util.Scanner;publicpublic classclass Shixun3 publicpublic staticstatic voidvoid main(String args)Scanner sc=newnew Scanner(System.in);intint a=sc.nextInt();intint b=sc.nextInt();intint c=sc.nextInt();intint max=a;ifif(ab)ifif(ac)max=a;e
4、lseelsemax=c;elseelseifif(bc)max=b;elseelsemax=c;System.out.print(max);packagepackage shixun;/显示200以内能被5或6整除,但不能被两者同时整除的数publicpublic classclass Shixun4 publicpublic staticstatic voidvoid main(String args)intint k=0;forfor(intint i=1;i200;i+)ifif(i%5=0)&(i%6!=0)|(i%5!=0)&(i%6=0)System.out.print(i+t)
5、;k+;ifif(k%5=0)System.out.println();packagepackage shixun;/1!到10!publicpublic classclass Shixun5 publicpublic staticstatic voidvoid main(String args)intint i=1,j=1,sum=0;dodoj=j*i;sum=sum+j;i+;whilewhile(i=10);System.out.println(sum);/编程实现简易计算器,根据+-*/运算符,对两数执行相应的运算packagepackage shixun;importimport
6、java.util.Scanner;publicpublic classclass Shixun6 publicpublic staticstatic voidvoid main(String args)Scanner input=newnew Scanner(System.in);System.out.print(请输入第一个操作数:);doubledouble num1=input.nextInt();System.out.print(请输入第二个操作数:);doubledouble num2=input.nextInt();System.out.print(运算符:);String fu
7、Hao=input.next();charchar c=fuHao.charAt(0);doubledouble num3=0;switchswitch(c)casecase+:num3=num1+num2;breakbreak;casecase-:num3=num1-num2;breakbreak;casecase*:num3=num1*num2;breakbreak;casecase/:num3=num1/num2;breakbreak;defaultdefault:System.out.println(输入符号错误);System.out.println(num1+fuHao+num2+
8、=+num3);/判断21世纪闰年的个数packagepackage shixun;publicpublic classclass Shixun7 publicpublic staticstatic voidvoid main(String args)intint k=0;forfor(intint i=2000;i=2100;i+)ifif(i%400=0|i%4=0&i%100!=0)System.out.print(i+t);k+;ifif(k%5=0)System.out.println();System.out.println(21世纪闰年的个数为+k);/对输入的数遇到9999停止
9、输入并显示之前数的和packagepackage shixun;importimport java.util.Scanner;publicpublic classclass Shixun8 publicpublic staticstatic voidvoid main(String args)intint sum=0;intint a;Scanner sc=newnew Scanner(System.in);whilewhile(a=sc.nextInt()!=9999)sum+=a;System.out.println(sum);packagepackage shixun;classclas
10、s MyCal intint sum(intint m)intint s=0;forfor(intint i=1;i=100;i+)s+=i;returnreturn s;intint sum(intint m,intint n)intint t=0;ifif(mn)forfor(intint i=m;in)forfor(intint i=n;i=m;i+)t+=i;returnreturn t;publicpublic staticstatic voidvoid main(String args)MyCal cc=newnew MyCal();System.out.println(1到100
11、的和为:+cc.sum(100);System.out.println(25到60的和为:+cc.sum(25,60);System.out.println(60到78的和为:+cc.sum(78,60);/利用Triangle类,求三条边长分别为2.5、3.5、4的三角形的周长和面积。packagepackage shixun;classclass Triangle doubledouble a,b,c;Triangle(doubledouble x,doubledouble y,doubledouble z)a=x;b=y;c=z;doubledouble getL()returnretu
12、rn a+b+c;doubledouble getS()doubledouble p=(a+b+c)/2;doubledouble s=Math.sqrt(p*(p-a)*(p-b)*(p-c);returnreturn s;publicpublic staticstatic voidvoid main(String args)System.out.println(newnew Triangle(2.5,3.5,4).getL();System.out.println(newnew Triangle(2.5,3.5,4).getS();/*设计一个类Triangle,分别将三角形的三条边作为属
13、性,并能计算面积(默认为勾三股四弦五的直角三角形,一个参数时默认为等边三角形,三个参数时分别为三条边长)。*/packagepackage shixun;classclass Triangle1doubledouble i;doubledouble j;doubledouble k;Triangle1(doubledouble a)i=j=k=a;doubledouble p=(3*a)/2;doubledouble s=Math.sqrt(p*(p-a)*(p-a)*(p-a);System.out.print(这是一个边长为+a+的等边三角形);System.out.println(面积是
14、+s);Triangle1(doubledouble x,doubledouble y,doubledouble z)i=x;j=y;k=z;doubledouble p=(i+j+k)/2;doubledouble s=Math.sqrt(p*(p-i)*(p-j)*(p-k);System.out.print(这是一个边长为+i+t+j+t+k+的三角形);System.out.println(面积是+s);publicpublic classclass Triangle2 publicpublic staticstatic voidvoid main(String args)newnew Triangle1(3.0);newnew Triangle1(3,4,5);