欢迎来到天天文库
浏览记录
ID:32168788
大小:66.00 KB
页数:7页
时间:2019-02-01
《浙大java实验题答案14answer》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验14类的设计和实验题库中未做的题1.程序填空题,不要改变与输入输出有关的语句。30009求一元二次方程的根(选做)输入一个正整数repeat(02、ath.sqrt(d))/(2*a))x2=值2(值2为(-b-Math.sqrt(d))/(2*a))(5)x1=值1+值2ix2=值1-值2i(值1为-b/(2*a),值2为Math.sqrt(-d)/(2*a))输入输出示例:括号内为说明输入:5(repeat=5)000(a=0,b=0,c=0)001(a=0,b=0,c=1)025(a=0,b=2,c=5)218935(a=21,b=89,c=35)223(a=2,b=2,c=3)输出:a=b=c=0,meaninglessa=b=0,c!=0,errorx=-2.5x1=-3、0.44x2=-3.8x1=-0.5+1.12ix2=-0.5-1.12iimportjava.util.Scanner;publicclassTest30009{publicstaticvoidmain(Stringargs[]){intrepeat,ri;inta,b,c,d;doublex1,x2;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){a=in.nextInt();b=in.nextInt();c=in.next4、Int();/*---------*/if(a==0&&b==0&&c==0)System.out.println("a=b=c=0,meaningless");elseif(a==0&&b==0&&c!=0)System.out.println("a=b=0,c!=0,error");elseif(a==0&&b!=0){x1=-(double)c/b;System.out.println("x="+Math.round(x1*100)/100.0);}else{d=b*b-4*a*c;if(d>=0){x1=(-b+Math.sq5、rt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*a);System.out.println("x1="+Math.round(x1*100)/100.0);System.out.println("x2="+Math.round(x2*100)/100.0);}else{x1=-(double)b/(2*a);x2=Math.sqrt(-d)/(2*a);System.out.println("x1="+Math.round(x1*100)/100.0+"+"+Math.round(x2*100)/100.0+6、"i");System.out.println("x2="+Math.round(x1*100)/100.0+"-"+Math.round(x2*100)/100.0+"i");}}}}}说明:为防止出错,在编程前应考虑周全²当a=b=c=0时,方程有无数解;²当a=b=0,c!=0时,方程无解;²当a=0,b!=0时,方程解为x=-c/b;²当a!=0时,求:d=b*b-4*a*c若d>=0,有两个不同实根:x=(-b±Math.sqrt(d))/(2*a)若d<0,有两个不同复根:x=-b/(2*a)±Math.sqrt(-d)/7、(2*a)i或:if(a==0){if(b==0){if(c==0)System.out.println("a=b=c=0,meaningless");elseSystem.out.println("a=b=0,c!=0,error");}else{x1=-(double)c/b;System.out.println("x="+Math.round(x1*100)/100.0);}}else{d=b*b-4*a*c;if(d>=0){x1=(-b+Math.sqrt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*8、a);System.out.println("x1="+Math.round(x1*100)/100.0);System.out.println("x2="+Math.round(x2*100)/100.0);}else
2、ath.sqrt(d))/(2*a))x2=值2(值2为(-b-Math.sqrt(d))/(2*a))(5)x1=值1+值2ix2=值1-值2i(值1为-b/(2*a),值2为Math.sqrt(-d)/(2*a))输入输出示例:括号内为说明输入:5(repeat=5)000(a=0,b=0,c=0)001(a=0,b=0,c=1)025(a=0,b=2,c=5)218935(a=21,b=89,c=35)223(a=2,b=2,c=3)输出:a=b=c=0,meaninglessa=b=0,c!=0,errorx=-2.5x1=-
3、0.44x2=-3.8x1=-0.5+1.12ix2=-0.5-1.12iimportjava.util.Scanner;publicclassTest30009{publicstaticvoidmain(Stringargs[]){intrepeat,ri;inta,b,c,d;doublex1,x2;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){a=in.nextInt();b=in.nextInt();c=in.next
4、Int();/*---------*/if(a==0&&b==0&&c==0)System.out.println("a=b=c=0,meaningless");elseif(a==0&&b==0&&c!=0)System.out.println("a=b=0,c!=0,error");elseif(a==0&&b!=0){x1=-(double)c/b;System.out.println("x="+Math.round(x1*100)/100.0);}else{d=b*b-4*a*c;if(d>=0){x1=(-b+Math.sq
5、rt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*a);System.out.println("x1="+Math.round(x1*100)/100.0);System.out.println("x2="+Math.round(x2*100)/100.0);}else{x1=-(double)b/(2*a);x2=Math.sqrt(-d)/(2*a);System.out.println("x1="+Math.round(x1*100)/100.0+"+"+Math.round(x2*100)/100.0+
6、"i");System.out.println("x2="+Math.round(x1*100)/100.0+"-"+Math.round(x2*100)/100.0+"i");}}}}}说明:为防止出错,在编程前应考虑周全²当a=b=c=0时,方程有无数解;²当a=b=0,c!=0时,方程无解;²当a=0,b!=0时,方程解为x=-c/b;²当a!=0时,求:d=b*b-4*a*c若d>=0,有两个不同实根:x=(-b±Math.sqrt(d))/(2*a)若d<0,有两个不同复根:x=-b/(2*a)±Math.sqrt(-d)/
7、(2*a)i或:if(a==0){if(b==0){if(c==0)System.out.println("a=b=c=0,meaningless");elseSystem.out.println("a=b=0,c!=0,error");}else{x1=-(double)c/b;System.out.println("x="+Math.round(x1*100)/100.0);}}else{d=b*b-4*a*c;if(d>=0){x1=(-b+Math.sqrt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*
8、a);System.out.println("x1="+Math.round(x1*100)/100.0);System.out.println("x2="+Math.round(x2*100)/100.0);}else
此文档下载收益归作者所有