Java语言程序设计(基础篇)(原书第10版) 第二章编程练习题

Java语言程序设计(基础篇)(原书第10版) 第二章编程练习题

心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。

在这里插入图片描述

需要书籍或者相关资料可以私聊!!!

2-1 将摄氏温度转换为华氏温度

import java.util.Scanner;

public class Program2_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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.0;
		
		System.out.println(C + " Celsius is " + F + " Fahrenheit.");
		
	}

}

2-2 计算圆柱体的体积

import java.util.Scanner;

public class Program2_2 {

   public static void main(String[] args) {
   	// TODO Auto-generated method stub
   	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);
   	
   }

}

2-3 将英尺转换为米

import java.util.Scanner;

public class Program2_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a value for feet:");
		double feet = input.nextDouble();
		
		double meter = feet * 0.305;
		
		System.out.println(feet + " feet is " + meter + " meters.");
		
		
	}

}

2-4 将磅转换为千克

import java.util.Scanner;

public class Program2_4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a number in pounds:");
		
		double pound = input.nextDouble();
		double kilograms = pound * 0.454;
		
		System.out.println(pound + " pounds is " + kilograms + " kilograms");
		
	}

}

2-5 财务应用程序:计算小费

import java.util.Scanner;

public class Program2_5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the subtotal and a gratuity rate:");
		
		double subtotal = input.nextDouble();
		double gratuity_rate = input.nextDouble();
		
		double gratuity = subtotal * gratuity_rate / 100;
		double total = subtotal + gratuity;
		
		System.out.println("The gratuity is $" + gratuity + " and total is $" + total);
		
	}

}

2-6 求一个整数各位数的和

import java.util.Scanner;

public class Program2_6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a number between 0 and 1000:");
		
		int number = input.nextInt();
		
		int sum = number/100 + number % 10 + (number / 10) % 10;
		
		System.out.println("The sum of the digit is " + sum);
		
	}

}

2-7 求出年数

import java.util.Scanner;

public class Program2_7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the number of minutes:");
		int minutes = input.nextInt();
		
		int years = minutes / 60 / 24 / 365;
		int days = minutes / 60 / 24 % 365;
		
		System.out.println(minutes + " minutes is approximately " + years + " years and " + days + " days");
		
	}

}

2-8 当前时间

import java.util.Scanner;

public class Program2_8 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the time zone offset to GMT:");
		
		int change = input.nextInt();
		
		long totalMilliseconds = System.currentTimeMillis();
		long totalSeconds = totalMilliseconds / 1000;
		long currentSeconds = totalSeconds % 60;
		long totalMinutes = totalSeconds / 60;
		long currentMinutes = totalMinutes % 60;
		long totalHours = totalMinutes / 60;
		long currentHours = totalHours % 24;
		
		long Hours = currentHours + change;
		
		System.out.println("The current time is " + Hours + ":" + currentMinutes + ":" + currentSeconds);
	}

}

2-9 物理:加速度

import java.util.Scanner;

public class Program2_9 {

    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 acceleration is " + a);
    }

}

2-10 科学:计算能量

import java.util.Scanner;

public class Program2_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 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);
    }

}

2-11 人口统计

import java.util.Scanner;

public class Program2_11 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of years: ");
        int year = input.nextInt();
        double add = 365*24*60*60/7.0 - 365*24*60*60/13.0 + 365*24*60*60/45.0;

        System.out.printf("The population in %d years is %.0f\n", year, (312032486 + add * year));
    }

}

2-12 物理:求出跑到长度

import java.util.Scanner;

public class Program2_12 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter speed and acceleration:");
		double v = input.nextDouble();
		double a = input.nextDouble();
		
		double length = v * v / 2 / a;
		
		System.out.println("The minimum runway length for this airplane is " + length);
	}

}

2-13 财务应用程序:复利值

import java.util.Scanner;

public class Program2_13 {

