1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答

Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答

时间:2022-04-17 05:21:16

相关推荐

Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答

//编程练习题2.1~2.25/*** 2.1 程序要求:编写程序,从控制台读入double型的摄氏温度,然后将其转换为华氏温度,并且显示结果。* 转换公式如下所示:* fahrenheit = (9/5) * celsius +32 (华氏度= (9/5) * 摄氏度+32)* @作者:wwj* 日期:/5/6* 功能:将摄氏温度转换为华氏温度* * 运行结果:* Enter a degree in Celsius: 43* 43.0 Celsius is 109.4 Fahrenheit*/import java.util.Scanner;public class Exercise2_1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);//输入一个摄氏温度System.out.println("Enter a degree in Celsius: ");double celsius = input.nextDouble();//转换为华氏温度double fahrenheit = (9.0/5) * celsius + 32;//输出结果System.out.println(celsius + " Celsius is " + fahrenheit + " Fahrenwheit");}}//Exersise2_2.java/*** 程序要求:读入圆柱体的半径和高,并使用下列公式计算圆柱的体积:* 面积=半径 x 半径 x π* 体积= 面积 x 高* 作者:wwj* 功能:计算圆柱体的体积* * 运行结果:* Enter the radius and length of a cylinder: 5.5 12* The area is 95.0331* The volume is 1140.4*/import java.util.Scanner;public class Exercise2_2 {public static void main(String[] args) {final double PI = 3.14159;Scanner input = new Scanner(System.in);//输入半径和高System.out.println("Enter the radius and length of a cylinder: ");double radius = input.nextDouble();double height = input.nextDouble();//计算面积和体积double area = radius * radius * PI;double volume = area * height;//输出结果System.out.println("The area is " + area);System.out.println("The volume is " + volume);}}//Exercise2_3.java/*** * 程序要求:编写程序,读入英尺数,将其转换为米数并显示结果。一英尺等于0.305米。* * 作者:wwj* 日期:/5/8* * 运行结果示例:* Enter a value for feet: 16* 16 feet is 4.88 meters*/import java.util.Scanner;public class Exercise2_3 {public static void main(String[] args) {final double ONE_FEET = 0.305;Scanner input = new Scanner(System.in);//读入英尺数System.out.println("Enter a value for feet:");double feet = input.nextDouble();//转换结果double meter = feet * ONE_FEET;//输出System.out.println(feet + " feet is "+ meter + " meters ");}}//Exercise2_4.java/*** * 程序要求:编写程序,将磅数转换为千克数。程序提示用户输入磅数,然后转换车成千克并显示结果。* 一磅=0.454千克。* * 作者:wwj* 日期:/5/8* * 运行结果示例:* Enter a number in pounds: 55.5* 55.5 pounds is 25.197 kilograms* */import java.util.Scanner;public class Exercise2_4 {public static void main(String[]args){Scanner input = new Scanner(System.in);final double ONE_POUND = 0.454;//提示输入磅数System.out.println("Enter a number in pounds: ");double pounds = input.nextDouble();//将磅数转换为千克数double kilograms = pounds * ONE_POUND;//输出结果System.out.println(pounds + " pounds is " + kilograms + " kiligrams");}}//Exercise2_5.java/*** * 程序要求:读入一笔费用与酬金率,计算酬金和总钱数。例如,如果用户输入10作为费用,15%作为酬金率,* 计算结果显示酬金$1.5,总费用$11.5.* * 作者:wwj* 日期:/5/8* 功能:计算小费* * 运行结果示例:* Enter the subtotal and a gratuity rate: 15.69 15* The gratuity is 2.35 and total is 18.04* */import java.util.Scanner;public class Exercise2_5 {public static void main(String[] args) {Scanner input = new Scanner(System.in);//读入一笔费用和酬金率System.out.println("Enter the subtotal and a gratuity rate: ");double subtotal = input.nextDouble();double rate = input.nextDouble() * 0.01;//计算酬金和总费用double gratuity = subtotal * rate;double totalMoney = subtotal + gratuity;//输出结果System.out.println("The gratuity is " + gratuity + " and total is "+ totalMoney);}}//Exercise2_6.java/*** * 程序要求:读取一个在0到1000之间的整数,并将该整数的各位数字相加。* 例如:整数是932,各位数字之和为14。* * 作者:wwj* 日期:/5/8* 功能:求一个整数各位数之和* * 运行结果示例:* Enter a number between 0 and 1000:999* The sum of the digits is 27*/import java.util.Scanner;public class Exercise2_6 {public static void main(String[] args){Scanner input = new Scanner(System.in);int sum;//输入一个数字System.out.println("Enter a number between 0 and 1000: " );int num = input.nextInt();//分解数字 if(num <= 99){int num1 = num % 10;int num2 = num / 10;sum = num1 + num2;}else {int num1 = num % 10;//个位数int num2 = num % 100 / 10;//十位数int num3 = num / 100;//百位数sum = num1 + num2 + num3;}//输出结果System.out.println("The sum of the digits is " + sum);}}//Exercise2_7.java/*** * 程序要求:编写程序,提示用户输入分钟数(例如十亿)然后显示这些分钟数代表多少年和多少天* 为了简化问题,假设一年又365天。* * 作者:wwj* 日期:/5/8* 功能:求出年数* * 运行结果示例:* Enter the number of minutes: 1000000000* 1000000000 minutes is approximately 1902 years and 214 days.*/import java.util.Scanner;public class Exercise2_7 {public static void main(String[]args){Scanner input = new Scanner(System.in);final int ONEYEAR_OF_MINUTES = 365 * 60 * 24;final int ONEDAY_OF_MINUTES = 60 * 24;//输入分钟数System.out.println("Enter the number of minutes:");int minutes = input.nextInt();//计算年数和天数int numOfYears = minutes / ONEYEAR_OF_MINUTES;int numOfDays = (minutes % ONEYEAR_OF_MINUTES) / ONEDAY_OF_MINUTES;//输出年数和天数System.out.println(minutes + " minutes is approximately " + numOfYears + " years "+ numOfDays + " days");}}//Exercise2_8.java/*** * 程序要求:编写程序接收一个ASCII码(从0到128的整数),然后显示它代表的字符。* 例如,如果用户输入的是97,程序显示的是字符a。* * 作者:wwj* 日期:/5/8* 功能:求ASCII码对应的字符* * 运行结果示例:* Enter an ASCII code:69* The character for ASCII code 69 is E*/import java.util.Scanner;public class Exercise2_8 {public static void main(String[]args){Scanner input = new Scanner(System.in);//输入ASCII码System.out.println("Enter an ASCII code:");int ascii_code = input.nextInt();//转换为对应字符char character = (char)ascii_code;System.out.println("The character for ASCII code " + ascii_code + " is " + character);}}//Exercise2_9.java/*** * 程序要求:改写程序清单2-10,解决将double型值为int型值时可能会造成精度损失的问题。* 输入的输入值是一个整数,其最后两位代表是分币值。例如:1156就表示的是11美元56美分* * 作者:wwj* 日期:/5/8* 功能:整钱兑零*/import java.util.Scanner;public class Exercise2_9 {public static void main(String[] args){//创建一个Scanner对象Scanner input = new Scanner(System.in);//输入总钱数System.out.print("Enter an amount,for example 1156: ");int amount = input.nextInt();//找出1美元的个数int numberOfOneDollars = amount / 100;amount = amount % 100;//找出2角5分币的个数int numberOfQuarters = amount / 25;amount = amount % 25;//找出1角币的个数int numberOfDimes = amount / 10;amount = amount % 10;//找出5分币的个数int numberOfNickles = amount /5;amount = amount % 5;//找出1分币的个数int numberOfPennies = amount;//输出结果System.out.println("Your amount " + amount + " consist of \n" +"\t" + numberOfOneDollars + " dollars\n" +"\t" + numberOfQuarters + " quarters\n" + "\t" + numberOfDimes + " dimes\n" +"\t" + numberOfNickles + " nickles\n" +"\t" + numberOfPennies + " pennies");}}//Exercise2_10.java/*** * 程序要求:使用图形用户界面输入* 改写程序清单2-10,使用图形用户界面输出* * 作者:wwj* 日期:/5/8* 功能:整钱兑换* */import javax.swing.JOptionPane;public class Exercise2_10 {public static void main(String[] args){//输入总钱数String amountString = JOptionPane.showInputDialog("Enter an amount for example 1156:");int amount = Integer.parseInt(amountString);int numberOfOneDollars = amount / 100;//美元int numberOfPennies = amount % 100;//美分String output = "Your amount " + amount + " consist of \n" + "\t" + numberOfOneDollars + " dollars\n" +"\t" + numberOfPennies + " pennies";//输出兑零后的结果JOptionPane.showMessageDialog(null, output);}}import javax.swing.JOptionPane;//Exercise2_11.java/*** 2.11 程序要求:编写程序,读入下列信息并打印工资单* 雇员的名字(例如Smith)* 每周的工作小时数(例如10)* 每小时工资(例如6.75)* 联邦所得税税率(例如20%)* 州所得税税率(例如9%)* 作者:wwj* 日期:/5/17* 功能:计算工资单*/public class Exercise2_11 {public static void main(String[] args){//输入对话框:雇员的名字String empName = JOptionPane.showInputDialog("Enter employee's name,for example Smith");//输入对话框:每周的工作小时数String empHoursString = JOptionPane.showInputDialog("Enter number of hours worked in a week:");//将string类型的工作小时数转换为double类型double empHours = Double.parseDouble(empHoursString);//输入对话框:每小时工资String empHourSalaryString = JOptionPane.showInputDialog("Enter hourly pay rate:");//将string类型的工资转换为double类型double empHourSalary = Double.parseDouble(empHourSalaryString);//输入对话框:联邦所得税税率String fedaralRateString = JOptionPane.showInputDialog("Enter federal tax withholding rate:");//将string类型税率转换为double类型double federalRate = Double.parseDouble(fedaralRateString);//输入对话框:州所得税税率String stateRateString = JOptionPane.showInputDialog("Enter state tax withholding rate:");double stateRate = Double.parseDouble(stateRateString);double grossPay = empHourSalary * empHours;double federalRatePay = grossPay * federalRate;double stateRatePay = grossPay * stateRate;double totalDeduction = federalRatePay + stateRatePay;//打印结果String output = "Employee Name: " + empName +"\nHour Worked: " + empHours + "\nPay Rate: " + empHourSalary + "\nGross Pay: " + grossPay +"\nDeductions:\n" + " Federal Withholding(" + federalRate + "):"+ federalRatePay+ "\n State Withholding(" + stateRate + "):"+ stateRatePay +"\n Total Deduction" + totalDeduction + "";JOptionPane.showMessageDialog(null, output);}}import java.util.Scanner;/*** 2.12 程序要求:如果你知道收支余额和年利率的百分比,你就可以是使用下面的公式计算下个月要支付的利息额:* 利息额=收支余额 x (年利率/1200)* @author wwj**/public class Exercise2_12 {public static void main(String[] args){Scanner input = new Scanner(System.in);//请输入收支余额和年利率System.out.println("Enter balance and interest rate(e.g., 3 for 3%):" );double balance = input.nextDouble();double interestRate = input.nextDouble();//计算下个月要支付的利息额double interest = balance * (interestRate / 1200);//输出结果System.out.println("The interest is:" + interest);}}import java.util.Scanner;/*** 2.13 程序要求:编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来的投资金额:* futureInvestmentValue = investmentAmount * (1 + annuallyInterestRate)^(numberOfYears*12)* @author wwj**/public class Exercise2_13 {public static void main(String[] args){Scanner input = new Scanner(System.in);//读取投资总额System.out.print("Enter investment amount:");int investment = input.nextInt();//读取年利率System.out.print("Enter monthly interest rate:");double rate = input.nextDouble();//读取年数System.out.print("Enter number of years:");int years = input.nextInt();//计算未来的投资金额double futureInvestmentValue = investment * Math.pow(1 + rate, years * 12);//显示结果System.out.println("Accumulated value is:" + futureInvestmentValue);}import java.util.Scanner;/*** 2.13 程序要求:编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来的投资金额:* futureInvestmentValue = investmentAmount * (1 + annuallyInterestRate)^(numberOfYears*12)* @author wwj**/public class Exercise2_13 {public static void main(String[] args){Scanner input = new Scanner(System.in);//读取投资总额System.out.print("Enter investment amount:");int investment = input.nextInt();//读取年利率System.out.print("Enter monthly interest rate:");double rate = input.nextDouble();//读取年数System.out.print("Enter number of years:");int years = input.nextInt();//计算未来的投资金额double futureInvestmentValue = investment * Math.pow(1 + rate, years * 12);//显示结果System.out.println("Accumulated value is:" + futureInvestmentValue);}import java.util.Scanner;/*** 2.14 程序要求:计算BMI(身体质量指数),它的值可以通过将体重(以公斤为单位)除以身高(以米为单位)* 的平方值得到。编写程序,提示用户输入体重(以磅为单位)以及身高(以米为单位),然后显示BMI* @author Administrator**/public class Exercise2_14 {public static void main(String[] args){Scanner input = new Scanner(System.in);//输入体重System.out.print("Enter weight in pounds :");double weight = input.nextDouble() * 0.45359237;//输入身高System.out.print("Enter height in inches :");double height = input.nextInt() * 0.0254;//计算BMIdouble BMI = weight / Math.pow(height, 2);//显示BMISystem.out.println("BMI is:" + BMI);}}import java.util.Scanner;/*** 2.15 程序要求:假设你每月向银行账户存100美元,年利率为5%,那么每月利率是0.05/12=0.00417.* 第一个月之后,账户上的值就变成:100 * (1 + 0.00417) = 100.417* 第二个月之后,账户上的值就变成:(100 + 100.417) * (1+ 0.00417) = 201.252* 第三个月之后,账户上的值就变成:(100 + 201.252) * (1+ 0.00417) = 302.507* 依此类推。* 编写程序显示六个月之后账户上的钱数。* @author wwj**/public class Exercise2_15 {public static void main(String[] args){Scanner input = new Scanner(System.in);int annuallyMonthDollar = 100;double interestRate = 0.05 / 12.0;//1~6月份账户的值double firstMonthAcount = annuallyMonthDollar * (1 + interestRate);double secondMonthAcount = (annuallyMonthDollar + firstMonthAcount) * (1 + interestRate);double thirdMonthAcount = (annuallyMonthDollar + secondMonthAcount) * (1 + interestRate);double forthMonthAcount = (annuallyMonthDollar + thirdMonthAcount) * (1 + interestRate);double fifthMonthAcount = (annuallyMonthDollar + forthMonthAcount) * (1 + interestRate);double sixthMonthAcount = (annuallyMonthDollar + fifthMonthAcount) * (1 + interestRate);//显示六月后账户的值System.out.println("sixthMonthAcount is " + sixthMonthAcount);}}import java.util.Scanner;/*** 2.16 程序要求:编写程序,计算将水从初始温度加热到最终温度所需的能量。* 计算能量的公式是:* Q = M * (最终温度 - 初始温度) * 4184* 这里的M是千克为单位的水的重量,温度以摄氏度为单位,而能量Q以焦耳为单位。* @author wwj**/public class Exercise2_16 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner 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 initialTemp = input.nextDouble();//输入最终温度System.out.print("Enter the final temperature:");double finalTemp = input.nextDouble();double Q = M * (finalTemp - initialTemp) * 4184;System.out.println("The energy needed is :" + Q);}}import java.util.Scanner;/*** 2.17 程序要求:计算风寒温度,计算公式如下:* t=35.74 + 0.6215 * t' - 35.75 * v^0.16 + 0.4275 * t' * v^0.16* 这里的t’是室外温度,以华摄氏度为单位,而v是速度,以每小时英里数为单位。t是风寒温度。* 该公式不适用于风速低于2mph或温度在-58F以下或41F以上的情况。* @author wwj**/public class Exercise2_17 {public static void main(String[] args){Scanner input = new Scanner(System.in);//输入室外的温度System.out.print("Enter the temperature in Fahrenheit: ");double temp = input.nextDouble();//输入风速System.out.print("Enter the wind speed miles per hours: ");double wSpeed = input.nextDouble();//计算风寒温度double t = 35.74 + 0.6215 * temp - 35.75 * Math.pow(wSpeed, 0.16) + 0.4275 * temp * Math.pow(wSpeed, 0.16);//显示结果System.out.println("The wind chill index is " + t);}}/*** 2.18 程序要求:打印表格,显示下面的表格:* abpow(a,b)* 121* 238* 3481* 451024* 5615625* @author wwj**/public class Exercise2_18 {public static void main(String[] args){int a,b;int n = 5;System.out.println("abpow(a,b)");for (int i = 1; i <= n; i++){a = i;b = i + 1;System.out.println(a + "" + b + "" + Math.pow(a,b));}}}/*** 2.19 编写程序,使用System.CurrentTimeMillis()显示任意一个大写字母* @author wwj**/public class Exercise2_19 {public static void main(String[] args){long time = System.currentTimeMillis();int num = (int)(time % 25) + 65;System.out.println((char)num);}}import java.util.Scanner;/*** 2.20 程序要求:编写程序,提示用户输入两个点(x1,y1)和(x2,y2)然后显示两点间的距离。* 计算两点间距离的公式是sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y2,2)).* @author Administrator**/public class Exercise2_20 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);//输入第一个点System.out.println("Enter x1 and y1:");double x1,y1;x1 = input.nextDouble();y1 = input.nextDouble();//输入第二个点System.out.println("Enter x2 and y2");double x2,y2;x2 = input.nextDouble();y2 = input.nextDouble();//计算两点间距离double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));//显示结果System.out.println("The distance of the two points is:" + distance);}}import java.util.Scanner;/*** 2.21 程序要求:编写程序,提示用户输入三角形的三个点(x1,y1)、(x2,y2)、(x3,y3)* 然后显示它的面积。计算三角形面积的公式是:* s = (side1 + side2 + side3)/2* area= sqrt(s(s-side1)(s-side2)(s-side3))* @author Administrator**/public class Enxecise2_21 {public static void main(String[] args){Scanner input = new Scanner(System.in);//输入三个点System.out.print("Enter three points for a triangle:");double x1,x2,x3,y1,y2,y3;x1 = input.nextDouble();y1 = input.nextDouble();x2 = input.nextDouble();y2 = input.nextDouble();x3 = input.nextDouble();y3 = input.nextDouble();//计算三角形面积double side1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));double side2 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2));double side3 = Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3,2));double s = (side1 + side2 + side3) / 2;double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3) );//显示结果System.out.println("The area of the triangle is :" + area);}}import java.util.Scanner;/*** 2.22 程序要求:编写程序,提示用户输入六边形的边长,然后显示它的面积。* 计算六边形面积的公式:* area =( 3 * sqrt(3) * s^2 )/ 2* @author Administrator**/public class Exercise2_22 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);//输入六边形的边长System.out.print("Enter the side:" );double side = input.nextDouble();//计算面积double area =( 3 * Math.sqrt(3) * side * side)/2;//显示结果System.out.println("The area of the hexagon " + area);}}import java.util.Scanner;/*** 2.23 程序要求:平均加速度定义为速度的变化量除以这个变化所用的时间,如下式:* a = (v1 - v0) / t* @author wwj**/public class Exercise2_23 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner 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);}}import java.util.Scanner;/*** 2.24 程序要求:假设一个飞机的加速度是a而起飞速度是v,那么飞机起飞所需要的最短跑道长度* length = v^2 / 2*a* 编写程序,提示用户输入以米/秒为单位的速度v和以米/秒的平方为单位的加速度a,然后显示最短跑道长度* @author wwj**/public class Exercise2_24 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);//输入v和aSystem.out.print("Enter v and a:");double v = input.nextDouble();double a = input.nextDouble();//计算跑道长度double length = Math.pow(v,2)/( 2 * a);//显示结果System.out.println("The minimum runway length for this airplaneis :" + length);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。