java答案.docx
- 文档编号:10541044
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:51
- 大小:22.76KB
java答案.docx
《java答案.docx》由会员分享,可在线阅读,更多相关《java答案.docx(51页珍藏版)》请在冰豆网上搜索。
java答案
importjava.util.*;
publicclassQ1
{
publicstaticvoidmain(String[]args)
{
Scannerinput=newScanner(System.in);
System.out.print("EnteradegreeinCelsius:
");
doublec=input.nextDouble();
doublef;
f=9.0/5*c+32;
System.out.println(c+"Celsiusis"+f+"Fahrenheit");
}
}
importjava.util.*;
publicclassQ2
{
publicstaticvoidmain(String[]args)
{
Scannerinput=newScanner(System.in);
System.out.print("EnteranASCIIcode:
");
intnum=input.nextInt();
charletter=(char)num;
System.out.println("ThecharacterforASCIIcode"+num+"is"+letter);
}
}
importjava.util.*;
publicclassQ3
{
publicstaticvoidmain(String[]args)
{
Scannerinput=newScanner(System.in);
System.out.print("Enteranumberbetween0and1000:
");
intnum=input.nextInt();
intunit=num%10;
intdecade=num/10%10;
inthundred=num/100;
intsum=unit+decade+hundred;
System.out.println("Thesumofthedigitsis"+sum);
}
}
importjava.util.*;
publicclassQ4
{
publicstaticvoidmain(String[]args)
{
finalintMAXSIZE=100;
int[]student=newint[MAXSIZE];
intbest=0;
Scannerinput=newScanner(System.in);
System.out.print("Enterthenumberofstudents:
");
intnum=input.nextInt();
System.out.print("Enter"+num+"scores:
");
for(inti=0;i { student[i]=input.nextInt(); if(student[i]>best)best=student[i]; } for(inti=0;i { if(student[i]>=best-10)System.out.println("Student"+i+"scoreis"+student[i]+"andgradeisA"); elseif(student[i]>=best-20)System.out.println("Student"+i+"scoreis"+student[i]+"andgradeisB"); elseif(student[i]>=best-30)System.out.println("Student"+i+"scoreis"+student[i]+"andgradeisC"); elseif(student[i]>=best-40)System.out.println("Student"+i+"scoreis"+student[i]+"andgradeisD"); elseSystem.out.println("Student"+i+"scoreis"+student[i]+"andgradeisF"); } } } importjava.util.*; publicclassQ1 { publicstaticvoidmain(String[]args) { String[]represent=newString[3]; Scannerinput=newScanner(System.in); Randomrandom=newRandom(); represent[0]="scissor"; represent[1]="rock"; represent[2]="paper"; System.out.print("scissor(0),rock (1),paper (2): "); intcom=random.nextInt(3); intuser=input.nextInt(); if(com==user) { System.out.println("Thecomputeris"+represent[com]+".Youare"+represent[user]+"too.Itisadraw."); } elseif((com==0&&user==1)||(com==1&&user==2)||(com==2&&user==0)) { System.out.println("Thecomputeris"+represent[com]+".Youare"+represent[user]+".Youwin"); } else { System.out.println("Thecomputeris"+represent[com]+".Youare"+represent[user]+".Youlose"); } } } importjava.util.*; publicclassQ2 { publicstaticvoidmain(String[]args) { Scannerinput=newScanner(System.in); booleanresult; System.out.print("Enterthreeedges: "); doublea=input.nextDouble(); doubleb=input.nextDouble(); doublec=input.nextDouble(); System.out.print("Canedges"+a+","+b+",and"+c+"formatriangle? "); if(a+b>c&&a+c>b&&b+c>a)result=true; elseresult=false; System.out.println(result); } } importjava.util.*; publicclassQ3 { publicstaticvoidmain(String[]args) { int[]digit=newint[9]; Scannerinput=newScanner(System.in); System.out.print("InputanintegertocalculateISBN(Onlyninedigits): "); intdata=input.nextInt(); intchecksum=0; inttemp=data; for(inti=0;i<9;i++) { digit[i]=temp%10; temp=temp/10; checksum+=digit[i]*(9-i); } checksum=checksum%11; System.out.print("TheISBNis: "); for(inti=8;i>=0;i--) { System.out.print(digit[i]); } if(checksum==10)System.out.println("X"); elseSystem.out.println(checksum); } } importjava.util.*; publicclassQ1 { publicstaticvoidmain(String[]args) { print("chicks","chick"); print("cows","moo"); print("ducks","quack"); print("pigs","oink"); } publicstaticvoidprint(Stringword1,Stringword2) { System.out.println("OldMacDonaldhadafarmOldMacDonaldhadafarm,eieio"); System.out.println("Andonhisfarmhehadsome"+word1+",eieio"); System.out.println("witha"+word2+word2+"hereanda"+word2+""+word2); System.out.println("there"); System.out.println("herea"+word2+"therea"+word2); System.out.println("everywherea"+word2+""+word2); System.out.println("OldMacDonaldhadafarm,eieio"); System.out.println(""); } } importjava.util.*; publicclassQ2 { publicstaticvoidmain(String[]args) { Scannerinput=newScanner(System.in); System.out.print("InputhowmanyFibonaccinumbersyouwanttoprint: "); intnum=input.nextInt(); for(inti=1;i<=num;i++) { System.out.print(Fibonacci(i)+"\t"); if(i%5==0)System.out.printf("\n"); } } publicstaticintFibonacci(intN) { if(N==1)return0; elseif(N==2)return1; elsereturnFibonacci(N-1)+Fibonacci(N-2); } } importjava.util.*; publicclassQ3 { publicstaticvoidmain(String[]args) { Scannerinput=newScanner(System.in); System.out.print("Enteradecimalinteger: "); intnum=input.nextInt(); Convert(num); } publicstaticvoidConvert(intdecimal) { int[]hexa=newint[20]; intQuotient=decimal; inti=0; while(Quotient! =0) { hexa[i++]=Quotient%16; Quotient=Quotient/16; } i--; System.out.print("Thehaxademicalis: "); for(;i>=0;i--) { if(hexa[i]>9) { switch(hexa[i]) { case10: System.out.print("A");break; case11: System.out.print("B");break; case12: System.out.print("C");break; case13: System.out.print("D");break; case14: System.out.print("E");break; case15: System.out.print("F");break; } } else { System.out.print(hexa[i]); } } System.out.println(""); } } importjava.util.*; publicclassQ4 { publicstaticvoidmain(String[]args) { for(inti=3;i<998;i+=2) { intj=i+2; if(Prime(i)&&Prime(j)) { System.out.println("("+i+","+j+")"); } } } publicstaticbooleanPrime(intnum) { for(inti=2;i<=Math.sqrt(num);i++) { if(num%i==0) { returnfalse; } } returntrue; } } importjava.util.*; publicclassCoutingOccurrenceOfNumbers { staticfinalintMAXSIZE=100; publicstaticvoidmain(String[]args) { Scannerinput=newScanner(System.in); int[]array1=newint[MAXSIZE]; int[]array2=newint[101]; System.out.print("Entertheintegersbetwenn1and100: "); for(inti=0;i<101;i++) { array2[i]=0; } for(inti=0;;i++) { array1[i]=input.nextInt(); array2[array1[i]]++; if(array1[i]==0) { break; } } for(inti=1;i<101;i++) { if(array2[i]! =0) { System.out.println(i+"occurs"+array2[i]+"times"); } } } } importjava.util.*; publicclassEliminatingDuplicate { staticintlength=0; publicstaticvoidmain(String[]args) { int[]numbers=newint[10]; System.out.print("Entertennumbers: "); int[]result=eliminatingDuplicates(numbers); System.out.print("Thedistinctnumbersare: "); for(inti=0;i<=length;i++) { System.out.print(result[i]+""); } } publicstaticint[]eliminatingDuplicates(int[]numbers) { Scannerinput=newScanner(System.in); numbers[0]=input.nextInt(); inttemp; booleanrep=true; for(inti=1;i<10;i++) { temp=input.nextInt(); for(intj=0;j<=length;j++) { if(numbers[j]==temp) { rep=false; break; } } if(rep)numbers[++length]=temp; rep=true; } returnnumbers; } } importjava.util.*; publicclassIdenticalArrays { publicstaticvoidmain(String[]args) { Scannerinput=newScanner(System.in); intn1,n2; System.out.print("Enterlist1: "); n1=input.nextInt(); int[]list1=newint[n1]; for(inti=0;i list1[i]=input.nextInt(); System.out.print("Enterlist2: "); n2=input.nextInt(); int[]list2=newint[n2]; for(inti=0;i list2[i]=input.nextInt(); System.out.println("Twolistsare"+(isIdentical(list1,list2)? "identical": "notidentical")); } publicstaticbooleanisIdentical(int[]list1,int[]list2) { Arrays.sort(list1); Arrays.sort(list2); if(Arrays.equals(list1,list2))returntrue; elsereturnfalse; } } publicclassNSideRegularPolygon { publicstaticvoidmain(String[]args) { RegularPolygonpolygon1=newRegularPolygon(6,4); RegularPolygonpolygon2=newRegularPolygon(10,4,5.6,7.8); System.out.println("Polygon1: "); System.out.println("perimeter: "+polygon1.getPerimeter()+"area: "+polygon1.getArea()); System.out.println("Polygon2: "); System.out.println("perimeter: "+polygon2.getPerimeter()+"area: "+polygon2.getArea()); } } classRegularPolygon { intn; doubleside; doublex; doubley; RegularPolygon() { n=3; side=1; x=0; y=0; } RegularPolygon(intn0,doubles0) { n=n0; side=s0; x=0; y=0; } RegularPolygon(intn1,doubles1,doublex1,doubley1) { n=n1; side=s1; x=x1; y=y1; } doublegetPerimeter() { returnn*sid
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)