Java语言程序设计(沈泽刚主编)第3版 第1~3章编程练习答案

Java语言程序设计(沈泽刚主编)第3版 第1~3章编程练习答案

第1章Java语言概述
1.1-编写程序,打印您的姓名和年龄。

public class PrintName {
    public static void main(String[] args){
        System.out.println("姓名:王国栋 年龄:24") ;
    }
}

1.2-编写程序,计算1+2+3+4+5+6+7+8+9+10的结果.

public class PrintSum {
    public static void main(String[] args){
        int sum = 0 ;
        for(int i=1; i<=10; i++){
            sum += i ;
        }
        System.out.println(sum) ;
    }
}

1.3-编写程序,计算并显示半径为5.5的圆形的周长和面积。

public class CircleTest {
    public static void main(String[] args){
        System.out.println(2 * Math.PI * 5.5) ;
        System.out.println(Math.PI * 5.5 * 5.5) ;
    }
}

1.4-编写程序,打印以下图形。
在这里插入图片描述

public class PrintFigure {
    public static void main(String[] args){
        for(int i=0; i<5; i++){
            for(int j=0; j<=i; j++){
                System.out.print("* ") ;
            }
            System.out.println() ;
        }
    }
}

第2章 Java语言基础
2.1 编写程序键盘输入一个double型的华氏温度,将其转换为摄氏温度,转换公式为:摄氏温度=(5/9)*(华氏温度-32)

public class TempConvert {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入华氏温度:") ;
        double t1 = input.nextDouble() ;
        double t2 = (5.0 / 9) * (t1 - 32) ;
        System.out.println("摄氏温度为:" + t2) ;
    }
}

2.2键盘输入圆柱底面半径和高,计算圆柱的体积

import java.util.Scanner;
public class Cylinder {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.println("请输入圆柱的半径和高:") ;
        double radius = input.nextDouble() ;
        double height = input.nextDouble() ;
        System.out.println("圆柱的体积为:" + Math.PI * radius * radius * height) ;
    }
}

2.3键盘输入你的体重(千克)和身高(米),计算身体质量指数。公式为:BIM = 体重 / 身高的平方

import java.util.Scanner;
public class BodyMassIndex {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请依次输入您的体重和身高:") ;
        double mass = input.nextDouble() ;
        double height = input.nextDouble() ;
        double Bim = mass / (height * height) ;
        System.out.println(Bim) ;
    }
}

2.4 读取一个0-1000的整数,将该整数的各位数字相加,例如:输入932,各位数字之和是14

import java.util.Scanner;
public class DigitSum {
    public static void main(String[] args){
        Scanner input =  new Scanner(System.in) ;
        System.out.print("请输入一个0~1000之间的整数:") ;
        int num = input.nextInt() ;
        if(num >=0 && num <= 9){
            System.out.println(num) ;
        }else if(num >= 10 && num <= 99){
            System.out.println(num/10 + num % 10) ;
        }else if(num >= 100 && num <= 999){
            System.out.println(num%10 + (num/10)%10 + num/100) ;
        }else if(num == 1000){
            System.out.println(1) ;
        }else{
            System.out.println("您输入的数据有误") ;
        }
    }
}

2.5 编写程序,显示当前时间

public class CurrentTime {
    public static void main(String[] args){
        long t = System.currentTimeMillis() ; //1970年1月1日00:00:00开始到当前时刻的毫秒数
        long seconds = t / 1000 ; //总秒数
        long s = seconds % 60 ;
        long minutes = seconds / 60 ; //总分钟数
        long m = minutes % 60 ;
        long hours = minutes / 60 ; //总小时数
        long h = hours %  24 ;
        System.out.println("当前时间: " + (h+8) + ":" + m + ":" + s + " (GMT)") ;
    }
}

2.6键盘输入a,b,c的值,计算下列表达式的值

