JAVA语言程序设计(基础篇)原书第10版第2章编程练习题答案

1

import java.util.Scanner;
public class test1 {

	public static void main(String[] args) {
		
           Scanner input=new Scanner(System.in);
           System.out.print("Enter a degree in Celsius:");
           double c=input.nextDouble();
           double f=(9.0/5)*c+32;
           System.out.println(c+" Celsius is "+f+" Fahrenheit");
           input.close();
	}
	

}

2

import java.util.Scanner;
public class test2 {
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
        System.out.print("Enter the radius and length of a cylinder:");
        double radius=input.nextDouble();
        double length=input.nextDouble();
        double area=radius*radius*Math.PI;
        double volume=area*length;
        System.out.println("The area is "+area);
        System.out.println("The volume is "+volume);
        input.close();
	}

}

3

import java.util.Scanner;

public class test3 {
	public static void main(String[] args) {
		 Scanner input=new Scanner(System.in);
	        System.out.print("Enter a value for feet:");
	        double feet=input.nextDouble();
	        double inch=feet*0.305;
	        System.out.println(feet+" feet is "+inch+" meters");
	        input.close();
	}

}

4

import java.util.Scanner;
public class test4 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a number in pounds:");
        double pounds=input.nextDouble();
        double kg=pounds*0.454;
        System.out.println(pounds+" pounds is "+kg+" kilograms");
        input.close();
    }
}

 

5

import java.util.Scanner;
public class test5{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the subtotal and a gratuity rate:");
        double subtotal=input.nextDouble();
        double rate=input.nextDouble();
        double gratuity=subtotal*(rate/100.0);
        double total=subtotal+gratuity;
        System.out.println("The gratuity is $"+gratuity+"and total is $"+total);
        input.close();
    }
}

6

import java.util.Scanner;
public class test6 {
    public static void main(String[] args){
         Scanner input=new Scanner(System.in);
         System.out.print("Enter a number between 0 and 1000:");
         int n=input.nextInt();
         int sum=0;
         int m=n;
         for(;m!=0;)
         {
             sum+=m%10;
             m=m/10;
         }
         System.out.println("The sum of the digits is "+sum);
         input.close();
    }
}

7

import java.util.Scanner;
public class test7 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the number of minutes:");
        long minutes=input.nextLong();
        long year,date;
        year=minutes/(365*24*60);
        date=minutes/(24*60)-year*365;
        System.out.println(minutes+" minutes is approximately "+year+" years "+" and "+date+" dates");
        input.close();
    }
}

8

 

9

import java.util.Scanner;
public class test9 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter v0,v1,and t:");
        double v0=input.nextDouble();
        double v1=input.nextDouble();
        double t=input.nextDouble();
        double a=(v1-v0)/t;
        System.out.println("The average accereration is "+a);
        input.close();
    }
}

10

import java.util.Scanner;
public class test10 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the amount of water in kilgrams:");
        double M=input.nextDouble();
        System.out.print("Enter the initial temperature:");
        double t0=input.nextDouble();
        System.out.print("Enter the final temperature:");
        double t1=input.nextDouble();
        double Q=M*(t1-t0)*4184;
        System.out.println("The energy needed is "+Q);
        input.close();
    }
}

11

import java.util.Scanner;
public class test11 {
    public static void main(String[] args)
    {
        final long now=312032486;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the number of years:");
        double years=input.nextDouble();
        double minutes=years*365*24*60*60;
        double future=minutes/7.0+minutes/45.0-minutes/13.0;
        System.out.println("The population in "+years+" is "+future);
        input.close();
    }
}

12

import java.awt.*;
import java.util.Scanner;
public class test12 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter speed and acceleration:");
        double v=input.nextDouble();
        double a=input.nextDouble();
        double length=Math.pow(v,2)/(2*a);
        System.out.println("The minimum runway length for this airplane is "+length);
        input.close();
    }
}

13

