//程序清单2-1
public 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-2
import 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.java
import 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.java
import 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);
}
}
java语言程序设计(基础篇) 第2章 基本程序设计 课本源代码
最新推荐文章于 2024-06-12 21:18:01 发布