import java.util.Scanner;
public class EquationRoot {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        double a = input.nextDouble() ;
        double b = input.nextDouble() ;
        double c = input.nextDouble() ;
        double result = ((-b) + Math.sqrt(b*b - 4*a*c)) / (2*a) ;
        System.out.println(result) ;
    }
}

2.7 计算贷款的每月支付额。键盘输入年款的年利率,总金额,年数,程序计算出每月支付金额和总偿还金额。

public class ComputeLoan {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.println("请输入贷款的年利率:") ;
        Double t1 = input.nextDouble() ;
        double t4 = t1 / 1200 ; //月利率
        System.out.println("请输入总金额:") ;
        Double t2 = input.nextDouble() ;
        System.out.println("请输入年数:") ;
        Double t3 = input.nextDouble() ;
        Double monthPayment = (t2 * t4) /(1 - (1 / Math.pow((1+t4), t3*12))) ;
        Double totalPayment = monthPayment * 12 * t3 ;
        System.out.println("月支付金额:" + monthPayment + "总偿还金额:" + totalPayment) ;
    }
}

第3章 选择与循环
3.1 键盘输入一个整数,判断奇数还是偶数

import java.util.Scanner;
public class EvenOdd {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        int num = input.nextInt() ;
        if(num % 2 == 0){
            System.out.println(num + "是偶数") ;
        }else{
            System.out.println(num + "是奇数") ;
        }
    }
}

3.2编写程序,要求用户键盘输入一个年份,输出该年是否是闰年。

import java.util.Scanner;
public class LeapYear {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        int year = input.nextInt() ;
        if((year % 4 == 0) && (year% 100 != 0)){
            System.out.println(year + "年是闰年") ;
        }else if(year % 400 == 0){
            System.out.println(year + "年是闰年") ;
        }else{
            System.out.println(year + "年不是闰年") ;
        }
    }
}

3.3键盘输入4个整数,找出最大值和最小值并打印输出

import java.util.Scanner;
public class MaxMin {
    public static void main(String[] args){
        int [] num = new int[4] ;
        int max = Integer.MIN_VALUE ;
        int min = Integer.MAX_VALUE ;
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入4个整数:") ;
        for(int i=0; i<num.length; i++){
            num[i] = input.nextInt() ;
        }
        for(int i=0;i<num.length; i++){
            if(num[i] >= max){
                max = num[i] ;
            }
            if(num[i] <= min){
                min = num[i] ;
            }
        }
        System.out.println("四个数字的最大值为:" + max + "  最小值为:" + min) ;
    }
}

3.4判断一元二次方程有无实数根。提示用户输入方程系数a,b,c,判断方程有无实数根,有实数根则打印实数根。

import java.util.Scanner;
public class EquationRoot {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.println("请输入一元二次方程的系数a,b,c:") ;
        Double a = input.nextDouble() ;
        Double b = input.nextDouble() ;
        Double c = input.nextDouble() ;
        if(b*b - 4*a*c > 0){
            Double x1 = (-b + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a) ;
            Double x2 = (-b - Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a) ;
            System.out.println("方程有两个实数根:" + "第一个实数根=" + x1 + " 第二个实数根=" +x2) ;
        }else if(b*b - 4*a*c < 0){
            System.out.println("方程无实数根") ;
        }else{
            Double x3 = (-b) / (2 * a) ;
            System.out.println("方程就一个实数根=" + x3) ;
        }
    }
}

3.5 从键盘输入一个百分制的成绩,例如85,输出良好,要求用switch实现.


import java.util.Scanner;
public class GradeTest {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.println("请输入成绩,要求输入的成绩大于等于0,且小于等于100") ;
        int num = input.nextInt() ;
        int scores = num / 10 ;
        String grade = "" ;
        switch(scores){
            case 10 : grade = "优秀"; break ;
            case 9 : grade = "优秀"; break ;
            case 8 : grade = "良好"; break ;
            case 7 : grade = "中等"; break ;
            case 6 : grade = "及格"; break ;
            default : grade = "不及格"; break ;
        }
        System.out.println(grade) ;
    }
}

