天天看點

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);
	}
}
           

以上代碼均為個人編寫,不當之處,悉請指正。

繼續閱讀