Java语言程序设计基础篇编程练习题-第二章

2.1(摄氏温度转华氏温度)

import java.util.Scanner;
public class Test2_01 {
     
	public static void main(String[] args) {
     
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a degree in Celsius:");
//		double celsius = input.nextDouble();
		int celsius = input.nextInt();
		input.close();
		double fahrenheit = (9.0 / 5) * celsius + 32;
		System.out.println(celsius + " Celsius is " + fahrenheit + " Fahrenheit");
	}
}

2.2(计算圆柱体的体积)

import java.util.Scanner;
public class Test2_02 {
     
	public static void main(String[] args) {
     
		final double p = 3.1415926;
		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();
		double area = radius * radius * p;
		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 Test2_03 {
     
	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 = 0.305 * feet;
		System.out.println(feet + " feet is " + meters + " meters");
	}
}

2.4(将磅转换为千克)

import java.util.Scanner;
public class Test2_04 {
     
	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 = pounds * 0.454;
		System.out.println(pounds + " pounds is " + kilograms + " kilograms");
	}
}

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

import java.util.Scanner;
 
public class Test2_05 {
     
	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();
		input.close();
		double gratuity = rate / 100 * subtotal; 
		System.out.println("The gratuity is $" + gratuity + " and total is $" + (gratuity + subtotal));
	}
}

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

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

2.7(求出年数)

import java.util.Scanner;
 
public class Test2_07 {
     
	public static void main(String[] args) {
     
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of minutes: ");
		int minutes = input.nextInt();
		input.close();
		
		int days = minutes / 60 / 24;
		int years = days / 365;
		int day = days % 365;
		
		System.out.println(minutes + " minuise is approximately " + years + " years and " + day + " days");
	}
}

2.8(当前时间)

import java.util.Scanner;
 
public class Test2_08 {
     
	public static void main(String[] args) {
     
		//获取系统时钟数
		long totalMilliseconds = System.currentTimeMillis();
		//整除1000得到总秒数
		long totalSeconds = totalMilliseconds / 1000;
		//总秒数对 60 取余得当现在时间的秒数
		long currentSeconds = totalSeconds % 60;
		//总秒数对 60 取整得当现在时间的总分钟数
		long totalMinutes = totalSeconds / 60;
		//总分钟数对 60 取余得当现在时间的分钟数
		long currentMinute = totalMinutes % 60;
		//总分钟数对 60 取整得当现在时间的总小时数
		long totalHours = totalMinutes / 60;
		//总小时数对 24 取余得当现在时间的小时数
		long currentHour = totalHours % 24;
		
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the time zone offset to GMT: ");
		int time = input.nextInt();
		input.close();
		int hour = (int)currentHour + time;
		System.out.println("The current time is " + hour + ":" + currentMinute + ":" + currentSeconds);
	}
}

2.9(物理,加速度)

import java.util.Scanner;
 
public class Test2_09 {
     
	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 acc = (v1 - v0) / t;
		
		System.out.println("The average acceleration is " + acc);
	}
}

2.10(科学,计算能量)

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

2.11(人口统计)

import java.util.Scanner;
 
public class Test2_11 {
     
	public static void main(String[] args) {
     
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of years: ");
		int years = input.nextInt();
		input.close();
		int num = 312032486;
		int time = 3600 * 24 * 365;
		int birth = time / 7;
		int die = time / 13;
		int in = time / 45;
		int change = birth + in - die;
		for(int i = 0; i < years; i++) {
     
			num += change; 
		}
		System.out.println("The population in " + years + " years is " + num);
	}
}

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

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

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

import java.util.Scanner;

public class Test2_13 {
     
	public static void main(String[] args) {
     
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the monthly saving amount: ");
		double amount = input.nextDouble();
		input.close();
		double value = 0;
		for(int i = 0; i < 6; i++) {
     
			value = (amount + value) * (1 + 0.00417);
		}
		System.out.println("After the sixth month, the account value is $" + value);
	}
}

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

import java.util.Scanner;

public class Test2_14 {
     
	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 inches: ");
		double height = input.nextDouble();
		input.close();
		weight = weight * 0.4539237;
		height = height * 0.0254;
		double bmi = weight / (height * height);
		System.out.println("BMI is " + bmi);
	}
}

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

import java.util.Scanner;
public class Test2_15 {
     
	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 ang y2: ");
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		input.close();
		double d1 = Math.pow((x2 - x1),2);
		double d2 = Math.pow((y2 - y1),2);
		double distance = Math.pow(d1 + d2, 0.5);
		System.out.println("The distance between the points is " + distance);
	}
}

2.16(几何,六边形面积)

import java.util.Scanner;

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

2.17(科学,风寒温度)

import java.util.Scanner;

public class Test2_17 {
     
	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 temperature = input.nextDouble();
		System.out.print("Enter the wind speed (>=2) in miles per hour: ");
		int wind = input.nextInt();
		input.close();
		double d = Math.pow(wind, 0.16);
		double t = 35.74 + 0.6215 * temperature - 35.75 * d + 0.4275 * temperature * d;
		System.out.println("The wind chill index is " + t);
	}
}

2.18(打印表格)

import java.util.Scanner;

public class Test2_18 {
     
	public static void main(String[] args) {
     
		System.out.println("a\tb\tpow(a,b)");
		for(int a = 1; a <= 5; a++) {
     
			int b = a + 1;
			int pow = (int)(Math.pow(a, b));
			System.out.println(a + "\t" + b + "\t" + pow);
		}
	}
}

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

import java.util.Scanner;

public class Test2_19 {
     
	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((x2 - x3),2) + Math.pow((y2 - y3),2), 0.5);
		double s3 = Math.pow(Math.pow((x3 - x1),2) + Math.pow((y3 - y1),2), 0.5);
		double s = (s1 + s2 + s3) / 2;
		
		double area = Math.pow(s * (s - s1) * (s - s2) * (s - s3), 0.5);
		System.out.println("The area of the triangle is " + area);
	}
}

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

import java.unit.Scanner;

public class Test2_20 {
     
	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();
		input.close();
		double amount = balance * (rate / 1200);
		System.out.println("The interest is " + amount);
	}
}

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

import java.util.Scanner;

public class Test2_21 {
     
	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:");
		int years = input.nextInt();
		input.close();
		double value = amount * Math.pow((1 + rate/1200), (years * 12));
		System.out.println("Accumulated value is $" + value);
	}
}

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

import java.util.Scanner;

public class Test2_22 {
     
	public static void main(String[] args) {
     
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number: ");
		int num = input.nextInt();
		input.close();
		int num1 = num / 100;
		int num2 = num % 100;
		System.out.println(num + " is " + num1 + " dollars and " + num2 + " cents.");
	}
}

2.23(驾驶费用)

import java.util.Scanner;

public class Test2_23 {
     
	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();
		input.close();
		double cost = distance / miles * price;
		System.out.println("The cost of driving is $" + cost);
	}
}

以上代码均为个人编写,不当之处,悉请指正。

你可能感兴趣的:(Java语言程序设计,基础篇,课后,编程题,第二章)