3.6 键盘输入10个整数,比较并输出其中的最大值和最小值

import java.util.Scanner;
public class TenNumber {
    public static void main(String[] args){
        int [] number = new int [10] ;
        int max = Integer.MIN_VALUE ;
        int min = Integer.MAX_VALUE ;
        Scanner input = new Scanner(System.in) ;
        for(int i=0; i<number.length; i++){
            number[i] = input.nextInt() ;
        }
        for(int i=0; i<number.length; i++){
            if(max <= number[i]){
                max = number[i] ;
            }
            if(min >= number[i]){
                min = number[i] ;
            }
        }
        System.out.println("10个整数的最大值 = " + max + "  最小值 = " + min) ;
    }
}

3.7 编写程序,要求用户从键盘输入一个年份和月份,显示这个月的天数。


import java.util.Scanner;
public class YearMonth {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入年份:") ;
        int year = input.nextInt() ;
        System.out.print("请输入月份:") ;
        int month = input.nextInt() ;
        int monthDays = 0 ;
        switch(month){
            case 1 : case 3 : case 5 : case 7 : case 8 : case 12 : monthDays = 31; break ;
            case 4 : case 6 : case 9 : case 10 : case 11 : monthDays = 30 ; break ;
            case 2 :
                if((year % 4 == 0) && (year % 100 != 0)){
                    monthDays = 29 ;
                }else if(year % 400 == 0){
                    monthDays = 29 ;
                }else{
                    monthDays = 28 ;
                }
            default : System.out.println("输入月份错误,请重新输入") ; break ;
        }
        System.out.println(year + "年" + month + "月" + "有" + monthDays + "天") ;
    }
}

3.9 编写程序,要求用户从键盘输入一个年份,程序输出该年出生的人的生肖。


import java.util.Scanner;
public class AnimalYear {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入一个年份:") ;
        int year = input.nextInt() ;
        String animal = new String("") ;
        switch(year % 12){
            case 4 : animal = "鼠"; break ;
            case 5 : animal = "牛"; break ;
            case 6 : animal = "虎"; break ;
            case 7 : animal = "兔"; break ;
            case 8 : animal = "龙"; break ;
            case 9 : animal = "蛇"; break ;
            case 10 : animal = "马"; break ;
            case 11 : animal = "羊"; break ;
            case 12 : animal = "猴"; break ;
            case 1 : animal = "鸡"; break ;
            case 2 : animal = "狗"; break ;
            case 3 : animal = "猪"; break ;
        }
        System.out.println("您的属相是:" + animal) ;
    }
}

3.10 编写程序,计算并输出0-1000含有7或者是7的倍数的整数之和及个数。


public class SevenDemo {
    public static void main(String[] args){
        int sum = 0 ;
        for(int i=7; i<=1000; i++){
            if((i%7 == 0)){
                sum ++ ;
                System.out.println(i) ;
            }else if((i%10 == 7) || ((i / 10) % 10) == 7 || (i / 100 == 7)){
                sum ++ ;
                System.out.println(i) ;
            }
        }
        System.out.println("总个数为:" + sum) ;
    }
}

3.11 编写程序,显示从100到1000所有能被5和6整除的数,每行显示10个数字, 数字之间通过一个空格隔开。

public class FiveSixDemo {
    public static void main(String[] args){
        int count = 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 = 0 ;
                    System.out.println() ;
                }
            }
        }
    }
}

3.12 编写程序,从键盘输入一个正整数,计算并输出该整数的各位数字之和。


public class DigitSum {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入一个整数:") ;
        int num = input.nextInt() ;
        int sum = 0, result = num ;
        while(num > 0){
            sum += (num % 10) ;
            num /= 10 ;
        }
        System.out.println(result + "的各位数字之和为:" + sum) ;
    }
}

3.13 编写程序,输入一个十进制正整数,输出对应二进制数,不允许使用Java中自带的方法。

