admin 管理员组

文章数量: 887021


2023年12月17日发(作者:mysql安装时check失败)

java语言程序设计(基础篇)第二章程序练习题答案

2.1(将摄氏温度转化为华氏温度)

import .*;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter a degree in celsius: ");

double Celsius = uble();

double Fahrenheit;

Fahrenheit = (9.0/5) * Celsius + 32;

n(Celsius + " Celsius is" + Fahrenheit + "

Fahrenheit");

2.2(计算圆柱体的体积)

import .*;

public class test {

2

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the radius and length of a cylinder: ");

double radius = uble();

double length =uble();

double area = radius * radius * ;

double volume = area * length;

n("The area is " + area);

n("The volume is " + volume);

}

2.3(将英尺转换为米)

import r;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter a value for feet: ");

double feet = uble();

double meters = feet * 0.305;

n(feet+ " feet is " + meters + " meters");

2.4(将磅转换为千克)

import r;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter a number in pounds: ");

double pounds = uble();

double kilograms = pounds * 0.454;

n(pounds + " pounds is " + kilograms + " kilograms");

3

2.5(财务应用程序:计算消费)

import r;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the subtotal and gratuity rate: ");

double subtotal = uble();

double Gratuity = uble();

double gratuity = subtotal * Gratuity * 0.01;

double total = gratuity + subtotal;

n("The gratuity is $" + gratuity + " and total is "

+ total);

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

import r;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter a number between 0 and 1000: ");

int number = t();

int sum = number % 10 + (number / 10) % 10 + (number / 100) % 10;

n("The sum of the digits is " + sum);

4

2.7(求出年数)

import r;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the number of minutes: ");

double minutes = uble();

int years = (int)minutes / (60*24*365);

int days = (int)minutes / (60*24) - ((int)minutes / (60*24*365)) * 365;

n(minutes + "minutes is approximately " + years + "

years and " + days + " days");

2.8(当前时间)

import .*;

public class ShowXureentTime {

public static void main(String[] args) {

// TODO Auto-generated method stub

long totalMilliseconds = tTimeMillis();//得到1970年1月1

long totalSeconds = totalMilliseconds / 1000;//将总毫秒转化为总秒

long currentSecond = totalSeconds % 60;//当前的秒数

long totalMinutes = totalSeconds / 60;//得到总的分钟

long currentMinute = totalMinutes % 60;//当前的分钟数

long totalHours = totalMinutes / 60;//得到总小时

5

日到现在的毫秒数

Scanner input = new Scanner();

("Enter the time zone offset to GMT: ");

long zone = t();

long currentHour = totalHours % 24 + zone;//当前的小时数

if(currentHour <0) {

}

n("Current time is " + currentHour + ":" +

currentHour = currentHour + 24;

currentMinute + ":" + currentSecond + " GMT");

}

//GMT世界时

}

2.9(物理:加速度)

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter v0, v1 and t: ");

double v0 = uble();

double v1 = uble();

double t = uble();

double acceleration = (v1 - v0) / t;

n("The average acceleration is " +

("%.4f", acceleration));

}

}

2.10(科学:计算能量)

import .*;

6

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the amount of water in kilograms: ");

double M = uble();

("Enter the initial temperature: ");

double temperature1 = uble();

("Enter the final temperature: ");

double temperature2 = uble();

double energy = M * (temperature2 - temperature1) * 4184;

n("The energy needed is " + energy);

2.1(人口统计)

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the number of years: ");

int year = t();

int population = 312032486;

int temp = (365*12*60*60/7)-(365*12*60*60/13)+(365*12*60*60/45);//j =

for(int i = 1 ;i <= year ;i ++ ) {

}

n("The population in " + year + " years is " +

7

诞生-死亡+移民迁入;

population = temp + population ;

population);

}

}

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

import .*;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter speed and acceleration: ");

double speed = uble();

double acceleration = uble();

double length = speed * speed / (2 * acceleration);

n("The minimum runway length for this airplane is "

+ ("%.3f", length));

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

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the monthly saving aomunt: ");

double monthly = uble();

double money = 0;

for(int i = 1; i <= 6; i++) {

}

n("After the sixth month, the account value is " +

8

money = (monthly + money) * (1 + 0.00417);

("%.2f", money));

}

}

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

import .*;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter weight in pounds: ");

double pounds = uble();

double kilogram = pounds * 0.45359237;

("Enter height in inches: ");

double inches = uble();

double meter = inches * 0.0254;

double BMI = (kilogram / (meter * meter)) ;

n("BMI is " + ("%.4f", BMI));

2.15(几何:两点间距离)

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter x1 and y1: ");

double x1 = uble();

double y1 = uble();

("Enter x2 and y2: ");

double x2 = uble();

9

}

}

double y2 = uble();

double distance = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);

n("The distance between the two points is " +

(distance, 0.5));

2.16(几何:六边形面积)

import .*;

public class test {

}

public static void main(String[] args) {

}

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the side: ");

double side = uble();

double area = 3 * (3) / 2 * side * side;

n("The area of the hexagon is " + area);

2.17(科学:风寒温度)

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter the temperature in Fahrenheit between -58°F and

double Fahrenheit = uble();

10

41°F: ");

("Enter the wind speed (>=2) in miles per hour: ");

double speed = uble();

double t = 35.74 + 0.6215 * Fahrenheit - 35.75 * (speed, 0.16)

n("The win chill index is " + ("%.5f",

+ 0.4275 * Fahrenheit * (speed, 0.16);

t));

}

}

2.18(打印表格)

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

n("at" + "bt" + "pow(a, b)");

n("1t" + "2t" + (int)(1, 2));

n("2t" + "3t" + (int)(2, 3));

n("3t" + "4t" + (int)(3, 4));

n("4t" + "5t" + (int)(4, 5));

n("5t" + "6t" + (int)(5, 6));

}

}

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

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter three points fir a trinangle: ");

double x1 = uble();

double y1 = uble();

11

}

}

double x2 = uble();

double y2 = uble();

double x3 = uble();

double y3 = uble();

double edge1= ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1),

double edge2= ((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2),

double edge3= ((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1),

double s =(edge1 + edge2 + edge3) / 2;

double area = (s * (s - edge1) * (s - edge2) * (s - edge3), 0.5);

n("The area of the tringle is " + ("%.1f",

0.5);

0.5);

0.5);

area));

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

import .*;

public class test {

}

}

double interst = balance * ( annual / 1200);

n("The interst is " + ("%.5f", interst));

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter balance and interst rate (e.g, 3 for 3%): ");

double balance = uble();

double annual = uble();

12

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

import .*;

public class test {

}

}

("Enternumber of yuears: ");

double years = uble();

double value = investment * ((1 + interest), (years * 12));

n("Accumulated value is $" + ("%.2f",

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter investment amount: ");

double investment = uble();

("Enter annual interest rate in percentage: ");

double interest = uble();

interest = interest / 100 / 12;

value));

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

import .*;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner();

("Enter an amount in int, for example 1156: ");

int amount = t();

int remainingAmount = amount;

int numberOfOneDollars = remainingAmount / 100;13

14

15


本文标签: 计算 财务 应用程序 安装 面积