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

终于,今天开始正式的写循环这一章的题目,小伙伴们久等了,通常一个问题会有多种解决的方法,有小伙伴的方法比我的好可以留在评论中,相互学习

5.1统计正数和负数的个数然后计算这些数的平均值

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.print("Enter an integer, the input end if it is 0:");  
         Scanner input=new Scanner(System.in);  
//        int n=input.nextInt();
        double sum=0,ave;
        int positive=0,negative=0,count=0;
        int n=input.nextInt();
        while(n==0){
            System.out.println("nonononono");
            break;
        }
        while(n!=0){
            sum+=n;
            ++count;
            if(n>0){    
                positive++;
            }else
                negative++;


            n=input.nextInt();
         ave=sum/count;
        System.out.println("The number of positives is "+positive);
        System.out.println("The number of negatives if "+negative);
        System.out.println("The total is "+sum);
        System.out.println("The average is "+ave);
        }

    }
}

5.2重复加法

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         final int NUMBER_OF_ADDITION =10;//产生10道题
         int corretcount=0;
         int count=0;
         String output="";
         long startTime=System.currentTimeMillis(); 
         Scanner input=new Scanner(System.in); 
         while(countint number1=(int)(Math.random()*16+1);
             int number2=(int)(Math.random()*16+1);
             System.out.print("what is "+number1+"+"+number2+"?");
             int answer=input.nextInt();
         if(number1+number2==answer){
             System.out.println("you are correct!");
             corretcount++;
         }else
             System.out.println("you 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"+corretcount+"\nTest time is "+testTime/1000+" seconds\n"+output);
    }
}

5.3将千克转换为磅(1千克为2.2磅)

package nameyu;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
         int kilogram=1;
         System.out.printf("%-5s%-5s%n", "千克","磅 ");
         for(kilogram=1;(kilogram<=200);kilogram=kilogram+2){
             double pound=kilogram*2.2;
             System.out.printf("%-5d%-2.1f%n",kilogram,pound);
         }
         }
}           

5.6英里与千米之间的转换(1英里为1.609千米)

package nameyu;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int miles=1,kilometers=1;
        System.out.printf("%-5s%-5s%-6s%-5s%n","英里","千米","千米","英里");
        double answermiles,answerkilometers;
        for(miles=1,kilometers=20;miles<=10&&kilometers<=65;miles++,kilometers=kilometers+5){
            answerkilometers=miles*2.2;
            answermiles=kilometers*1.609;   
            System.out.printf("%-5d%-5.2f%-6d%-5.2f%n",miles,answerkilometers,kilometers,answermiles);
        }

    }
         }

5.7财务应用程序:计算将来的学费

package nameyu;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        double Current_Amount=10000;
        for(int years=1;years<=10;years++){
            Current_Amount=Current_Amount*1.05;
            }           
            System.out.println((int)Current_Amount);
            double firstyears=Current_Amount;
            int sum=(int)firstyears;
            for(int i=1;i<4;i++){

            firstyears=firstyears*1.05;
             sum+=(int)firstyears;  
            }System.out.println(sum);
            }
            }

5.8找出最高分
这道题,大家还得稍微的往后翻一下,用到一维数组来做的。我拿到手上第一反应就是用数组做,如果有其他方法可以在评论中留下你的代码,可能做题有了思维。

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.println("Enter student numbers:");
        int number=input.nextInt();//学生的个数
        String []student=new String[number];//存放学生的姓名
        double []score=new double [number];//存放学生的分数
        double max=0;//最高分
        int index=0;//下标
        for(int i=0;i"Enter student name:");
            student[i]=input.next();
            System.out.print("Enter student score:");
            score[i]=input.nextDouble();
            if(score[i]>max){
                max=score[i];
                index=i;
            }
        }
        System.out.println("The max score is "+score[index]+" The name is "+student[index]);
            }
            }

5.9找出两个分数最高的学生

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("Enter student numbers:");
        int number=input.nextInt();
        String []student=new String[number];
        double []score=new double[number];
        double max1=0,max2=0;
        int index1=0,index2=0;
        for(int i=0;i"Enter student name:");
             student[i]=input.next();
             System.out.print("Enter student score:");
             score[i]=input.nextDouble();
            if(score[i]>max1&&score[i]>max2){
                max1=score[i];
                index1=i;
            }else if(score[i]max2){
                max2=score[i];
                index2=i;
            }
        }
        System.out.println("The max score name is "+student[index1]+" score is "+score[index1]+" The second score name is "+student[index2]+" score is "+score[index2]);
            }
            }

5.10找出能被5和6整除的数

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int count=0;
        for(int i=100;i<=1000;i++){
            if(i%5==0&&i%6==0){
                count++;
                if(count%10!=0){
                    System.out.print(i+" ");
                }else
                    System.out.println(i);
            }

        }
            }
            }

5.11找出能被5或6整除,但不能被两者同时整除的数

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int count=0;
        for(int i=100;i<=1000;i++){
            if((i%5==0||i%6==0)&&(i%5!=0||i%6!=0)){
                count++;
                if(count%10!=0){
                    System.out.print(i+" ");
                }else
                    System.out.println(i);
            }

        }
            }
            }

