首页 > 代码库 > JAVA语言程序设计基础课后习题第四章
JAVA语言程序设计基础课后习题第四章
//exercise 4.1package fourthchapterexercise1;import java.util.Scanner;public class first { public static void main(String[] args) { // TODO Auto-generated method stub int integer=0,sum=0,positive=0,negative=0; Scanner in=new Scanner(System.in); System.out.println("Enter an int value,the program exits if the input is 0:"); while(true){ integer=in.nextInt(); if(integer==0) break; else if(integer>0) positive++; else negative++; sum=sum+integer; } double average=(double)sum/(positive+negative); System.out.println("The number of positives is "+positive); System.out.println("The number of negatives is "+negative); System.out.println("The total is "+sum); System.out.println("The average is "+average); }}//exercise 4.2package fourthchapterexercise1;import java.util.Scanner;public class second { public static void main(String[] args) { // TODO Auto-generated method stub final int NUMBER_OF_QUESTIONS=10; int correctCount=0; int count=0; long startTime=System.currentTimeMillis(); String output=""; Scanner in=new Scanner(System.in); //The main program while (count<NUMBER_OF_QUESTIONS){ //0-15 random number int number1=(int)(Math.random()*15+1); int number2=(int)(Math.random()*15+1); //input expression System.out.print("What is "+number1+" + "+number2+"? "); //get answer int answer=in.nextInt(); //judge answer if(number1+number2==answer){ System.out.println("You are correct!"); correctCount++; } else System.out.println("Your answer is wrong.\n"+number1+" + "+number2+" sh" + "ould be "+(number1+number2)); //count count++; output +="\n"+number1+" + "+number2+"="+answer+((number1+number2==answer)?" cor" + "rect":" wrong"); } long endTime=System.currentTimeMillis(); long testTime=endTime-startTime; System.out.println("Correct count is "+correctCount+"\nTest time is "+testTime/1000+" seco" + "nds\n"+output); }}//exercise 4.3package fourthchapterexercise1;public class third { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("千克\t\t英镑"); for(int i=0;i<100;i++){ System.out.println(2*i+1+"\t\t"+(2*i+1)*2.2); } }}//exercise 4.4package fourthchapterexercise1;public class fourth { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("英里\t\t千米"); for(int i=1;i<=10;i++){ System.out.println(i+"\t\t"+i*1.609); } }}//exercise 4.5package fourthchapterexercise1;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("千克\t\t镑\t\t磅\t\t千克"); int pound=20; for(int i=0;i<100;i++){ System.out.println(2*i+1+"\t\t"+(int)((2*i+1)*2.2*100)/100.0+"\t" + "\t"+pound+"\t\t"+(int)(pound/2.2*100)/100.0); pound+=5; } }}//exercise 4.6package fourthchapterexercise1;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("英里\t\t千米\t\t英里\t\t千米"); int pound=20; for(int i=0;i<10;i++){ System.out.println(i+"\t\t"+(int)(i*1.609*1000)/1000.0+"\t" + "\t"+pound+"\t\t"+(int)(pound/1.609*1000)/1000.0); pound+=5; } }}//exercise 4.7package fourthchapterexercise1;public class seventh { public static void main(String[] args) { // TODO Auto-generated method stub double tuition=10000; for(int i=0;i<10;i++){ tuition *= (1+0.05); } System.out.println("after ten year,tuition is "+tuition); double sum=0; for(int i=0;i<4;i++){ sum += tuition; tuition *= (1+0.05); } System.out.println("tuition of four year is "+sum); }}//exercise 4.8package fourthchapterexercise1;import java.util.Scanner;public class eighth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input number of students is "); int number =in.nextInt(); String maxname=null; int max=0; for(int i=0;i<number;i++){ System.out.print("please input student‘s name and score:"); String name =in.next(); int score=in.nextInt(); if(score>max) maxname=name; } System.out.print("The most high score students name is :"+maxname); }}//exercise 4.9package fourthchapterexercise1;import java.util.Scanner;public class ninth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input number of students is: "); int number =in.nextInt(); String maxname=null,secondname=null; int max=0; int secondhigh=0; for(int i=0;i<number;i++){ System.out.print("please input student‘s name and score:"); String name =in.next(); int score=in.nextInt(); if(score>max){ maxname=name; max=score; } if(score>secondhigh&&score!=max){ secondname=name; secondhigh=score; } } System.out.println("The most high score students name is :"+maxname); System.out.println("The second high score students name is :"+secondname); }}//exercise 4.10package fourthchapterexercise1;import java.util.Scanner;public class tenth { public static void main(String[] args) { // TODO Auto-generated method stub int count=0; for (int i=100;i<=1000;i++){ if(i%5==0&&i%6==0){ System.out.print("\t"+i); count++; if(count%10==0) System.out.println(); } } }}//exercise 4.11package fourthchapterexercise2;public class first { public static void main(String[] args) { // TODO Auto-generated method stub int count=0; for (int i=100;i<=200;i++){ if(i%5==0^i%6==0){ System.out.print("\t"+i); count++; if(count%10==0) System.out.println(); } } }}//exercise 4.12package fourthchapterexercise2;public class second { public static void main(String[] args) { // TODO Auto-generated method stub int n=1; while(true){ if(n*n>12000){ System.out.println("The smallest integer is "+n); break; } n++; } }}//exercise 4.13package fourthchapterexercise2;public class third { public static void main(String[] args) { // TODO Auto-generated method stub int n=1; while(true){ if(n*n*n>12000){ System.out.println("The smallest integer is "+(n-1)); break; } n++; } }}//exercise 4.14package fourthchapterexercise2;public class fourth { public static void main(String[] args) { // TODO Auto-generated method stub int startascii=33; int count=0; while(startascii<=126){ char output=(char)startascii; count++; System.out.print("\t"+output); if(count%10==0) System.out.println(); startascii++; } }}//exercise 4.15package fourthchapterexercise2;import java.util.Scanner;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input two integer n1 and n2 :"); int n1=in.nextInt(); int n2=in.nextInt(); int min; if(n1>n2) min=n2; else min=n1; while(true){ if(n1%min==0&&n2%min==0){ System.out.println("The greatest common divisor is "+min); break; } min--; } }}//exercise 4.16package fourthchapterexercise2;import java.util.Scanner;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input integer:"); int integer=in.nextInt(); System.out.print("The factor is :"); while(true){ for(int i=2;i<=integer;i++){ if(integer%i==0){ System.out.print(i); integer=integer/i; if(integer==1) System.out.print("."); else System.out.print(","); break; } } if(integer==1) break; } }}//exercise 4.17package fourthchapterexercise2;import java.util.Scanner;public class seventh { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input integer (0-15):"); int input =in.nextInt(); for(int i=1;i<=input;i++){ for(int j=1;j<=input-i;j++){ if(j==input-i){ System.out.print("\t"); break; } System.out.print("\t "); } for(int k=i;k>0;k--){ if(k==1){ System.out.print(k); break; } System.out.print(k+"\t"); } for(int j=2;j<=i;j++){ System.out.print("\t"+j); } System.out.println(); } }}//exercise 4.18package fourthchapterexercise2;public class eighth { public static void main(String[] args) { // TODO Auto-generated method stub //pattern 1 for(int i=1;i<=6;i++){ for(int j=1;j<=i;j++){ System.out.print(j+"\t"); } System.out.println(); } System.out.println(); System.out.println(); //pattern 2 for(int i=1;i<=6;i++){ for(int j=1;j<=7-i;j++){ System.out.print(j+"\t"); } System.out.println(); } System.out.println(); System.out.println(); //pattern 3 for(int i=1;i<=6;i++){ for(int j=1;j<=6-i;j++){ System.out.print(" \t"); } for(int k=i;k>=1;k--){ System.out.print(k+"\t"); } System.out.println(); } System.out.println(); System.out.println(); //pattern 4 for(int i=1;i<=6;i++){ for(int k=2;k<=i;k++){ System.out.print(" \t"); } for(int j=1;j<=7-i;j++){ System.out.print(j+"\t"); } System.out.println(); } }}//exercise 4.19package fourthchapterexercise2;public class ninth { public static void main(String[] args) { // TODO Auto-generated method stub int integer; for(int i=1;i<=8;i++){ for(int j=1;j<=8-i;j++){ System.out.print(" "); } for(int j=1;j<=i;j++){ integer=(int)Math.pow(2, j-1); System.out.printf("%4d",integer); } for(int k=i-1;k>=1;k--){ integer=(int)Math.pow(2, k-1); System.out.printf("%4d",integer); } System.out.println(); } } }//exercise 4-20package fourthchapterexercise3;public class first { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("prime of 2 to 1000 is:"); int count=0; for(int i=2;i<=1000;i++){ boolean isprime=true; for(int j=2;j<=i/2;j++){ if(i%j==0){ isprime=false; break; } } if(isprime){ count++; System.out.printf("%4d",i); } if(count%8==0&&isprime) System.out.println(); } }}//exercise 4.21package fourthchapterexercise3;import java.util.Scanner;//This function is not a rigorous//The correctedpublic class second { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input loan amount:"); int loanamount=in.nextInt(); System.out.print("Number of years:"); int numberofyears=in.nextInt(); double interestrate=0.05; int count; System.out.println("Inerest Rate Monthly payment Total Payment"); //computational for(count=0;count<=24;count++){ double monthlyinterestrate=interestrate/12; //The monthly payment double monthlypayment=(loanamount*monthlyinterestrate)/ (1-1/Math.pow(1+monthlyinterestrate,numberofyears*12)); //total payment double totalpayment=monthlypayment*12*numberofyears; System.out.println((int)(interestrate*100000)/1000.0+"%\t\t "+ (int)(monthlypayment*100)/100.0+"0\t\t"+(int)(totalpayment*100)/100.0); interestrate +=0.00125; } }}//exercise 4.22package fourthchapterexercise3;import java.util.Scanner;public class third { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Loan Amount:"); int loanamount=in.nextInt(); System.out.print("Number of Years:"); int numberofyears=in.nextInt(); System.out.print("Annual Interest rate:"); double annualinterestrate=in.nextDouble(); double monthlyinterestrate=annualinterestrate/12; double monthlypayment=(loanamount*monthlyinterestrate)/ (1-1/Math.pow(1+monthlyinterestrate,numberofyears*12)); double totalpayment=monthlypayment*12*numberofyears; System.out.println("\nMonthly Payment:"+monthlypayment); System.out.println("Total Payment"+totalpayment); System.out.println(); double interest=0,principal; double balance=loanamount; System.out.println("Payment\t\tInterest\t\t Principal\t\t Balance"); for(int i=1;i<=numberofyears*12;i++){ interest=monthlyinterestrate*balance; principal=monthlypayment-interest; balance=balance-principal; System.out.println(i+"\t\t"+interest+"\t\t"+principal+"\t\t"+balance); } }}//exercise 4.23package fourthchapterexercise3;public class fourth { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; for(int i=1;i<=5000;i++){ sum += 1.0/i; } System.out.println("This is from left to right:"+sum); sum=0; for(int i=5000;i>=1;i--){ sum += 1.0/i; } System.out.println("This is from right to left:"+sum); }}//exercise 4.24package fourthchapterexercise3;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; double molecular=1; double denominator=3; for(int i=1;i<=49;i++){ sum += molecular/denominator; molecular += 2; denominator += 2; } System.out.println("result is "+sum); }}//exercise 4.25package fourthchapterexercise3;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=10000;i<=100000;i += 10000){ double pi=0,sum=0; for (int j=0;j<=i;j++){ sum =sum+Math.pow(-1,j)*(1.0/(2*j+1)); } pi=4*sum; System.out.print("when i="+i+",pi="+pi+"\n"); } }}//exercise 4.26package fourthchapterexercise3;//The arithmetic is not good enoughpublic class seventh { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=10000;i<=100000;i +=10000){ double sum=1; double generaltype=1; for(int j=1;j<=i;j++){ for(int k=1;k<=j;k++){ generaltype *= 1.0/k; } sum += generaltype; } System.out.println("When i="+i+",e="+sum); } }}//exercise 4.27package fourthchapterexercise3;public class eighth { public static void main(String[] args) { // TODO Auto-generated method stub int count=0; System.out.println("A leap year all in twenty-first Century is:"); for(int year=2001;year<=2100;year++){ if(year%4==0&&year%100!=0||year%400==0){ System.out.print(year+" "); count++; if(count%10==0) System.out.println(); } } }}//exercise 4.28package fourthchapterexercise3;import java.util.Scanner;public class ninth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input year and days of week:"); int year=in.nextInt(); int january=in.nextInt(); //The calculation of the first day of the month is the day of the week int february=(january+31)%7; int march=(february+28)%7; if(year%4==0&&year%100!=0||year%400==0) march=(february+29)%7; int april=(march+31)%7; int may=(april+30)%7; int june=(may+31)%7; int july=(june+30)%7; int august=(july+31)%7; int september=(august+31)%7; int october=(september+30)%7; int november=(october+31)%7; int december=(november+30)%7; //matching week String week=null; switch(january){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("January 1, "+year+" is "+week); switch(february){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("february 1, "+year+" is "+week); switch(march){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("march 1, "+year+" is "+week); switch(april){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("april 1, "+year+" is "+week); switch(may){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("may 1, "+year+" is "+week); switch(june){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("june 1, "+year+" is "+week); switch(july){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("july 1, "+year+" is "+week); switch(august){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("august 1, "+year+" is "+week); switch(september){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("september 1, "+year+" is "+week); switch(october){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("october 1, "+year+" is "+week); switch(november){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("november 1, "+year+" is "+week); switch(december){ case 0: week="Sunday"; break; case 1: week="Monday"; break; case 2: week="Tuesday"; break; case 3: week="Wednesday"; break; case 4: week="Thursday"; break; case 5: week="Friday"; break; case 6: week="Saterday"; break; default: break; } System.out.println("december 1, "+year+" is "+week); }}//exercise 4.29package fourthchapterexercise3;import java.util.Scanner;public class tenth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input year and days of week:"); int year=in.nextInt(); int january=in.nextInt(); //The calculation of the first day of the month is the day of the week int february=(january+31)%7; int march=(february+28)%7; if(year%4==0&&year%100!=0||year%400==0) march=(february+29)%7; int april=(march+31)%7; int may=(april+30)%7; int june=(may+31)%7; int july=(june+30)%7; int august=(july+31)%7; int september=(august+31)%7; int october=(september+30)%7; int november=(october+31)%7; int december=(november+30)%7; //January System.out.println("\t\t January"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); int count=1; for(int i=0;i<january;i++){ System.out.print(" \t"); } //wrap int temp=january; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //February System.out.println("\t\t February"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<february;i++){ System.out.print(" \t"); } //wrap temp=february; //A month has 28 or 29 days int feb=28; if(year%4==0&&year%100!=0||year%400==0) feb=29; for(count=1;count<=feb;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //March System.out.println("\t\t March"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<march;i++){ System.out.print(" \t"); } //wrap temp=march; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //April System.out.println("\t\t April"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<april;i++){ System.out.print(" \t"); } //wrap temp=april; //A month has 30 days for(count=1;count<=30;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //May System.out.println("\t\t May"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<may;i++){ System.out.print(" \t"); } //wrap temp=may; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //June System.out.println("\t\t June"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<june;i++){ System.out.print(" \t"); } //wrap temp=june; //A month has 30 days for(count=1;count<=30;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //July System.out.println("\t\t July"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<july;i++){ System.out.print(" \t"); } //wrap temp=july; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //August System.out.println("\t\t August"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<august;i++){ System.out.print(" \t"); } //wrap temp=august; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //September System.out.println("\t\t September"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<september;i++){ System.out.print(" \t"); } //wrap temp=september; //A month has 30 days for(count=1;count<=30;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //October System.out.println("\t\t October"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<october;i++){ System.out.print(" \t"); } //wrap temp=october; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //November System.out.println("\t\t November"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<november;i++){ System.out.print(" \t"); } //wrap temp=november; //A month has 30 days for(count=1;count<=30;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); //December System.out.println("\t\t December"+year+" \t"); System.out.println("----------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); count=1; for(int i=0;i<december;i++){ System.out.print(" \t"); } //wrap temp=december; //A month has 31 days for(count=1;count<=31;count++){ if(temp%7==0){ System.out.println(); temp=0; } System.out.printf("%3d\t",count); temp++; } System.out.println("\n"); }}//exercise 4.30package fourthchapterexercise4;import java.util.Scanner;public class first { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input money and annual interest rate and number of month:"); double monthlymoney=in.nextDouble(); double yearlyinterestrate=in.nextDouble(); double numberofmonth=in.nextDouble(); double monthlyinterestrate=yearlyinterestrate/12; double sum=0; for(int i=1;i<=numberofmonth;i++){ sum=(monthlymoney+sum)*(1+monthlyinterestrate); } System.out.print(numberofmonth+"months later,the money on account of "+sum); }}//exercise 4.31package fourthchapterexercise4;import java.util.Scanner;public class second { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the initial deposit amount:"); double sum=in.nextDouble(); System.out.print("Enter annual percentage yiekd:"); double yearlyinterestrate=in.nextDouble(); System.out.print("Enter maturityperiod(number if months):"); double numberofmonth=in.nextDouble(); double monthlyinterestrate=yearlyinterestrate/1200; System.out.println("Month\t\tCD Value"); for(int i=1;i<=numberofmonth;i++){ sum=sum*(1+monthlyinterestrate); System.out.printf("%5d",i); System.out.println("\t\t"+sum); } }}//exercise 4.32package fourthchapterexercise4;import java.util.Scanner;public class third { public static void main(String[] args) { // TODO Auto-generated method stub int lottery=0; //Generate an integer, the number of two integers is different while (true){ lottery=(int)(Math.random()*90+10); int first=lottery/10; int second=lottery%10; if(first!=second) break; } Scanner in=new Scanner(System.in); System.out.print("Enter your lottery pick (two digits):"); int guess =in.nextInt(); //separation lottery digit int lotteryDigit1=lottery/10; int lotteryDigit2=lottery%10; //separation guess digit int guessDigit1=guess/10; int guessDigit2=guess%10; System.out.println("The lottery number is "+lottery); //judge if (guess == lottery) System.out.println("Exact match:you win $10000"); else if(guessDigit2==lotteryDigit1&&guessDigit1==lotteryDigit2) System.out.println("Match all digits: you win $3000"); else if(guessDigit1==lotteryDigit1 ||guessDigit1==lotteryDigit2 ||guessDigit2==lotteryDigit1 ||guessDigit2==lotteryDigit2) System.out.println("Match one digit:you win $ 1000"); else System.out.println("Sorry,no match"); }}//exercise 4.33package fourthchapterexercise4;public class fourth { public static void main(String[] args) { // TODO Auto-generated method stub System.out.print("Ten thousand the following numbers,perfect number:"); for(int i=1;i<=10000;i++){ int sum=0; for (int j=1;j<i;j++){ if(i%j==0){ sum=sum+j; } } if(sum==i){ System.out.print(i+" "); } } }}//exercise 4.34package fourthchapterexercise4;import java.util.Scanner;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int computercount=0; int personcount=0; while (true){ System.out.print("scissor(0),rock(1),paper(2):"); int input=in.nextInt(); int computer=(int)(Math.random()*3); if(computer==0&&input==1){ System.out.println("The computer is scissor.You are rock.You win"); personcount++; computercount=0; } else if (computer==0&&input==2){ System.out.println("The computer is scissor.You are paper.You lost."); computercount++; personcount=0; } else if (computer==0&&input==0){ System.out.println("The computer is scissor.You are scissor.It is a draw."); computercount=0; personcount=0; } else if (computer==1&&input==0){ System.out.println("The computer is rock.You are scissor.You lost."); computercount++; personcount=0; } else if (computer==1&&input==1){ System.out.println("The computer is rock.You are rock.It is a draw."); computercount=0; personcount=0; } else if (computer==1&&input==2){ System.out.println("The computer is rock.You are paper.You win."); personcount++; computer=0; } else if (computer==2&&input==0){ System.out.println("The computer is paper.You are scissor.You win."); personcount++; computercount=0; } else if (computer==2&&input==1){ System.out.println("The computer is paper.You are rock.You lost."); computercount++; personcount=0; } else if (computer==2&&input==2){ System.out.println("The computer is paper.You are paper.It is a draw."); computercount=0; personcount=0; } if(computercount==2){ System.out.println("The computer won two times."); break; } if(personcount==2){ System.out.println("You won two times."); break; } } }}//exercise 4.35package fourthchapterexercise4;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; for(int i=1;i<=624;i++){ sum += 1/(Math.pow(i, 0.5)+Math.pow(i+1,0.5)); } System.out.print("sum is "+sum); }}//exercise 4.37package fourthchapterexercise4;import java.util.Scanner;public class seventh { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("input a integer(Decimal system):"); int integer=in.nextInt(); int count=0; int temp=integer; String number=""; //求出字符变成二进制后的长度 while (integer!=0 ){ int remainder=integer%2; integer /=2; number =number+(char)(remainder+48); count++; } //转换在数组中 int newcount=count; char a[]=new char[count]; while (temp!=0 ){ int remainder=temp%2; temp /=2; a[count-1]=(char)(remainder+48); count--; } for(int i=0;i<newcount;i++){ System.out.print(a[i]); } System.out.print("\n"+number); }}//exercise 4.38package fourthchapterexercise4;import java.util.Scanner;public class eight { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("please input integer(Decimal system):"); int integer=in.nextInt(); String hexadecimal=""; while(integer!=0){ int temp=integer%16; if(temp<=9&&temp>=0) hexadecimal=(char)(temp+‘0‘)+hexadecimal; else hexadecimal=(char)(temp-10+‘A‘)+hexadecimal; integer=integer/16; } System.out.println("The hexadecimal is "+hexadecimal); }}//exercise 4.39package fourthchapterexercise4;public class ninth { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; for(int i=0;i<=5000;i++){ sum=5000+i*0.08; if(sum>=30000){ System.out.println("minimum sales is "+(i-1)); System.exit(0); } } for(int i=5000;i<=10000;i++ ){ sum=5000+5000*0.08+i*0.10; if(sum>30000){ System.out.println("minimum sales is "+(i-1)); System.exit(0); } } int i=10000; while (sum<30000){ sum=5000+5000*0.08+10000*0.10+i*0.12; i++; } System.out.println("minimum sales is "+(i-1)); System.exit(0); }}//exercise 4.40package fourthchapterexercise4;public class tenth { public static void main(String[] args) { // TODO Auto-generated method stub int positive=0; int negative=0; for(int i=1;i<=1000000;i++){ int temp=(int)(Math.random()*2); if(temp==0) positive++; else negative++; } System.out.println("The positive is "+positive); System.out.println("The negative is "+negative); }}//exercise 4.41package fourthchapterexercise5;import java.util.Scanner;public class first { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter numbers: "); int integer=in.nextInt(); int max=0; int count=0; while (integer!=0){ if(max<integer){ max=integer; count=0; } if(max==integer){ count++; } integer=in.nextInt(); } System.out.println("The large number is "+max); System.out.println("The occurrence count of the largest number is "+count); }}//exercise 4.43package fourthchapterexercise5;import java.util.Scanner;public class second { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the number of second :"); int seconds=in.nextInt(); while(true){ try { //pause for a second Thread.sleep(1000); if((seconds-1)==0){ System.out.println("Stopped"); break; } if((seconds-1)==1){ System.out.println((seconds-1)+" second remaining"); seconds--; continue; } System.out.println((seconds-1)+" seconds remaining"); seconds--; //format } catch (InterruptedException e) { //If abnormal, the output to the console e.printStackTrace(); } } }}//exercise 4.44package fourthchapterexercise5;import java.util.Scanner;public class third { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int count=0; for(int i=1;i<=1000000;i++){ double x=Math.random()*2-1; double y=Math.random()*2-1; double distance1=Math.pow((x*x+y*y), 0.5); double distance2=Math.pow((x-1)*(x-1)+(y-1)*(y-1),0.5); if(distance1>distance2&&x>0&&y>0) count++; if(x<0) count++; } double chance=count/1000000.0; System.out.println("The chance is "+(int)(chance*10000)/100.0+"%"); }}//exercise 4.45package fourthchapterexercise5;public class fourth { public static void main(String[] args) { // TODO Auto-generated method stub int count=0; for(int i=1;i<=7;i++){ for(int j=1;j<=7;j++){ if(i!=j){ System.out.println(i+" "+j); count++; } } } System.out.println("The numbers of combination is "+count); }}//exercise 4.46package fourthchapterexercise5;import java.util.Scanner;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter an integer:"); int integer =in.nextInt(); //Judgment is a positive or negative number boolean judge=false; if(integer<0){ integer=-integer; judge=true; } //Convert decimal number to a binary number int count=0; String decimal=""; while(integer!=0){ int temp=integer%2; decimal=(char)(temp+‘0‘)+decimal; integer=integer/2; count++; } for(int i=1;i<=16-count;i++){ decimal=‘0‘+decimal; } //Judgment is a positive or negative number if(judge){ char []decimalarr=decimal.toCharArray(); for(int i=0;i<decimalarr.length;i++){ if(decimalarr[i]==‘0‘) decimalarr[i]=‘1‘; else decimalarr[i]=‘0‘; } for(int i=decimalarr.length-1;i>=0;i--){ if(decimalarr[i]==‘1‘){ decimalarr[i]=‘0‘; } else{ decimalarr[i]=‘1‘; break; } } System.out.print("The bits are "); for(int i=0;i<decimalarr.length;i++){ System.out.print(decimalarr[i]); } } else System.out.println("The bits are "+decimal); }}
JAVA语言程序设计基础课后习题第四章
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。