Java语言程序设计基础篇原书第十版第五章综合题答案

时间:2021-03-10 21:49:14

循环这一章的综合题答案。
5.21财务应用程序:比较不同利率下的贷款
自己注意下输出的格式,自己调整就好了的。

package nameyu;

import java.util.Scanner;

public class Test {
    /** * @param args */
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            System.out.print("Loan Amount:");
            int loan=input.nextInt();
            System.out.print("Number of years:");
            int years=input.nextInt();
            System.out.printf("%-5s%-5s%-5s%n","Interest Rate","Monthly Payment","Total Payment");
            for(double annualInterestRate=5.0;annualInterestRate<=8.0;annualInterestRate=annualInterestRate+0.125){
                double MonthlyPayment=loan*(annualInterestRate/1200)/(1-1/Math.pow(1+annualInterestRate/1200, years*12));
                double TotalPayment=MonthlyPayment*years*12;
                System.out.printf("%-5.3f%s%s%5.2f%s%-5.2f%n",annualInterestRate,"%"," ",MonthlyPayment," ",TotalPayment);
            }
            }

5.22财务应用程序:显示分期还贷时间表

package nameyu;
import java.util.Scanner;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Loan Amount:");
        double loan=input.nextDouble();
        System.out.print("Numeber of years:");
        int years =input.nextInt();
        System.out.print("Annual Interest Rate:");
        double InterestRate=input.nextDouble();
        double MonthlyPayment=loan*(InterestRate/1200)/(1-1/Math.pow(1+InterestRate/1200, years*12));
        double TotalPayment=MonthlyPayment*years*12;
        System.out.printf("%-4.2f%s%-4.2f%n",MonthlyPayment," ",TotalPayment);
        System.out.println();
        System.out.println("Payment#"+"\t"+"Interest"+"\t\t\t"+"Principal"+"\t\t\t"+"Balance");
        double balance=loan;
        for(int i=1;i<=years*12;i++){
            double  Interest=(InterestRate/1200)*balance;
            double principal=MonthlyPayment-Interest;
            balance=balance-principal;
            System.out.println(i+"\t\t"+Interest+"\t\t"+principal+"\t\t"+balance);
        }

    }

}

5.23示例抵消错误
从左到右
Enter a number such as 5000:5000
9.094508852984404

package nameyu;
import java.util.Scanner;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a number such as 5000:");
        int n=input.nextInt();
        double sum=0;
        for(int i=1;i<=n;i++){
            sum+=(1.0)/i;
        }
        System.out.println(sum);

    }

}

从右到左
Enter a number such as 5000:5000
9.09450885298443

package nameyu;
import java.util.Scanner;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a number such as 5000:");
        int n=input.nextInt();
        double sum=0;
        for(int i=n;i>0;i--){
            sum+=(1.0)/i;
        }
        System.out.println(sum);

    }

}

5.24数列求和

package nameyu;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double sum=0;
        for(int i=1;(2*i-1)<=97;i++){
            double a=((2*i)*1.0-1)/(1+2*i);
         sum+=a;
// System.out.println(a);

        }
          System.out.println(sum);

    }

}       

5.25计算π值
π=3.141592653589793238462643383279502 我也只能大概记住这么多位。输入10000 -100000 自己输出看看就行。

package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a number:");
        int n=input.nextInt();
         double sum=0;
        for(int i=1;i<=n;i++){

        sum+=(double)(Math.pow((-1),(i+1))/(2*i-1));

        }
         System.out.println((double)(4*sum));
        }

}       
/* Enter a number:10000 3.1414926535900345 Enter a number:20000 3.1415426535898248 Enter a number:30000 3.141559320256462 */        

5.26计算e
1:用Scanner


public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            System.out.print("Enter a number:");
            int number=input.nextInt();
            double e = 1.0;
            double item = 1.0;
            for (int i = 1; i <= number; i++) {
                item = item / i;
                e = e+item;
        }
            System.out.println("The e is " + e + " for i = " + number);
        }

}       

2:用if

package nameyu;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            double e = 1.0;
            double item = 1.0;
            for (int i = 1; i <= 100000; i++) {
                item = item / i;
                e = e+item;
                if (i == 10000 || i == 20000 || i == 30000 || i == 40000 || i == 50000 || i == 60000 || i == 70000 || i == 80000 ||i == 90000 || i == 100000){
                    System.out.println("The e is " + e + " for i = " + i);
                }
        }
        }

}       

