JAVA语言程序设计(基础篇)第十版——第二章 基本程序设计 编程练习题(参照答案)

(2.2-2.12小节)+(2.13-2.17小节)

(2.2-2.12小节)

2.1 (将摄氏温度转换为华氏温度)

import java.util.Scanner;

public class X {

	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a degree in Celsius : ");
		double Celsius=input.nextDouble();
		
		input.close();//以后要养成好习惯,输入完要记得关input
		
		double Fahrenheit=(9/5)*Celsius+32;
		System.out.println(Celsius +" Celsius is " + Fahrenheit + " Fahrenheit " );
	
	}

}

2.2(计算圆柱体的体积)

import java.util.Scanner;

public class X1 {

	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();
		input.close();
		final double PI=3.14159008;
		double area=radius*radius*PI;
		double volume=area*length;
		
		System.out.println("The area is " + area );
		System.out.println("The volume is " + volume);
		
		

	}

}

2.3(将英尺转换为米)

import java.util.Scanner;

public class X2 {

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

	}

}

2.4(将磅转换为千克)

import java.util.Scanner;

public class X4 {

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

	}

}

*2.5(财务应用程序:计算小费)

import java.util.Scanner;

public class X5 {

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

}

**2.6(求一个整数各位数的和)

import java.util.Scanner;

public class X6 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a number between 0 and 1000: " );
		int integer=input.nextInt();
		input.close();
			
		int unitsDigit=integer%10;
		int tensDigit=(integer/10)%10;
		int hundredsDigit=(integer/100)%10;
		
		int sum=unitsDigit+tensDigit+hundredsDigit;
		System.out.println("The sum of the digits is  " + sum);
		
	}

}

*2.7(求出年数)

暂时不会

*2.8(当前时间)

暂时不会

2.9(物理:加速度)

import java.util.Scanner;

public class X9 {

	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();
		input.close();
		
		double acceleration=(v1-v0)/t;
		System.out.println("The average acceleration is " + acceleration);
		
	}

}

2.10(科学:计算能量)

import java.util.Scanner;

public class X10 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the amount of water in kilograms : ");
		double amount=input.nextDouble();
	
		System.out.print("Enter the initial temperature : ");
		double initialTemperature=input.nextDouble();
		
		System.out.print("Enter the final temperature : ");
		double finalTemperature=input.nextDouble();
		
		input.close();//
		
		double energy=amount*(finalTemperature-initialTemperature)*4184;
		
		System.out.println("The energy needed is " + energy);
		
	}

}

2.11(人口统计)

import java.util.Scanner;

public class X11 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of years: ");
		int numbers=input.nextInt();
		input.close();
		
		int secondsTotal=365*(60*60*24);//一年总共有多少秒
		double live=secondsTotal/7;
		double dead=secondsTotal/13;
		double in=secondsTotal/45;
		
		double population= 312032486 + numbers*(live + in - dead );
		
		System.out.println("The population in " + numbers + " years is " + (int)population);

	}

}

2.12(物理:求出跑道长度)

import java.util.Scanner;

public class X12 {

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

}

**2.13(财务应用程序:复制值)

(用循环一起完成了 第五章的编程习题 5.30)

import java.util.Scanner;

public class X13 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the monthly saving amount : ");
		double save=input.nextDouble();
		
		System.out.print(" 请问您想要显示第几个月后的账户值 (1-12): ");
		int number=input.nextInt();
		
		input.close();
		
		double sum=0;
		if(number>0 && number<13) {
			
			for(int i=1; i<=number; i++) {
				
				sum= ((sum+save)*(1+0.00417));
			}
			
			
		}
		
		System.out.println("After the " + number +" month, the account value is $" + sum );
		
		
	}

}

*2.14(医疗应用程序:计算BMI)

import java.util.Scanner;

public class X14 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter weight in pounds (输入的体重以磅为单位):");
		double weight=input.nextDouble();

		System.out.print("Enter height in pounds (输入的身高以英寸为单位):");
		double height=input.nextDouble();
		input.close();
		 
		double weight1= weight*0.45359237;
		double height1= height*0.0254;
		double bmi=weight1/Math.pow(height1,2);
		System.out.print(" BMI is :" + bmi );
		
		
	}

}

2.15(几何:两点间的距离)

import java.util.Scanner;

public class X15 {

	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();
		input.close();
		
		double a=Math.pow(x2-x1,2) + Math.pow(y2-y1,2);
		double distance= Math.pow(a, 0.5);
		
		System.out.print("The distance between the two points is " + distance );
		
	}
	
}

2.16(几何:六边形面积)

import java.util.Scanner;

public class X16 {

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

}

*2.17(科学:风寒温度)

import java.util.Scanner;

public class X17 {

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

}

2.18(打印表格)

public class X18 {

	public static void main(String[] args) {
		System.out.println("  a    b    pow(a,b)");
		System.out.println("  1    2    "+(int)Math.pow(1,2));
		System.out.println("  2    3    "+(int)Math.pow(2,3));
		System.out.println("  3    4    "+(int)Math.pow(3,4));
		System.out.println("  4    5    "+(int)Math.pow(4,5));
		System.out.println("  5    6    "+(int)Math.pow(5,6));
		
	}

}

*2.19(几何:三角形的面积)

import java.util.Scanner;

public class X19 {

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

}

(2.13-2.17小节)

*2.20(财务应用程序:计算利息)

import java.util.Scanner;

public class X20 {

	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 interestRate=input.nextDouble();
		input.close();
		
		Double interest= balance * (interestRate/1200);
		
		System.out.print("The interest is " + interest );
	}

}

*2.21(财务应用:计算未来投资值)

import java.util.Scanner;

public class X21 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter investment amount:");
		double investment=input.nextDouble();
		 
		System.out.print("Enter annual interest rate in percentage:");
		double annualInterestRate=input.nextDouble();
		
		System.out.print("Enter number of years:");
		int years=input.nextInt();
		input.close();
		
		double value= investment * Math.pow( (1+annualInterestRate/12/100) , 12*years) ;
		System.out.print("Accumulated value is $" + (float)value);
		
	}

}

*2.22(财务应用:货币单位)

*2.23(驾驶费用)

发布了10 篇原创文章 · 获赞 5 · 访问量 2468

猜你喜欢

转载自blog.csdn.net/cxj18867076391/article/details/104838624