java 程序设计报告Word格式.docx
- 文档编号:22864945
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:40
- 大小:1.73MB
java 程序设计报告Word格式.docx
《java 程序设计报告Word格式.docx》由会员分享,可在线阅读,更多相关《java 程序设计报告Word格式.docx(40页珍藏版)》请在冰豆网上搜索。
System.out.println("
}
}
Fig2.1
Q3.OutputyourNameandStudentIDNumber.
publicclasst{
Ruanzhenyao"
1341903111"
Fig3.1
Q4.InputfourintitemsnamedAge,Math,EnglishandHistoryfromyourkeyboard,giveapromptifanyitem’sinputisnotcorrect.
importjava.util.Scanner;
publicclasstest_4{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
//ItisaBytestream.
pleaseinputage,math,english,history"
intage=sc.nextInt();
//readaintegervariable
doublemath=sc.nextDouble();
//readaintegervariable
doubleenglish=sc.nextDouble();
//readaintegervariable
doublehistory=sc.nextDouble();
sc.close();
//endtheinput
Try{
System.out.println("
age:
"
+age);
System.out.println("
math"
+math);
english"
+english);
history"
+history);
//Iftheinputisright,thesecodeswillbeexecuted.
catch(NumberFormatExceptione)
//Iftheinputiswrong,thecodesbelowwillbeexecuted.
{
it'
snotanumber"
}
Fig4.1
Q5.ReadArraysUtil.javacarefully,addthepropercommentsintheprogram,andfinallyanalyzetheresultwithyourownwords.
importjava.util.Arrays;
//ItincludesaclassnamedArrays。
importjava.util.Scanner;
publicclasstest_te{
double[]X=newdouble[5];
//Itisadefinitionofaarray,andithasfivesnumbers.
pleaseinputthenumbers:
Scannersc=newScanner(System.in);
for(inti=0;
i<
X.length;
i++)
X[i]=sc.nextDouble();
sc.close();
Arrays.sort(X);
//Theclasscansortthearray.
aftersortX:
+Arrays.toString(X));
double[]Y=Arrays.copyOfRange(X,2,4);
//CopytheelementsofXarraytoYarray,andit’ssubscript
fromtwotofour.
copyY:
+Arrays.toString(Y));
double[]Z=Arrays.copyOf(X,10);
//CopythefirsttenelementsfromXtoZ.Ifthereareno
TenelementsinX,itcanbereplacedbyzero.
copyZ:
+Arrays.toString(Z));
double[]W=newdouble[10];
System.arraycopy(X,1,W,2,4);
//CopytheelementsofXandthesubscriptshouldstartfromone.
ThenputthemintheW,andit’ssubscriptshouldstartfromtwo.
Thenumberoftheelementscopiedisfour.
copyW:
+Arrays.toString(W));
intpos=Arrays.binarySearch(X,5);
//BinarySearchisusedtoreturnthesubscriptoftheelementfive.
if(pos>
0)
5isX'
s"
+pos+"
position"
if(Arrays.equals(Y,W))
//Arrays.equalsisusedtocheckwhetherXarrayisequalstotheYarray.
YisequalstoW"
else
YisNOTequalstoW"
}
Fig5.1
Q6.Inputaparticularyearfromyourkeyboardandcheckwhetheritisaleapyear.
publicclasstest_6{
pleaseinputaparticularyear"
intyear=sc.nextInt();
if((year%4==0)&
&
(year%100==0)||(year%400)==0)
//Iftheparticularyearcanbedividedexactlybyfourandone
hundred,oritcanbedividedexactlybyfourhundred,itisaleapyear.
System.out.println(+year+"
isaleapyear."
//Ifnot,itisn’taleapyear.
else{
System.out.println(+year+"
isnotaleapyear"
}}
Fig6.1
Fig6.2
Q7.Inputaparticularyearandmonthfromyourkeyboard,andthenoutputhowmanydaysthismonthhas.
publicclasstest_7{
pleaseinputamonth"
intmonth=sc.nextInt();
intdays=0;
switch(month)
//Wecanuseswitchtodividedthemonthsintovarioussituations.
{case1:
case3:
case5:
case7:
case8:
case10:
case12:
31days"
break;
case4:
case6:
case9:
case11:
30days"
case2:
if((year%4==0)&
System.out.println("
29days"
//Ifitisaleapyear,Februaryhastwenty-ninedays.
else
28days"
//Ifitisnotaleapyear,Februaryhastwenty-eightdays.
default:
inputiserror"
year"
+month+"
allhave"
+days+"
days"
Fig7.1
Fig7.2
Q8.Inputfivearbitraryintegersfromyourkeyboard,andoutputthemfromsmallesttolargest,notethatonlyifstructurecanbeemployedinyourprogram.
publicclasstest_8{
Scannerin=newScanner(System.in);
inputfivenumbers"
int[]array=newint[5];
//Itisadefinitionofanarray,andithasfivenumbers.
for(inti=0;
i<
5;
i++){
array[i]=in.nextInt();
//Inputfivenumbers.
beforesort:
i++)
System.out.print(array[i]+"
"
//Outputthearraybeforeitissorted.
System.out.println();
for(intj=i;
j<
j++){
if(array[i]>
array[j])
{intt=array[i];
array[i]=array[j];
array[j]=t;
//Comparethefirsttwoelementsandputthesmalloneinthefirstposition.Thencomparetherestlikethis.
aftersort:
for(inta=0;
a<
a++){
System.out.print(array[a]+"
"
//Useaforlooptooutputthearray.
System.out.println();
Fig8.1
Q9.Sortanintarraybythebubbleandselectionalgorithms,thecapacityofthearrayshouldbenolessthan10.
publicclasstest_9{
int[]array={12,23,4,7,9,42,19,38,33,20,10,47};
//Itisadefinitionofaknownarray.
12;
i++){
intmin=i;
for(intj=i+1;
j<
j++){
if(array[j]<
array[min]){
min=j;
//Thisisthewaycalledselectionsorting.Firstselectthesmallestelementandputitinthefirstposition.Then,selectthesmallestfromtherestelements,andsoon.
intt=array[i];
array[i]=array[min];
array[min]=t;
selectionsort"
for(inta=0;
a<
array.length;
a++){
System.out.print(array[a]+"
//Useaforloopoutputthearraywhichhasbeensorted.
bubblesort"
for(inta=0;
}
Fig9.1
Q10.Outputallprimesandnon-primesinanintarrayrespectively,thecapacityofthearrayshouldbenolessthan10.
publicclasstest_24{
for(inti=1;
=30;
i++){
booleanflag=true;
for(intj=2;
=i/2;
j++){
if(i%j==0){
flag=false;
//Itisusedtofindalltheprimesfromonetothirty.Everynumbershouldbedividedfromonetoitself.Ifitcanbedividedexactlybyanynumberwhichissmallertoitself,itisnotaprimenumber.Else,itisaprimenumberanditwillbeoutput.
if(flag){
System.out.print("
+i);
booleanflag=false;
flag=true;
//Ifitisnotaprime,outputit.
System.out.print("
Fig10.1
Q11.Inputanintegerfromyourkeyboardandcheckwhetheritisapalindromenumber.
publicclasstest_11{
pleaseinputanumber"
intInput=in.nextInt();
inti=0;
intsum=0;
intr=Input;
while(true)
//Whenthenumberisnotequaltozero,itwillexecutethecodesbelow.
{
i=Input%10;
sum=sum*10+i;
Input/=10;
if(Input==0)break;
//First,takethelastdigitfromthenumberyouinput,andmultiplythisnumberbyten,andsoon.Thenyouwillgetareversenumberofit.Ifthereversenumberisequaltothenumberyouhaveinput,itisapalindrome.
if(sum==r)System.out.println("
ispalin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 程序设计报告 程序设计 报告