博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java语言程序设计(基础篇) 第2章 基本程序设计 课本源代码
阅读量:4676 次
发布时间:2019-06-09

本文共 7095 字,大约阅读时间需要 23 分钟。

//程序清单2-1public class ComputeArea {	//功能:实现计算圆面积		public static void main(String[] args) {				double radius;	//Declare radius		double area;	//Declare area				//Assign a radius		radius = 20;				//Compute Area		area = radius * radius * 3.14159;				//Display results		System.out.println("The area for the circle of radius "+				radius + " is " + area);	}}//程序清单2-2import java.util.Scanner;//功能:从标准输入设备读取Double类型数,实现计算圆面积public class ComputeAreaWithConsuleInput {	public static void main(String[] args) {			//Create a Scanner object		Scanner input = new Scanner(System.in);				//Prompt the user to enter a radius		System.out.print("Enter a number for radius: ");		double radius = input.nextDouble();				//Compute area		double area = radius * radius * 3.14159;				//Display result		System.out.println("The area for the circle of radius "+				radius + "is " +area);	}}//程序清单2-3//功能:读取三个数值,然后显示它们的平均值import java.util.Scanner;public class ComputeAverage {	public static void main(String[] args) {		//Create a Scanner object	   Scanner input = new Scanner(System.in);	   	   //Prompt the user to enter three numbers	   System.out.print("Enter three numbers: ");	   double number1 = input.nextDouble();	   double number2 = input.nextDouble();	   double number3 = input.nextDouble();	   	   //Compute average	   double average = (number1 + number2 + number3)/3;	   	   //Display result	   System.out.println("The average of "+ number1  + ""			   +  number2  + "" +  number3  + " is " + average);		}}//程序清单2-4 //功能:计算一个以秒为单位的时间量所包含的分钟数和剩余秒数的程序import java.util.Scanner;public class DisplayTime {		public static void main(String[] args) {		Scanner input = new Scanner(System.in);		//提示用户输入		System.out.println("Enter an integer for seconds: ");		int seconds = input.nextInt();				int minutes = seconds / 60; //计算包含的分钟数		int remainingSeconds = seconds % 60; //剩余秒数		System.out.println(seconds + " seconds is " + minutes +				" minutes and "+ remainingSeconds + " seconds");	}}//程序清单2-5//功能:利用公式将华氏温度转换成摄氏温度import java.util.Scanner;public class FahrenheitToCelsius {	public static void main(String[] args) {		Scanner input = new Scanner(System.in);				System.out.print("Enter a degree in Fahrenheit: ");		double fahrenheit = input.nextDouble();				//Convert Fahrenheit to Celsius		double celsius = (5.0 / 9) * (fahrenheit - 32);		System.out.println("Fahrenheit " + fahrenheit + " is "				+ celsius + "in Celsius");	}}//程序清单2-6 ShouCurrentTime.java//功能:显示当前时间//说明:本题是开发一个显示当前GMT(格林威治标准时间)的程序;public class ShowCurrentTime {	public static void main(String[] args) {		//获得从1970年1月1日0点到当前时间之间单位为毫秒的差值		long totalMilliseconds = System.currentTimeMillis();				//获取总的秒数		long totalSeconds = totalMilliseconds / 1000;			   //计算当前秒数		long currentSecond = totalSeconds % 60;				//获取总分钟数		long totalMinutes = totalSeconds / 60;				//计算当前分钟数		long currentMinute = totalMinutes % 60;				//获取总的小时数		long totalHours = totalMinutes / 60;				//计算当前小时数		long currentHour = totalHours % 60;				//输出结果		System.out.println("Current time is " + currentHour + ":"				+ currentMinute + ":" + currentSecond + "GMT");	}}//程序清单2-8 ComputeLoan.javaimport java.util.Scanner;public class ComputeLoan {	public static void main(String[] args) {		//创建一个Scanner对象		Scanner input = new Scanner(System.in);				//输入年利率		System.out.println("Enter yearly interest rate,for example 8.25:" );		double annualInterestRate = input.nextDouble();				//获取月利率		double monthlyInterestRate = annualInterestRate/1200;				//输入年数		System.out.println(				"Enter number of years as an integer,for example 5: ");		int numberOfYears = input.nextInt();				//输入贷款总数		System.out.println("Enter loan amount,for example 120000.95: ");		double loanAmount = input.nextDouble();				//计算每月贷款支付额和总贷款支付额		double monthlyPayment = loanAmount * monthlyInterestRate / (1 				- 1 / Math.pow(1+monthlyInterestRate,numberOfYears*12));		double totalPayment = monthlyPayment * numberOfYears * 12;				//输出计算结果		System.out.println("The monthly payment is " + (int)(monthlyPayment * 100)/100.0);		System.out.println("The total payment is " + (int)(totalPayment * 100)/100.0);			}}//程序清单2-9 DisplayUnicode.java//程序功能:显示两个中文字符和三个希腊字母import javax.swing.JOptionPane;public class DisplayUnicode {	public static void main(String[] args){		JOptionPane.showMessageDialog(null, "\u6B22\u8FCE \u03b1\u03b2\u03b3",				"\u6B22\u8FCE welcome",JOptionPane.INFORMATION_MESSAGE);	}}//程序清单2-10//程序功能:整钱兑零import java.util.Scanner;public class ComputeChange {	public static void main(String[] args){		//创建一个Scanner		Scanner input = new Scanner(System.in);				//从控制台输入钱数		System.out.print(				"Enter an amount in double, for example 11.56: ");		double amount = input.nextDouble();				//引入变量remainingAmount来存储变化的余额		int remainingAmount = (int)(amount * 100); //将钱数转换为1分币的个数				//求出1美元的个数		int numberOfOneDollars = remainingAmount / 100;		remainingAmount = remainingAmount % 100;				//求出2角5分币的个数		int numberOfQuarters = remainingAmount / 25;		remainingAmount = remainingAmount % 25; 	//剩余1分币的个数				//求出1角币的个数		int numberOfDimes = remainingAmount / 10;		remainingAmount = remainingAmount % 10;		//剩余1分币的个数				//求出5分币的个数		int numberOfNickles = remainingAmount / 5;		remainingAmount = remainingAmount % 5;		//剩余1分币的个数				//一分币的个数		int numberOfPennies = remainingAmount;				//输出结果		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");			}}//程序清单2-11 ComputeLoanUsingInputDialog.javaimport javax.swing.JOptionPane;public class ComputeLoanUsingInputDialog {	public static void main(String[] args){		//Enter yearly interest rate		String annualInterestRateString = JOptionPane.showInputDialog(				"Enter yearly interest rate,for example 8.25: ");				//Covert string to double		double annualInterestRate =			Double.parseDouble(annualInterestRateString);				//Obtain monthly interest rate		double monthlyInterestRate = annualInterestRate / 1200;				//Enter number of years		String numberOfYearString = JOptionPane.showInputDialog(				"Enter numbers of years as an integer, \nfor example 5: ");			    //Convert string to int		int numberOfYears = Integer.parseInt(numberOfYearString);				//Enter loan amount		String loanString = JOptionPane.showInputDialog(				"Enter loan amount,for example 120000.95:");				//Convert String to double		double loanAmount = Double.parseDouble(loanString);				//Calclate payment 		double monthlyPayment = loanAmount * monthlyInterestRate / (1				- 1/ Math.pow(1 + monthlyInterestRate, numberOfYears * 12));		double totalPayment = monthlyPayment * numberOfYears *12;				//Format to keep two digits after the decimal point		monthlyPayment = (int)(monthlyPayment * 100) / 100.0;		totalPayment =(int)(totalPayment * 100) / 100.0;				//Display results		String output = "The monthly payment is " + monthlyPayment +		"\nThe total payment is " + totalPayment;		JOptionPane.showMessageDialog(null,output);				}}

转载于:https://www.cnblogs.com/wwj9413/archive/2012/05/03/2781243.html

你可能感兴趣的文章
Linux基础命令
查看>>
cat和cp的神奇用法:制作U盘安装盘
查看>>
JNI调用两层C++动态库
查看>>
状态压缩动态规划 - 总结【普及+,提高-】
查看>>
Git pull 强制覆盖本地文件
查看>>
android preferenceActivity的用法
查看>>
让Mac也能拥有apt-get类似的功能——Brew
查看>>
Scrapy开发指南
查看>>
暑假集训 || 网络流
查看>>
吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
查看>>
JVM学习笔记(一)JDK&JRE&JVM
查看>>
云计算商家必争之地 推荐几款云平台
查看>>
[转]B树(多向平衡查找树)详解
查看>>
深入入门正则表达式(java) - 1 - 入门基础
查看>>
ORACLE表、表分区、表空间的区别
查看>>
2015年创业中遇到的技术问题:21-30
查看>>
北戴河游记
查看>>
Intersecting Lines
查看>>
记忆化搜索=搜索的形式+动态规划的思想(来自百度百科)
查看>>
图 | 为什么存在关于图的研究
查看>>