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

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

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

5.1

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer, the input ends if it is 0: ");
        int pos = 0;
        int neg = 0;
        double sum = 0;
        int amo = 0;
        while(true)
        {
            int tmp = input.nextInt();
            if(tmp==0)
                break;
            else
            {
                amo++;
                sum+=tmp;
                if(tmp>0)
                    pos++;
                else
                    neg++;
            }
        }
        double ave = sum/amo;
        if(amo==0)
            System.out.println("No numbers are entered except 0");
        else {
            System.out.println("The number of positives is " + pos);
            System.out.println("The number of negatives is " + neg);
            System.out.println("The total is " + sum);
            System.out.println("The average is " + ave);
        }
    }
}

5.2

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
       final int NUMBER_OF_QUESTIONS=10;
       int correctCount = 0;
       int count = 0;
       long startTime = System.currentTimeMillis();
       String output="";
       Scanner input = new Scanner(System.in);
       while(count<NUMBER_OF_QUESTIONS)
       {
           int number1 = (int)(Math.random()*15);
           int number2 = (int)(Math.random()*15);
           System.out.print("What is "+number1+" + "+number2+"? ");
           int answer = input.nextInt();
           if(number1+number2==answer)
           {
               System.out.println("You are correct!");
               correctCount++;
           }
           else
               System.out.println("Your answer is wrong.\n"+number1+" + "+number2+" should be "+(number1+number2));
           count++;
           output+="\n"+number1+"+"+number2+"="+answer+((number1+number2==answer)?" correct":" wrong");
       }
       long endTime = System.currentTimeMillis();
       long testTime = endTime-startTime;
       System.out.println("Correct count is "+correctCount+"\nTest time is "+testTime/100+" seconds\n"+output);
    }
}

5.3

public class book {
    public static void main(String[] args)
    {
       System.out.println("千克         磅");
       for(int i=1;i<=100;i++)
           System.out.printf("%-6d%8.1f\n",i*2-1,(2*i-1)*2.2);
    }
}

5.4

public class book {
    public static void main(String[] args)
    {
       System.out.println("英里        千米");
       for(int i=1;i<=10;i++)
           System.out.printf("%-11d%-6.3f\n",i,i*1.609);
    }
}

5.5

public class book {
    public static void main(String[] args)
    {
       System.out.println("千克       磅      磅      千克");
       for(int i=1;i<=100;i++)
           System.out.printf("%-5d%8.1f     %-4d%8.2f\n",2*i-1,(2*i-1)*2.2,5*i+15,(5*i+15)/2.2);
    }
}

5.6

public class book {
    public static void main(String[] args)
    {
       System.out.println("英里     千米     千米     英里");
       for(int i=1;i<=10;i++)
           System.out.printf("%-8d%-7.3f  %-8d%-6.3f\n",i,i*1.609,5*i+15,(5*i+15)/1.609);
    }
}

5.7

public class book {
    public static void main(String[] args)
    {
       double ass = 10000;
       for(int i=0;i<10;i++)
           ass*=(1+0.05);
       double oldAss = ass;
       double sum = 0;
       for(int i=0;i<4;i++)
       {
           sum+=ass;
           ass*=(1+0.05);
       }
       System.out.printf("Ass after 10 years is %.2f\n",oldAss);
       System.out.printf("The total cost of 4 asses is %.2f\n",sum);
    }
}

5.8

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
       System.out.println("Enter the number of girls: ");
       Scanner input = new Scanner(System.in);
       int num = input.nextInt();
       String name = "";
       double cup = 0;
       for(int i=0;i<num;i++)
       {
           String tname = input.next();
           double tmp = input.nextDouble();
           if(tmp>cup)
           {
               cup=tmp;
               name=tname;
           }
       }
       System.out.println("The hottest girl is "+name);
    }
}

5.9

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
       System.out.print("Enter the number of girls: ");
       Scanner input = new Scanner(System.in);
       int num = input.nextInt();
       String name = "";
       String name2 = "";
       double cup = 0;
       double cup2 = 0;
       for(int i=0;i<num;i++)
       {
           String tname = input.next();
           double tmp = input.nextDouble();
           if(tmp>cup)
           {
               cup2=cup;
               name2 = name;
               cup=tmp;
               name=tname;
           }
           else if(tmp>cup2)
           {
               cup2=tmp;
               name2=tname;
           }
       }
       System.out.println("The hottest girl is "+name);
       System.out.println("The second hottest girl is "+name2);
    }
}

5.10

public class book {
    public static void main(String[] args)
    {
       int count=0;
       int base = 0;
       for(int i=100;i<=1000;i++)
       {
           if(i%5==0&&i%6==0)
           {
               count++;
               System.out.print(i+" ");
           }
           if(count%10==0&&count!=base) {
               System.out.print("\n");
               base+=10;
           }
       }
    }
}

5.11

public class book {
    public static void main(String[] args)
    {
       int count=0;
       int base = 0;
       for(int i=100;i<=1000;i++)
       {
           if(i%5==0^i%6==0)
           {
               count++;
               System.out.print(i+" ");
           }
           if(count%10==0&&count!=base) {
               System.out.print("\n");
               base+=10;
           }
       }
    }
}

5.12

public class book {
    public static void main(String[] args)
    {
       int n = 0;
       while(n*n<12000)
           n++;
       System.out.println("The n is "+n);
    }
}

5.13

public class book {
    public static void main(String[] args)
    {
        int n = 0;
        while(n*n*n<12000)
            n++;
        System.out.println("The n is "+(n-1));
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104466251