GetFactorialff=newGetFactorial();
ff.setInputn(++counter);
nn=ff.getInputn();
System.out.printf("Computethefactorialofnumber%d...\n",
ff.getInputn());
n=ff.factoriall(nn);
sum+=(double)1/n;//********************veryimportant
System.out.printf("Thefactorialof%dis%d\n",nn,n);
System.out.printf("\nTheexponentialapproximationis:
%.4f\n\n",sum);
}
//System.out.printf("\nItsexponentialapproximationis:
%f",sum);
}
}
packagemy.p4;
publicclassGetFactorial{
privateintcounter;
privateintfactorial;
privateintinputn;
publicGetFactorial(){
this.counter=0;
this.factorial=1;
this.inputn=0;
}
publicintgetInputn(){
returninputn;
}
publicvoidsetInputn(intinput){
this.inputn=input;
}
publicintfactoriall(intinput){
input=getInputn();
while(++counter<=input){
factorial*=counter;
}
returnfactorial;
}
}
//P139_4$24
packagemy.p4;
publicclassMystery2{
publicstaticvoidmain(Stringargs[]){
intcount=1;
while(count<=10){
System.out.println(count%2==1?
"****":
"++++++++");
++count;
}//endwhile
introw=10;
intcolumn;
while(row>=1){
column=1;
//P139_4$26
while(column<=10){
System.out.print(row%2==1?
"<":
">");
++column;
}//endwhile
--row;
System.out.println();
}//endwhile
}//endmain
}//endclassMystery2
packagemy.p4;
publicclassP135_4$5{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
intx=1;
intsum=0;
sum+=1;
System.out.printf("Thesumis:
%d",sum);
//sum1~10
intsum1=0,y=1;
//*inty=1;
while(y<=10){
sum1+=y;
++y;
//intsum,x,sum1;*/
//++sum1;
System.out.printf("\nThesumis:
%d",sum1);
}
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP136_4$17{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
inttanks=0;
doubleeven;
intgallons=0;
intmiles=0;
intincreasedpetrol;
intincreasedmiles;
System.out.print("startthelooptangks=...\n");
while(++tanks<=3){
System.out.printf("tanks=%d\n",tanks);
System.out.print("inputthepetrolcost:
");
increasedpetrol=input.nextInt();
gallons=gallons+increasedpetrol;
System.out.print("inputthemilespast:
");
increasedmiles=input.nextInt();
miles=miles+increasedmiles;
}
even=(double)gallons/miles;
System.out
.printf("Thetotalnumberoftankofpetrolcostis%d,whichmeansthat%.2fgallonpetrolwerecostpermile.",
tanks,even);
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP136_4$18{
/**
注意花括号再多行中的作用,理解static关键字的含义
*/
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
intsn;
intremainder;
inttotal;
intincome;
System.out.print("\nInputtheserialnumber:
");
sn=input.nextInt();
if(sn==1){
System.out.println("\nInputyourincomeofthismonth:
");
income=input.nextInt();
remainder=1000;
total=remainder+income;
System.out
.printf("theremainderofthismonthis%d,\nAddtheincome%d,\nThetotalof%dis%d",
remainder,income,sn,total);
System.out.print("\ncheckouttheCREDITLIMITED...");
checkCreditLmited(total);
}
if(sn==2){
remainder=600;
System.out.println("\nInputyourincomeofthismonth:
");
income=input.nextInt();
total=remainder+income;
System.out
.printf("theremainderofthismonthis%d,\nAddtheincome%d,\nThetotalof%dis%d",
remainder,income,sn,total);
System.out.print("\ncheckouttheCREDITLIMITED...");
checkCreditLmited(total);
}
if(sn==3){
remainder=500;
System.out.println("\nInputyourincomeofthismonth:
");
income=input.nextInt();
total=remainder+income;
System.out
.printf("theremainderofthismonthis%d,\nAddtheincome%d,\nThetotalof%dis%d",
remainder,income,sn,total);
System.out.print("\ncheckouttheCREDITLIMITED...");
checkCreditLmited(total);
}
}
//
publicstaticvoidcheckCreditLmited(inttotal){
if(total<500)
System.out.print("\n********CREDITLIMITEDExceeded********");
if(total>=500)
System.out.print("\n********YOUstillhaveCREDIT*********");
System.out.printf("\nYourcreditis:
%d",total-500);
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP138_4$19{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannerinput=newScanner(System.in);
intsalarybase=200;
intsalesamount1,salesamount2,salesamount3,salesamount4;
doublesaleprice1=239.99,saleprice2=129.75,saleprice3=99.95,saleprice4=350.89;
doubletotalsalary;
System.out.print("InputthesalesamountofFOURtypesofproductsofasaler\n\n");
System.out.print("inputthesalesamountofthefirsttypeofproductsofasaler:
\n");
salesamount1=input.nextInt();
System.out.print("inputthesalesamountofthesecondtypeofproductsofasaler:
\n");
salesamount2=input.nextInt();
System.out.print("inputthesalesamountofthethirdtypeofproductsofasaler:
\n");
salesamount3=input.nextInt();
System.out.print("inputthesalesamountofthefourthtypeofproductsofasaler:
\n");
salesamount4=input.nextInt();
System.out.print("\n\n\nComputinghistotalsalary...\n\n\n");
totalsalary=salarybase+(salesamount1*saleprice1+salesamount2*saleprice2+salesamount3*saleprice3+salesamount4*saleprice4)*0.09;
System.out.printf("\nThetotalsalaryis%.2f",totalsalary);
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP139_4$20{
/**
注意print类型问题
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannerinput=newScanner(System.in);
Stringname=null,nameinput;
intworkinghour=0;
intcounter=1;
doublesalary=0;
intsalaryperhour=10;
while(counter<=3){
System.out.print("\ninputtheworker'sname\n");
nameinput=input.next();
name=nameinput;
System.out.print("inputtheworker'sworkinghour\n");
workinghour=input.nextInt();
if(workinghour>40){
salary=(workinghour-40)*salaryperhour/2+40
*salaryperhour;
}
if(workinghour<=40){
salary=workinghour*salaryperhour;
}
System.out
.printf("\n\nThe%dthworker%sworking%dhoursperweek.\nAndhissalaryis%.2f\n\n\n",
counter,name,workinghour,salary);
++counter;
}
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP139_4$21{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannerinput=newScanner(System.in);
intcounter=0;
intnumber;//input
intlargest=0;
intmaxcounter=0;
while(++counter<=10){
System.out.printf("\n\nInputthe%dthnumber:
\n",counter);
number=input.nextInt();
if(number>largest){
largest=number;
System.out.printf("Thelargestnumberis%d",largest);
if(number==largest){
maxcounter=counter;
}
}
if(counter==10)
System.out.printf("thelargestoneofthetennumberis%d",
largest);
System.out.printf("\nthe%dthnuberisthelargest",maxcounter);
}
}
}
packagemy.p4;
publicclassP139_4$22{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
intcounter=0;
System.out.print("N\t10*N\t100*N\t1000*N\t\n");
while(++counter<=5){
System.out.printf("\n%d\t%d\t%d\t%d\t",counter,10*counter,
100*counter,1000*counter);
}
}
}
packagemy.p4;
importjava.util.Scanner;
publicclassP139_4$23{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannerinput=newScanner(System.in);
intcounter=0;
intnumber;//input
intlargest=0;
intsecondlargest=0;
intmaxcounter=0;
intsecondmaxcounter=0;
while(++counter<=10){
System.out.printf("\n\nInputthe%dthnumber:
\n",counter);
number=input.nextInt();
if(number>largest){
secondlargest=largest;
largest=number;
System.out.print("****************************************");
System.out.printf("\nThelargestnumberis%d",largest);
System.out.printf("\nThesecondlargestnumberis%d",secondlargest);
System.out.print("\n****************************************\n");
if(number==largest){
maxcounter=counter;
}
/*while(secondlargest==largest){
secondmaxcounter=counter;
}*/
}
if(numberif(number>secondlargest){
secondlargest=number;
secondmaxcounter=counter;
System.out.print("****************************************");
System.out.printf("\nThelargestnumberis%d",largest);
System.out.printf("\nThesecondlargestnumberis%d",secondlargest);
System.out.print("\n****