	public static void main(String[] args) {
		// TODO Auto-generated	 method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter the monthly saving amount: ");
		
		double amount = input.nextDouble();
		double account;
		account = amount * (1 + (0.05 / 12));
		for(int i = 2; i <= 6; i++)
			account = (amount + account) * (1 + (0.05 / 12));
		
		System.out.printf("After the sixth month, the account value is $%.2f", account);
	}

}

2-14 医疗应用程序:计算BMI

import java.util.Scanner;

public class Program2_14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter weight in pounds:");
		double pounds = input.nextDouble();
		
		System.out.print("Enter height in inches:");
		double height = input.nextDouble();
		
		double BMI = (pounds * 0.45359237) / (height * 0.0254) / (height * 0.0254);
		
		System.out.println("Bmi is " + BMI);
	}

}

2-15 几何:两点间距离

import java.util.Scanner;

public class Program2_15 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
		
		System.out.println("The distance between the two points is " + distance);
	}

}

2-16 几何:六边形面积

import java.util.Scanner;

public class Program2_16 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the side: ");
        double s = input.nextDouble();

        double area = 3 * Math.sqrt(3) * s * s / 2;

        System.out.println("The area of the hexagon is " + area);
    }

}

2-17 科学:风寒温度

import java.util.Scanner;

public class Program2_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 ta = input.nextDouble();

        System.out.print("Enter the wind speed (>=2) in miles per hour: ");
        double v = input.nextDouble();

        double twc = 35.74 + 0.6215*ta - 35.75 * Math.pow(v, 0.16) + 0.4275 * ta * Math.pow(v, 0.16);

        System.out.println("The wind chill index is " + twc);
    }

}

2-18 打印表格

public class Program2_18 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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 Program2_19 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter thre  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();
		
		double edge1 = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
		double edge2 = Math.pow((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3), 0.5);
		double edge3 = Math.pow((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1), 0.5);
		
		double s = (edge1 + edge2 + edge3) / 2;
		
		double area = Math.pow(s * (s - edge1) * (s - edge2) * (s - edge3),0.5);
		
		System.out.println("The area of the triangle is " + area);
	}

}

2-20 财务应用程序:计算利息

import java.util.Scanner;

public class Program2_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();

        double interest = balance * (rate / 1200);

        System.out.printf("The interest is %.5f", interest);
    }

}

2-21 财务应用:计算未来投资值

import java.util.Scanner;

public class Program2_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();

        double accumulated = amount * Math.pow(1+rate/1200.0, years*12);
        System.out.printf("Accumulated value is $%.2f", accumulated);
    }

}

2-22 财务应用:货币单位

import java.util.Scanner;

public class Program2_22 {
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Receive the amount
        System.out.print(
                "Enter an amount in int, for example 1156 pennies: ");

        int amount = input.nextInt();

        int remainingAmount = amount;

        // Find the number of one dollars
        int numberOfOneDollars = remainingAmount / 100;
        remainingAmount = remainingAmount % 100;

        // Find the number of quarters in the remaining amount
        int numberOfQuarters = remainingAmount / 25;
        remainingAmount = remainingAmount % 25;

        // Find the number of dimes in the remaining amount
        int numberOfDimes = remainingAmount / 10;
        remainingAmount = remainingAmount % 10;

        // Find the number of nickels in the remaining amount
        int numberOfNickels = remainingAmount / 5;
        remainingAmount = remainingAmount % 5;

        // Find the number of pennies in the remaining amount
        int numberOfPennies = remainingAmount;

        // Display results
        System.out.println("Your 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");
    }
}

2-23 驾驶费用

import java.util.Scanner;

public class Program2_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 mile = input.nextDouble();

        System.out.print("Enter price per gallon: ");
        double price = input.nextDouble();

        double cost = distance / mile * price;
        System.out.printf("The cost of driving is $%.2f\n", cost);
    }

}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值