Java語言程序設計基礎篇原書第十版第五章綜合題答案

循環這一章的綜合題答案。
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");
        }
}       

如有其它題目需要的話,在評論裏留下題號,博主看到會盡快解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章