资源描述
2.1(将摄氏温度转化为华氏温度)
import java.util.*;
public class test {
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 Celsius = input.nextDouble();
double Fahrenheit;
Fahrenheit = (9.0/5) * Celsius + 32;
System.out.println(Celsius + " Celsius is" + Fahrenheit + " Fahrenheit");
}
}
2.2(计算圆柱体体积)
import java.util.*;
public class test {
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 test {
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 meters = feet * 0.305;
System.out.println(feet+ " feet is " + meters + " meters");
}
}
2.4(将磅转换为千克)
import java.util.Scanner;
public class test {
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 pounds = input.nextDouble();
double kilograms = pounds * 0.454;
System.out.println(pounds + " pounds is " + kilograms + " kilograms");
}
}
2.5(财务应用程序:计算消费)
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the subtotal and gratuity rate: ");
double subtotal = input.nextDouble();
double Gratuity = input.nextDouble();
double gratuity = subtotal * Gratuity * 0.01;
double total = gratuity + subtotal;
System.out.println("The gratuity is $" + gratuity + " and total is " + total);
}
}
2.6(求一个整数个位数和)
import java.util.Scanner;
public class test {
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 % 10 + (number / 10) % 10 + (number / 100) % 10;
System.out.println("The sum of the digits is " + sum);
}
}
2.7(求出年数)
import java.util.Scanner;
public class test {
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: ");
double minutes = input.nextDouble();
int years = (int)minutes / (60*24*365);
int days = (int)minutes / (60*24) - ((int)minutes / (60*24*365)) * 365;
System.out.println(minutes + "minutes is approximately " + years + " years and " + days + " days");
}
}
2.8(目前时间)
import java.util.*;
public class ShowXureentTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
long totalMilliseconds = System.currentTimeMillis();//得到1970年1月1日到现在毫秒数
long totalSeconds = totalMilliseconds / 1000;//将总毫秒转化为总秒
long currentSecond = totalSeconds % 60;//目前秒数
long totalMinutes = totalSeconds / 60;//得到总分钟
long currentMinute = totalMinutes % 60;//目前分钟数
long totalHours = totalMinutes / 60;//得到总小时
Scanner input = new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT: ");
long zone = input.nextInt();
long currentHour = totalHours % 24 + zone;//目前小时数
if(currentHour <0) {
currentHour = currentHour + 24;
}
System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
}
//GMT世界时
}
2.9(物理:加速度)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 acceleration = (v1 - v0) / t;
System.out.println("The average acceleration is " + String.format("%.4f", acceleration));
}
}
2.10(科学:计算能量)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 temperature1 = input.nextDouble();
System.out.print("Enter the final temperature: ");
double temperature2 = input.nextDouble();
double energy = M * (temperature2 - temperature1) * 4184;
System.out.println("The energy needed is " + energy);
}
}
2.1(人口统计)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of years: ");
int year = input.nextInt();
int population = ;
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 ++ ) {
population = temp + population ;
}
System.out.println("The population in " + year + " years is " + population);
}
}
2.12(物理:求出跑道长度)
import java.util.*;
public class test {
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 speed = input.nextDouble();
double acceleration = input.nextDouble();
double length = speed * speed / (2 * acceleration);
System.out.println("The minimum runway length for this airplane is " + String.format("%.3f", length));
}
}
2.13(财务应用程序:复利值)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the monthly saving aomunt: ");
double monthly = input.nextDouble();
double money = 0;
for(int i = 1; i <= 6; i++) {
money = (monthly + money) * (1 + 0.00417);
}
System.out.println("After the sixth month, the account value is " + String.format("%.2f", money));
}
}
2.14(医疗应用程序:计算BMI)
import java.util.*;
public class test {
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();
double kilogram = pounds * 0.45359237;
System.out.print("Enter height in inches: ");
double inches = input.nextDouble();
double meter = inches * 0.0254;
double BMI = (kilogram / (meter * meter)) ;
System.out.println("BMI is " + String.format("%.4f", BMI));
}
}
2.15(几何:两点间距离)
import java.util.*;
public class test {
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 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
System.out.println("The distance between the two points is " + Math.pow(distance, 0.5));
}
}
2.16(几何:六边形面积)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the side: ");
double side = input.nextDouble();
double area = 3 * Math.sqrt(3) / 2 * side * side;
System.out.println("The area of the hexagon is " + area);
}
}
2.17(科学:风寒温度)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the temperature in Fahrenheit between -58°F and 41°F: ");
double Fahrenheit = input.nextDouble();
System.out.print("Enter the wind speed (>=2) in miles per hour: ");
double speed = input.nextDouble();
double t = 35.74 + 0.6215 * Fahrenheit - 35.75 * Math.pow(speed, 0.16) + 0.4275 * Fahrenheit * Math.pow(speed, 0.16);
System.out.println("The win chill index is " + String.format("%.5f", t));
}
}
2.18(打印表格)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("a\t" + "b\t" + "pow(a, b)");
System.out.println("1\t" + "2\t" + (int)Math.pow(1, 2));
System.out.println("2\t" + "3\t" + (int)Math.pow(2, 3));
System.out.println("3\t" + "4\t" + (int)Math.pow(3, 4));
System.out.println("4\t" + "5\t" + (int)Math.pow(4, 5));
System.out.println("5\t" + "6\t" + (int)Math.pow(5, 6));
}
}
2.19(几何:三角形面积)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter three points fir a trinangle: ");
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((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 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 tringle is " + String.format("%.1f", area));
}
}
2.20(财务应用程序:计算利息)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interst rate (e.g, 3 for 3%): ");
double balance = input.nextDouble();
double annual = input.nextDouble();
double interst = balance * ( annual / 1200);
System.out.println("The interst is " + String.format("%.5f", interst));
}
}
2.21(财务应用:计算未来投资值)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter investment amount: ");
double investment = input.nextDouble();
System.out.print("Enter annual interest rate in percentage: ");
double interest = input.nextDouble();
interest = interest / 100 / 12;
System.out.print("Enternumber of yuears: ");
double years = input.nextDouble();
double value = investment * Math.pow((1 + interest), (years * 12));
System.out.println("Accumulated value is $" + String.format("%.2f", value));
}
}
2.22(财务应用:货币单位)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in int, for example 1156: ");
int amount = input.nextInt();
int remainingAmount = amount;
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int numbersOfQuarters = 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("Your amount " + amount + " consosts of");
System.out.println(" " + numberOfOneDollars + " dollars");
System.out.println(" " + numbersOfQuarters + " quarters");
System.out.println(" " + numberOfDimes + " dimes");
System.out.println(" " + numberOfNickels + " nickels");
System.out.println(" " + numberOfPennies + " pennies");
}
}
2.23(驾驶费用)
import java.util.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the driving distance: ");
double drivingDistance = input.nextDouble();
System.out.print("Enter miles per gallon: ");
double milesPerGallon = input.nextDouble();
System.out.print("Enter price per gallon: ");
double pricePerGallon = input.nextDouble();
double costOfDriving = drivingDistance / milesPerGallon * pricePerGallon;
System.out.print("The cost of driving is $" + String.format("%.2f", costOfDriving));
}
}
展开阅读全文