《Java语言程序设计与数据结构》编程练习答案(第五章)(四)

《Java语言程序设计与数据结构》编程练习答案(第五章)(四)

英文名:Introduction to Java Programming and Data Strictures, Comprehensive Version, 11th Edition

5.40

public class book {
    public static void main(String[] args)
    {
        int zheng = 0;
        int fan = 0;
        for(int i=0;i<1000000;i++)
        {
            if(Math.random()>0.5)
                zheng++;
            else
                fan++;
        }
        System.out.println("正面"+zheng+"次,反面"+fan+"次");
    }
}

5.41

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int max = 0;
        int maxCount = 0;
        System.out.print("Enter numbers: ");
        while(true)
        {
            int ass = input.nextInt();
            if(ass==0)
                break;
            else
            {
                if(ass>max)
                {
                    max=ass;
                    maxCount=1;
                }
                else if(ass==max)
                    maxCount++;
            }
        }
        System.out.println("The largest number is "+max);
        System.out.println("The occurrence count of the largest number is "+maxCount);
    }
}

5.42

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your target: ");
        final double COMMISSION_SOUGHT = input.nextDouble();
        double ass = COMMISSION_SOUGHT/0.08+5000;
        int i=1;
        for(i=1;i<ass;i++)
        {
            double ticheng = 0;
            if(i<=5000)
                ticheng=i*0.08;
            else if(i<=10000)
                ticheng=5000*0.08+(i-5000)*0.10;
            else
                ticheng=5000*0.08+5000*0.10+(i-10000)*0.12;
            if(ticheng+5000>=COMMISSION_SOUGHT)
            {
                System.out.println("You need to sell $"+i);
                break;
            }
        }
    }
}

5.43

public class book {
    public static void main(String[] args)
    {
        int count = 0;
        for(int i=1;i<=6;i++)
            for(int j=i+1;j<=7;j++)
            {
                System.out.printf("%d  %d\n",i,j);
                count++;
            }
        System.out.println("The total number of all combinations is "+count);
    }
}

5.44

黑人问号

5.45

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        double sum = 0;
        double sumQ = 0;
        System.out.print("Enter 10 numbers: ");
        for(int i=0;i<10;i++)
        {
            double tmp=input.nextDouble();
            sum+=tmp;
            sumQ+=tmp*tmp;
        }
        double means = sum/10.0;
        double sd = Math.sqrt((sumQ-sum*sum/10.0)/9.0);
        System.out.println("The mean is "+means);
        System.out.println("The standard deviation is "+sd);
    }
}

5.46

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String ass = input.nextLine();
        System.out.print("The reversed string is ");
        for(int i=ass.length()-1;i>=0;i--)
            System.out.print(ass.charAt(i));
        System.out.print('\n');
    }
}

5.47

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first 12 digits:");
        String ass = input.nextLine();
        if(ass.length()!=12)
            System.out.println(ass+" is invalid.");
        else
        {
            int sum=0;
            for(int i=0;i<12;i++)
            {
                int tmp = ass.charAt(i)-'0';
                if(i%2==1)
                    sum+=tmp;
                else
                    sum+=3*tmp;
            }
            int tail = 10-sum%10;
            if(tail==10)
                tail=0;
            System.out.println("The ISBN-13 number is "+ass+tail);
        }
    }
}

5.48

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a string: ");
        Scanner input = new Scanner(System.in);
        String ass = input.nextLine();
        int len = ass.length();
        for(int i=0;i<len;i+=2)
            System.out.print(ass.charAt(i));
        System.out.println("");
    }
}

5.49

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a string: ");
        Scanner input = new Scanner(System.in);
        String ass = input.nextLine();
        int len = ass.length();
        ass=ass.toLowerCase();
        int countV =0;
        int countC = 0;
        for(int i=0;i<len;i++)
        {
            char tmp = ass.charAt(i);
            if(tmp=='a'||tmp=='e'||tmp=='i'||tmp=='o'||tmp=='u')
                countV++;
            else if(tmp!=' ')
                countC++;
        }
        System.out.println("The number of vowels is "+countV);
        System.out.println("The number of consonants is "+countC);
    }
}

5.50

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a string: ");
        Scanner input = new Scanner(System.in);
        String ass = input.nextLine();
        int len = ass.length();
        int countC = 0;
        for(int i=0;i<len;i++) {
            if (ass.charAt(i) >= 'A' && ass.charAt(i) <= 'Z')
                countC++;
        }
        System.out.println("The number of upper case letter is "+countC);
    }
}

5.51

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter the first string: ");
        Scanner input = new Scanner(System.in);
        String ass = input.nextLine();
        System.out.print("Enter the second string: ");
        String dick = input.nextLine();
        int len1 = ass.length();
        int len2 = dick.length();
        int range = Math.min(len1,len2);
        if(ass.charAt(0)!=dick.charAt(0))
            System.out.println(ass+" and "+dick+" have no common prefix");
        else {
            System.out.print("The common prefix is ");
            for (int i = 0; i < range; i++) {
                if (ass.charAt(i) == dick.charAt(i))
                    System.out.print(ass.charAt(i));
            }
            System.out.println("");
        }
    }
}

第五章 完

发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104520182
今日推荐