java語言程式設計基礎篇第二章程式設計練習題

``````1：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double degreeCelsius = input.nextDouble();
double fahrenheit = degreeCelsius*9.0/5.0   32;
System.out.print(fahrenheit);
}
}
2：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double height = input.nextDouble();
double volume = S*height;
System.out.print("area:"   S   " "   "volume:"   volume);
}
}
3：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double foot = input.nextDouble();
double meter = foot * 0.305;
System.out.println(meter);
}
}
4：

5：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double expenditure = input.nextDouble();
double reward = expenditure * 0.15;
double totalMoney = expenditure   reward;
System.out.println(totalMoney);
}
}
6：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 0 and 1000:");
int num = input.nextInt();
int sum = 0;
while(num > 0){
sum  = num%10;
num /= 10;
}
System.out.println(sum);
}
}
7：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
int days = minutes / (24*60);
int year = days / 365;
int day = days % 365;
System.out.println(year   " years and "   day   " days");
}
}
8：
//地理不好，說不定就做錯了
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int move = input.nextInt();
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
currentHour  = move;
if(currentHour < 0)
currentHour  = 24;
System.out.println("Current time is "   currentHour   ":"   currentMinute   ":"   currentSecond   "GMT");
}
}
9：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double v1,v0,t;
System.out.print("Enter v0,v1,and t:");
v0 = cin.nextDouble();
v1 = cin.nextDouble();
t = cin.nextDouble();
System.out.print("The average acceleration is ");
System.out.printf("%.4f\n",(v1-v0) / t);
}
}
10：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double k,t1,t2;
System.out.print("Enter the amount of water in kilograms:");
k = cin.nextDouble();
System.out.print("Enter the initial temperature:");
t1 = cin.nextDouble();
System.out.print("Enter the final temperature:");
t2 = cin.nextDouble();
System.out.print("The energy needed is "   (k*(t2-t1)*4184));
}
}
11：

12：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double a,v;
System.out.print("Enter speed and accelerate:");
v = cin.nextDouble();
a = cin.nextDouble();
System.out.println("The minimum runway length for this airplane is "   (v*v)/(2*a));
}
}
13：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double annualInterest = 0.05;
double monthlyInterest = annualInterest / 12;
double money = 100;
double sum = 0;
for(int i = 0; i < 6;   i){
sum  = 100;
sum = sum*(1 monthlyInterest);
}
System.out.println(sum);
}
}
14：
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double pounds = input.nextDouble();
double inches = input.nextDouble();
double weight = pounds * 0.45359237;
double height = inches * 0.0254;
double BMI = weight / Math.pow(height, 2.0);
System.out.println("BMI is:"   BMI);
}
}
15：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double x1,y1,x2,y2;
System.out.print("Enter x1 and y1:");
x1 = cin.nextDouble();
y1 = cin.nextDouble();
System.out.print("Enter x2 and y2:");
x2 = cin.nextDouble();
y2 = cin.nextDouble();
System.out.println("The distance between the two points is "   Math.sqrt((x2-x1)*(x2-x1)   (y2-y1)*(y2-y1)));
}
}
16：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double side;
System.out.print("Enter the side:");
side = cin.nextDouble();
System.out.println("The area of the hexagon is "   (Math.sqrt(3)*3/2)*side*side);
}
}
17：
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double t = input.nextDouble();
double v = input.nextDouble();
double rt = 35.74 0.6215*t-35.75*Math.pow(v, 0.16) 0.4275*t*Math.pow(v, 0.16);
System.out.println(rt);
}
}
18：
import java.util.Scanner;
public class Main{
public static void main(String[] args){
System.out.println("a    b    pow(a,b)");
for(int i = 1; i <= 5;   i){
int a = i;
int b = i 1;
int c = (int)Math.pow(a, b);
System.out.println(a   "    "   b   "    "   c);
}
}
}
19：

20：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double balance,interestRate;
System.out.print("Enter the balance and interest rate(e.g.,3 for 3%):");
balance = cin.nextDouble();
interestRate = cin.nextDouble();
System.out.println("The interest is:"   balance*(interestRate/1200));
}
}
21：
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
double investment,interestRate,year;
System.out.print("Enter investment amount:");
investment = cin.nextDouble();
System.out.print("Enter annual interest rate in percentage:");
interestRate = cin.nextDouble();
System.out.print("Enter the number of years:");
year = cin.nextDouble();
System.out.println("Accumulated value is \$"   investment*Math.pow((1 (interestRate/100)/12), year*12));
}
}
22：
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double , for example 11.56: ");
double amount = input.nextDouble();
int remainingAmount = (int)((amount 0.005)*100);
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int numberOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
int numberOfPennies = remainingAmount;
System.out.println("Yours amount "   amount   " consists of");
System.out.println("    "   numberOfOneDollars   " dollars");
System.out.println("    "   numberOfQuarters   " quarters");
System.out.println("    "   numberOfDimes   " dimes");
System.out.println("    "   numberOfNickels   " nickels");
System.out.println("    "   numberOfPennies   " pennies");
}
}
23：
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double distance = input.nextDouble();
double unit = input.nextDouble();
double price = input.nextDouble();
double cost = (distance/unit)*price;
System.out.println(cost);
}
}``````