5.27显示闰年
首先显示从101-2100期间所有的闰年,然后输出时,每行十个。中间用一个空格字符隔开,所以不能斜杠T了。虽然那个好用。
兄弟们,我坚强的使用了\t,在下强迫症,那个当数字是四位数的时候不整齐。

package nameyu;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int count=0;
        for(int years=101;years<=2100;years++){
            if((years%4==0&&years%100!=0)||(years%400==0)){
                count++;
                if(count%10==0){
                    System.out.println(years);
                }else 
                    System.out.print(years+"\t");
            }
        }

        }
}       

5.28显示每月第一天是星期几
看清楚这道题的代码,可能和你们百度的方法都不一样。我的答案并没有错哦~

package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a year :");
        int year=input.nextInt();
        System.out.print("Enter the first day of the year is a few weeks :");
        int week=input.nextInt();
        String M=null;
        int mouth;
        int W;
        boolean LeapYear=(year%4==0&&year%100!=0)||(year%400==0);
        for(mouth=1;mouth<=12;mouth++){
            if(mouth==1){
                M="January";
            }else if(mouth==2){
                W=(31%7+week)%7;
                week=W;
                M="February";
            }else if(mouth==3){
                    if(LeapYear){
                W=(29%7+week)%7;
                week=W;
                 M="March";
                }else
                {W=(28%7+week)%7;
                week=W;}
                M="March";
            }else if(mouth==4){
                W=(31%7+week)%7;
                week=W;
                M="April";
            }else if(mouth==5){
                W=(30%7+week)%7;
                week=W;
                M="May";
            }else if(mouth==6){
                W=(31%7+week)%7;
                week=W;
                M="June";
            }else if(mouth==7){
                W=(30%7+week)%7;
                week=W;
                M="July";
            }else if(mouth==8){
                W=(31%7+week)%7;
                week=W;
                M="August";
            }else if(mouth==9){
                W=(31%7+week)%7;
                week=W;
                M="September";
            }else if(mouth==10){
                W=(30%7+week)%7;
                week=W;
                M="October";
            }else if(mouth==11){
                W=(31%7+week)%7;
                week=W;
                M="November";
            }else if(mouth==12){
                W=(30%7+week)%7;
                week=W;
                M="December";
            }
                   switch(week){
            case 1: System.out.println(M+" "+mouth+" "+year+" is Monday");break;
            case 2: System.out.println(M+" "+mouth+" "+year+" is Tuesday");break;
            case 3: System.out.println(M+" "+mouth+" "+year+" is Wednesday");break;
            case 4: System.out.println(M+" "+mouth+" "+year+" is Thursday");break;
            case 5: System.out.println(M+" "+mouth+" "+year+" is Friday");break;
            case 6: System.out.println(M+" "+mouth+" "+year+" is Saturday");break;
            case 0: System.out.println(M+" "+mouth+" "+year+" is Sunday");break;
            }
        }

        }
}                

5.29显示日历
通过上一题改的,主要看if中间的代码,看一个就懂了。每个月里面的代码是差不多的,就是时间不同,31天30天,28,29的区别。