5.12求满足n的平方大于12000的n的最小值

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int n=0;
        while(Math.pow(n,2)<12000){
            n++;

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

5.13求满足n的立方小于12000的n的最大值

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int n=0;
        while(Math.pow(n,3)<12000){
            n++;

        }
        System.out.println(n-1);
            }
            }

5.14计算最大公约数

package nameyu;

import java.util.Scanner;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter first integer :");
        int number1=input.nextInt();
        System.out.print("Enter second integer :");
        int number2=input.nextInt();
        int Remainder=-1;
        int Max=-1;
        do{
            Remainder=number1%number2;
            number1=number2;
            number2=Remainder;
             Max=Remainder;
        }while(number1%number2!=0);
        System.out.println("The greatest common divisor is "+Max);
    }
}

5.15显示ASCII码字符表

package nameyu;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int count=0;
        for(int i=33;i<=126;i++){

            count++;

            if(count%10!=0){
                System.out.print((char)i+" ");
            }
                else if(count%10==0){
                    System.out.println((char)i);
            }
            }
        }
            }

5.16找出一个整数的因子

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("Enter a number of integer :");
            int number=input.nextInt();
            int i;
            for(i=2;i<=number;i++){
                while(number%i==0){
                    number=number/i;
                    if(number!=1){
                        System.out.print(i+",");
                    }else
                        System.out.print(i);
                }
            }
        }
            }

5.16找出一个整数因子

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("Enter a number of integer :");
            int number=input.nextInt();
            int i;
            for(i=2;i<=number;i++){
                while(number%i==0){
                    number=number/i;
                    if(number!=1){
                        System.out.print(i+",");
                    }else
                        System.out.print(i);
                }
            }
        }
            }

5.17显示金字塔

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.println("Enter the number of line");
            int a =input.nextInt();
            for(int i=1; i<=a; i++) {
                for(int j=(a-i)*3-1; j>=0; j--) {
                    System.out.print(" ");
                }
                for(int j=i; j>=1; j--) {
                    if(j<10)
                        System.out.print("  ");
                    else 
                        System.out.print(" ");
                    System.out.print(j);
                }
                for(int j=2; j<=i; j++) {
                    if(j<10)
                        System.out.print("  ");
                    else 
                        System.out.print(" ");
                    System.out.print(j);
                }
                System.out.println("");
            }

            }
            }


5.18使用循环语句打印4个图案
图案1:

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.println("Enter the number of line");
            int number =input.nextInt();
             for(int i=1;i<=number;i++){
                 for(int j=1;j<=i;j++){
                System.out.print(j+" ");
                 }
                 System.out.println("");
             }

            }
            }

图案2:

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.println("Enter the number of line");
            int number =input.nextInt();
             for(int i=1;i<=number;i++){
                 for(int j=1;j<=(number-i)+1;j++){
                System.out.print(j+" ");
                 }
                 System.out.println("");
             }

            }
            }       

图案3:
直接拿5.17的修改下就是了。

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("Enter the number of line");
            int number =input.nextInt();
            for(int i=1; i<=number; i++) {
                for(int j=(number-i)*3-1; j>=0; j--) {
                    System.out.print(" ");
                }
                for(int j=i; j>=1; j--) {
                    if(j<10)
                        System.out.print("  ");
                    else 
                        System.out.print(" ");
                    System.out.print(j);
                }
                 System.out.println("");
             }

            }
            }

图案4:

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("Enter the number of line");
            int number =input.nextInt();
            for(int i=1; i<=number; i++) {
                for(int k=1; k"   ");
                }
                for(int j=1;j<=(number-i)+1;j++) {
                    if(j<10)
                        System.out.print("  ");
                    else 
                        System.out.print(" ");
                    System.out.print(j);
                }
                 System.out.println("");
             }

            }
            }   

5.19打印金字塔形的数字
看见论坛里有大神说用制表符输出更加美观,用空格不整齐,也就试了一下。感觉挺好,get了

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("Enter the number of line");
            int line =input.nextInt();
            for(int i=1;i<=line;i++){
                for(int j=0;j"\t");
                }
                for(int j=1;j2, i);j*=2){
                    System.out.print(j+"\t");
                }
                for(int j=(int)Math.pow(2, i-2);j>0;j/=2){
                    System.out.print(j+"\t");
                }
                System.out.println();
             }

            }
            }

5.20

package nameyu;

public class PrimeNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final int NUMBER_OF_PRIMES=1000;
        final int NUMBER_OF_PRIMES_PER_LINR=8;
        int count=0;
        int number=2;   
//      System.out.println("The first 50 prime numbers are \n");
        while(numberboolean isPrime=true;
            for(int divisor=2;divisor<=number/2;divisor++){
            if(number%divisor==0){
                isPrime=false;
                break;
            }
            }

        if(isPrime){
            count++;
            if(count%NUMBER_OF_PRIMES_PER_LINR==0){
                System.out.println(number);
            }else
                System.out.print(number+"\t");
        }
        number++;
    }
}

}

综合题将会分开发出来,大家注意一下就好了。标题会是“Java语言程序设计基础篇原书第十版第五章编程综合题答案“

你可能感兴趣的:(Java语言程序设计基础篇原书第十版第五章编程练习题答案)