import java.util.Scanner;
public class ToBinaryDemo {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入一个正整数:") ;
        String result = new String("") ;
        int n = input.nextInt() ;
        while(n > 0){
            result += (n % 2) ;
            n /= 2;
        }
        for(int i=result.length()-1; i>=0; i--){
            System.out.print(result.charAt(i)) ;
        }
    }
}

3.14 编写程序,计算下面级数之和。

public class SeriesSum {
    public static void main(String[] args){
        double sum = 0 ;
        for(int i=1; i<=97; i+=2){
            sum += ((i * 1.0) / (i+2)) ;
        }
        System.out.println(sum) ;
    }
}

3.15 求解鸡兔同笼问题,鸡和兔在同一个笼子里,共有100条腿,40个头,问鸡兔各有多少只?

public class ChickenHare {
    public static void main(String[] args){
        for(int chicken=0; chicken<=40; chicken++){
            for(int rabbit=0; rabbit<=25; rabbit++){
                if((chicken + rabbit == 40) && (2 * chicken + 4 * rabbit == 100)){
                    System.out.println("笼子中有" + chicken + "只鸡" + rabbit + "个兔子") ;
                }
            }
        }
    }
}

3.16 编写程序,打印出所有的水仙花数。水仙花数是这样的三位数,它的各位数字的立方和等于数字本身。

public class Narcissus {
    public static void main(String[] args){
        for(int i=100; i<=999; i++){
            int x = (int)(Math.pow((i%10),3)) ;
            int y = (int)(Math.pow((i/10)%10,3)) ;
            int z = (int)(Math.pow(i/100, 3)) ;
            if(x+y+z == i){
                System.out.println(i) ;
            }
        }
    }
}

3.17 编写程序,键盘输入两个数字,计算最大公约数与最小公倍数。

import java.util.Scanner;
public class GcdLcm {
    public static int Gcd(int m, int n){
        if(n == 0){
            return m ;
        }
        return Gcd(n, m%n) ;
    }
    public static void main(String[] args){
       Scanner input = new Scanner(System.in) ;
       System.out.print("请输入两个整数:") ;
       int m = input.nextInt() ;
       int n = input.nextInt() ;
       System.out.println("最大公约数:" + Gcd(m,n)) ;
       System.out.println("最小公倍数:" + (m * n) / Gcd(m,n)) ;
    }
}

3.18 编写程序,求出1~1000的所有完全数,完全数就是该数的所有因子之和等于该数。
例如:28=1+2+4+7+14


public class PerfectNumber {
    public static boolean perfectNumber(int m){
        int sum = 0 ;
        for(int i=1; i<m; i++){
            if(m % i == 0){
                sum += i ;
            }
        }
        if(sum == m){
            return true ;
        }
        return false ;
    }
    public static void main(String[] args){
        for(int i=1; i<=1000; i++){
            if(perfectNumber(i)){
                System.out.println(i) ;
            }
        }
    }
}

/**
3.19 编写程序读入一个整数,显示该整数的所有素数因子。例如:输入的整数是120,则输出的结果应为2,2,2,3,5


import java.util.Scanner;
public class PrimeFactor {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("输入一个正整数:") ;
        int n = input.nextInt() ;
        int num = n ;
        for(int i=2; i<n; i++){
            if(num % i == 0){
                System.out.print(i + " ") ;
                num /= i ;
                i -- ;
            }
        }
    }
}

3.20编写程序,计算n=10000,20000,…,100000时,Π的值。

public class ComputePI {
    public static Double PI(int n){
        double sum = 0.0 ;
        for(int i=1; i<=n; i+=2){
          sum = sum + (1.0 / (2*i - 1)) - (1.0 / (2*i + 1)) ;
        }
        return 4 * sum ;
    }
    public static void main(String[] args){
        for(int n=10000; n<=100000; n+=10000){
            System.out.println("n = " + n + "  PI = " + PI(n)) ;
        }
    }
}

  • 15
    点赞
  • 152
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值