import java.util.Scanner;
public class test13 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        final double rate=0.00417;
        System.out.print("Enter the monthly saving amount:");
        double money=input.nextDouble();
        double sum=0;
        for(int i=1;i<=6;i++)
        {
            double a=money*Math.pow(1+rate,i);
            sum=sum+a;
        }
        System.out.println("After six month,the account value is $"+sum);
        input.close();
    }
}

14

import java.util.Scanner;
public class test14 {
    public static void main(String[] args)
    {
        final double kg=0.45359237;
        final double meter=0.0254;
        Scanner input=new Scanner(System.in);
        System.out.print("Enter weight in pounds:");
        double weight=input.nextDouble();
        System.out.print("Enter height in inches:");
        double height=input.nextDouble();
        double BIM=weight*kg/Math.pow(height*meter,2);
        System.out.println("BMI is "+BIM);
        input.close();
    }
}

15

import java.util.Scanner;
public class test15 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter x1 and y1:");
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        System.out.print("Enter x2 and y2:");
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double distance=Math.pow(Math.pow(x2-x1,2)+Math.pow(y2-y1,2),0.5);
         System.out.println("The distance between the two points is "+distance);
         input.close();
    }
}

16

import java.util.Scanner;
public class test16{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the side:");
        double side=input.nextDouble();
        double area=3*Math.pow(3,0.5)*Math.pow(side,2)/2;
        System.out.println("The area of the hexagon is "+area);
        input.close();
    }
}

17

import java.util.Scanner;
public class test17 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the temperature in Fahrenheit between -58F and 41F:");
        double tem=input.nextDouble();
        System.out.print("Enter the wind speed (>=2) in miles per hour:");
        double v=input.nextDouble();
        double Twc=35.74+0.6215*tem-35.75*Math.pow(v,0.16)+0.4275*tem*Math.pow(v,0.16);
        System.out.println("The wind chill index is "+Twc);
        input.close();
    }
}

18

19

import java.util.Scanner;
public class test19 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter three points for a triangle :");
        double x1,y1,x2,y2,x3,y3;
        x1=input.nextDouble();
        y1=input.nextDouble();
        x2=input.nextDouble();
        y2=input.nextDouble();
        x3=input.nextDouble();
        y3=input.nextDouble();
        double L1,L2,L3;
        L1=Math.pow(Math.pow(x2-x1,2)+Math.pow(y2-y1,2),0.5);
        L2=Math.pow(Math.pow(x3-x1,2)+Math.pow(y3-y1,2),0.5);
        L3=Math.pow(Math.pow(x3-x2,2)+Math.pow(y3-y2,2),0.5);
        double s=(L1+L2+L3)/2;
        double area=Math.pow(s*(s-L1)*(s-L2)*(s-L3),0.5);
        System.out.println("The area of the triangle is "+area);
        input.close();
    }
}

20

import java.util.Scanner;
public class test20{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter balance and interest rate(e.g.,3 for 3%):");
        double balance=input.nextDouble();
        double rate=input.nextDouble();
        double interest=balance*(rate/1200);
        System.out.println("The interest is "+interest);
        input.close();
    }
}

21

import java.util.Scanner;
public class test21 {
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter investment amount:");
        double amount=input.nextDouble();
        System.out.print("Enter annual interest rate in percentage:");
        double rate=input.nextDouble();
        System.out.print("Enter number of years:");
        double year=input.nextDouble();
        double money=amount*Math.pow((1+rate/100),year);
        System.out.println("Accumulated value is $"+money);
        input.close();
    }
}

22

23

import java.util.Scanner;
public class test23{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the driving distance:");
        double distance=input.nextDouble();
        System.out.print("Enter miles per gallon:");
        double miles=input.nextDouble();
        System.out.print("Enter price per gallon:");
        double price=input.nextDouble();
        double cost=distance/miles*price;
        System.out.println("The cost of driving is $"+cost);
        input.close();
    }
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值