Java语言程序设计与数据结构(基础篇) 课后题练习 第二章

本文提供了一系列使用Java编程语言解决实际问题的示例代码,包括温度转换、几何计算、单位换算等,适合初学者实践和巩固基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.1

import java.util.Scanner;
public class Test2_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 + " " + "Fahrenheit");
	}
}

2.2

import java.util.Scanner;
public class Test2_2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		final double PI = 3.14;
		System.out.println("Enter the radius and length of a cyclinder:");
		double radius = input.nextDouble();
		double length = input.nextDouble();
		double area = radius * radius * 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 Test2_3{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter a value for feet");
		double feet = input.nextDouble();
		double meter = feet * 0.305;
		System.out.println(feet + "feet is" + " " + meter + "meter");
	}
}

2.4

import java.util.Scanner;
public class Test2_4{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("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 Test2_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();
		double gratuity = subtotal * rate * 0.01;
		double total = subtotal + gratuity;		
		System.out.println("The gratuity is" + " "  + "$" + gratuity + " " + "and total is" + " " + "$" + total);
	}
}

2.6

import java.util.Scanner;
public class Test2_6{
	public static void main(String[] args){
		System.out.println("Enter a number between 0 and 1000:");
		Scanner input = new Scanner(System.in);
		int number = input.nextInt();
		int hundreds = number / 100;
		int ones = number % 10;
		int tens = (number / 10) % 10;
		int sum = hundreds + tens + ones;
		System.out.println("The sum of the digits is " + sum);
	}
}

2.7

import java.util.Scanner;
public class Test2_7{
	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 day= minutes / (60 * 24);
		int years = day / 365;
		int days =  day % 365;
		System.out.println(minutes + " " + "minuters is approxiamtely" + " " + years + " " + "and" + " " + days + " " + "days");
	}
}

2.8

在这里插入代码片

2.9

import java.util.Scanner;
public class Test2_9{
	public static void main(String[] args){
		Scanner input  = new Scanner(System.in);
		System.out.println("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 Test2_10{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the amount of water in kilograms:");
		double M = input.nextDouble();
		System.out.println("Enter the initial temperature:");
		double temperature1 = input.nextDouble();
		System.out.println("Enter the final temperature:");
		double temperature2 = input.nextDouble();
		double energy = M*(temperature2 - temperature1) * 4184;
		System.out.println("The enery needed is" + " " + energy);
	}
}

2.11

import java.util.Scanner;
public class Test2_11{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the number of years");
		int year = input.nextInt();
		double number = 312032486;
		double t;
		t = 365*24*60*60;
		for(int i = 1;i <= year;i++){
		number = number +(t/7-t/13+t/45);}

		System.out.println("The population in" + " " + year + " " + "years is" + " " + number);
	}
}

2.12

import java.util.Scanner;
public class Test2_12{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter speed and acceleration:");
		double speed  = input.nextDouble();
		double acceleration  = input.nextDouble();
		double length = (speed * speed) / (2 * acceleration);
		System.out.println("The minimum length for this airplane is" + " " + length);
	}
}

2.13

import java.util.Scanner;
public class Test2_13{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the monthly saving amount:");
		double money = input.nextDouble();
		final double t = 0.00417;
		double sum = 0.0;
		for(int i = 1;i <= 6;i++){
			sum = (money + sum) * (1 + t);
		}
		System.out.println("After the six month, the account value is $" + sum);
	}
}

2.14

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter weight in pouns:");
		double weight = input.nextDouble();
		System.out.println("Enter height in inches:");
		double height = input.nextDouble();
		double B = (weight * 0.45359237)/((height * 0.0254) *(height * 0.0254));
		System.out.println("BMI is" + " " + B + "."); 
	}
}

2.15

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter x1 and y1");
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		System.out.println("Enter x2 and y2");
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		double t = (x2 - x1)*(x2 - x1) + (y2 - y1) * (y2 - y1);
		double distance = Math.pow(t,0.5);
		System.out.println("The distance between the tow points is" + " " +distance);
	}		
}

2.16

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the length of the side:");
		double a = input.nextDouble();
		double S = (3 * Math.pow(3,0.5) * a * a) / 2;
		System.out.println("The area of the hexagon is" + " " +S);
	}		
}

2.17

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the temperature inFahrenheit between -58°F and 41°F:");
		double t = input.nextDouble();
		System.out.println("Enter the wind speed (>= 2) in miles per hour:");
		double v = input.nextDouble();
		double T = 35.74 + 0.6215 * t - 35.75 * Math.pow(v,0.16) + 0.4275 * t * Math.pow(v,0.16);
		System.out.println("The wind chill index is" + " " +T);
	}		
}

2.18

public class Test2{
	public static void main(String[] args){
		System.out.println("a     b     pow(a,b)");
		for(int i = 1;i<=5;i++){
			System.out.println(i + "     " + (i+1) + "     " + (int)Math.pow(i,i+1));
		}
	}		
}

2.19

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the coordinates of three points separated by spaces:");
		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 d1 = Math.pow((x1 - x2)* (x1 - x2) + (y1 - y2) * (y1 - y2),0.5);
		double d2 = Math.pow((x2 - x3)* (x2 - x3) + (y2 - y3) * (y2 - y3),0.5);
		double d3 = Math.pow((x1 - x3)* (x1 - x3) + (y1 - y3) * (y1 - y3),0.5);
		double s = (d1 + d2 +d3) / 2;
		double S = Math.pow(s * (s - d1) *(s - d2) * (s - d3),0.5);
		System.out.println("The area of the trangle is " + " " +S);	
		}
	}

2.20

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter balance and interest rate (eg.3 for 3%):");
		double surplus = input.nextDouble();
		double rate = input.nextDouble();
		double interest = surplus * (rate / 1200);
		System.out.println("The interest is" + " " +interest);
		}
	}

2.21

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter investment amount:");
		double investment = input.nextDouble();
		System.out.println("Enter annual interest rate in percentage:");
		double interest = input.nextDouble();
		System.out.println("Enter number of year:");
		int year = input.nextInt();
		double value = investment * Math.pow((1 + interest / 1200),year * 12);
		System.out.println("Future value is $" + value);
		}
	}

2.22

在这里插入代码片

2.23

import java.util.Scanner;
public class Test2{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the driving distance:");
		double distance = input.nextDouble();
		System.out.println("Enter miles per gallon:");
		double miles_per_gallon = input.nextDouble();
		System.out.println("Enter price per gallon:");
		double price_per_gallon = input.nextDouble();
		double money = distance / miles_per_gallon * price_per_gallon;
		System.out.println("The cost of driving is $" + " " + money);
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值