package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a year :");
        int year=input.nextInt();
        System.out.print("Enter the first day of the year is a few weeks :");
        int week=input.nextInt();
        String M=null;
        int mouth;
        int W;
        int count=0;
        boolean LeapYear=(year%4==0&&year%100!=0)||(year%400==0);
        for(mouth=1;mouth<=12;mouth++){
            if(mouth==1){
                M="January";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }


            else if(mouth==2){
                W=(31%7+week)%7;
                week=W;
                M="February";
                if(LeapYear){
                    System.out.println("\t\t "+M+"\t"+year);
                    System.out.println("----------------------------------------------------");
                    System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
                   for(int j=0;j<week;j++){
                            System.out.print("\t");
                   }
                   for(int i=1;i<=7-week;i++){

                        if(i%7==0){
                            System.out.println(i);
                        }else
                            System.out.print(i+"\t");
                    }
                   System.out.println();
                   for(int k=(7-week+1);k<=29;k++){
                       count++;
                       if(count%7==0){
                            System.out.println(k);
                        }else
                            System.out.print(k+"\t");
                   }
                    System.out.println();
                    count=0;
                }else{
                    System.out.println("\t\t "+M+"\t"+year);
                    System.out.println("----------------------------------------------------");
                    System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
                   for(int j=0;j<week;j++){
                            System.out.print("\t");
                   }
                   for(int i=1;i<=7-week;i++){

                        if(i%7==0){
                            System.out.println(i);
                        }else
                            System.out.print(i+"\t");
                    }
                   System.out.println();
                   for(int k=(7-week+1);k<=28;k++){
                       count++;
                       if(count%7==0){
                            System.out.println(k);
                        }else
                            System.out.print(k+"\t");
                   }
                    System.out.println();
                    count=0;
                }
            }

            else if(mouth==3){
                    if(LeapYear){
                W=(29%7+week)%7;
                week=W;
                M="March";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
                }
                else{W=(28%7+week)%7;
                week=W;
                M="March";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }
            }

            else if(mouth==4){
                W=(31%7+week)%7;
                week=W;
                M="April";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=30;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==5){
                W=(30%7+week)%7;
                week=W;
                M="May";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==6){
                W=(31%7+week)%7;
                week=W;
                M="June";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=30;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==7){
                W=(30%7+week)%7;
                week=W;
                M="July";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==8){
                W=(31%7+week)%7;
                week=W;
                M="August";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==9){
                W=(31%7+week)%7;
                week=W;
                M="September";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=30;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==10){
                W=(30%7+week)%7;
                week=W;
                M="October";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==11){
                W=(31%7+week)%7;
                week=W;
                M="November";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=30;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();
                count=0;
            }else if(mouth==12){
                W=(30%7+week)%7;
                week=W;
                M="December";
                System.out.println("\t\t "+M+"\t"+year);
                System.out.println("----------------------------------------------------");
                System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
               for(int j=0;j<week;j++){
                        System.out.print("\t");
               }
               for(int i=1;i<=7-week;i++){

                    if(i%7==0){
                        System.out.println(i);
                    }else
                        System.out.print(i+"\t");
                }
               System.out.println();
               for(int k=(7-week+1);k<=31;k++){
                   count++;
                   if(count%7==0){
                        System.out.println(k);
                    }else
                        System.out.print(k+"\t");
               }
                System.out.println();count=0;
            }

        }

        }
}       

5.30财务应用程序:复利值

package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the amount of savings :");
        double dollar=input.nextDouble();
        System.out.print("Enter annual rate:");
        double annualrate=input.nextDouble();
        System.out.print("Number of months :");
        int mouth=input.nextInt();
        double sum=0;
        double MonthlyInterestRate=annualrate/1200;
        for(int i=1;i<=mouth;i++){
            sum=(sum+dollar)*(1+MonthlyInterestRate);

        }
        System.out.println(sum);
        }
}        

5.31财务应用程序:计算CD价值

 package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the initial deposit amount :");
        double dollar=input.nextDouble();
        System.out.print("Enter annual percentage yield :");
        double annualrate=input.nextDouble();
        System.out.print("Enter maturity period (number of mounths) :");
        int mouth=input.nextInt();
        double sum=0;
        double MonthlyInterestRate=annualrate/1200;
        System.out.println("Month"+"\t"+"CD Value");
        for(int i=1;i<=mouth;i++){
            sum=dollar*(1+MonthlyInterestRate);
            dollar=sum;
              System.out.println(i+"\t"+sum);
        }

        }
}                                  

5.32游戏:彩票

package nameyu;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       int lottery1=(int) (Math.random()*10);
       int lottery2=(int) (Math.random()*10);
       int Lottery=lottery1*10+lottery2;
       if(lottery2==lottery1){
           int lottery3=(int) (Math.random()*10);
           lottery2 =lottery3;
       }
       Scanner input=new Scanner (System.in);
       System.out.print("Enter your lottery pick(two digits):");
       int guess=input.nextInt();
       int guessDigit1=guess/10;
       int guessDigit2=guess%10;
       System.out.println("The lottery number is "+Lottery);
       if(guess==Lottery)
           System.out.println("Exact match:you win $10000");
       else if(guessDigit2==lottery1&&guessDigit1==lottery2)
           System.out.println("Match one digit:you win $3000");
       else if(guessDigit1==lottery1||guessDigit1==lottery2||guessDigit2==lottery1||guessDigit2==lottery2)
           System.out.println("Match one digit:you win $1000");
       else
           System.out.println("sorry,no match");
        }
}       

如有其它题目需要的话,在评论里留下题号,博主看到会尽快解决。