程序清单2-1:ComputeArea.java


public class ComputeArea {public static void main(String[] args) {// TODO Auto-generated method stubdouble radius;double area;radius = 20;area = radius * radius * 3.14159; System.out.println("The area for the circle of radius " + radius + " is " + area);}}

程序清单2-2:ComputeAreaWithConsoleInput.java


import java.util.Scanner;public class ComputeAreaWithConsoleInput {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a number for radius: ");double radius = input.nextDouble();double area = radius * radius * 3.14159;System.out.println("The area for the circle of radius " + radius + " is " + area);input.close();}}

程序清单2-3:ComputeAverage.java


import java.util.Scanner;public class ComputeAverage {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a number for radius: ");double number1 = input.nextDouble();double number2 = input.nextDouble();double number3 = input.nextDouble();double average = (number1 + number2 + number3) / 3;System.out.println("The average of " + number1 + " " + number2 + " " + number3 + " is " + average);input.close();}}

程序清单2-4:ComputeAreaWithConstant.java


import java.util.Scanner;public class ComputeAreaWithConstant {public static void main(String[] args) {// TODO Auto-generated method stubfinal double PI = 3.14159;Scanner input = new Scanner(System.in);System.out.print("Enter a number for radius: ");double radius = input.nextDouble();double area = radius * radius * PI;System.out.println("The area for the circle of radius " + radius + "  is " + area);input.close();}}

程序清单2-5:DisplayTime.java


import java.util.Scanner;public class DisplayTime {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("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");input.close();}}

程序清单2-6:FahrenheitToCelsius.java


import java.util.Scanner;public class FahrenheitToCelsius {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a degree in Fahrenheit: ");double fahrenheit = input.nextDouble();double celsius = (5.0 / 9) * (fahrenheit - 32);System.out.println("Fahrenheit " + fahrenheit + " is " + celsius +" in Celeius");input.close();}}

程序清单2-7:ShowCurrentTime.java


public class ShowCurrentTime {public static void main(String[] args) {// TODO Auto-generated method stublong 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 % 24;System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");}}

程序清单2-8:SalesTax.java


import java.util.Scanner;public class SalesTax {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter purchase amount: ");double purchaseAmount = input.nextDouble();double tax = purchaseAmount * 0.06;System.out.println("Sales tax is $" + (int)(tax * 100) / 100.0);input.close();}}

程序清单2-9:ComputeLoan.java


import java.util.Scanner;public class ComputeLoan {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter annual interest rate, e.g., 7.25: ");double annualInterestRate = input.nextDouble();double monthlyInterestRate = annualInterestRate / 1200;System.out.print("Enter number of years as an integer, e.g.,5: ");int numberOfYears = input.nextInt();System.out.print("Enter loan amount, e.g.,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);input.close();}}

程序清单2-10: ComputeChange.java


import java.util.Scanner;public class ComputeChange {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);int numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;int numberOfQuarters = 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 + "consists of");System.out.println(" " + numberOfOneDollars + " dollars");System.out.println(" " + numberOfQuarters + " quarters");System.out.println(" " + numberOfDimes + " dimes");System.out.println(" " + numberOfNickels + " nickels");System.out.println(" " + numberOfPennies + " pennies");input.close();}}

Java语言程序设计与数据结构(基础篇)梁勇第二章书中例题相关推荐

  1. Java语言程序设计与数据结构(基础篇)梁勇第一章书中例题

    程序清单1-1:Welcome.java public class Welcome {public static void main(String[] args) {System.out.printl ...

  2. java语言程序设计与数据结构基础篇,2万字20个项目实例

    一.前言 聊的是八股的文,干的是搬砖的活! 面我的题开发都用不到,你为什么要问?可能这是大部分程序员求职时的经历,甚至也是大家讨厌和烦躁的点.明明给的是拧螺丝的钱.明明做的是写CRUD的事.明明担的是 ...

  3. java第十版基础篇答案第九章_《Java语言程序设计》(基础篇原书第10版)第九章复习题答案...

    第九章 9.1:类为对象定义属性和行为,对象从类创建. 9.2:public class ClassName { } 9.3:ClassName v; 9.4:new ClassName(); 9.5 ...

  4. 《Java语言程序设计》(基础篇原书第10版)第八章复习题答案

    第八章 8.1:int[] array = new int[4][5]; 8.2: 二维数组的行可以有不同的长度. 8.3:输出结果为:array[0][1] is 2 8.4: int[][] r ...

  5. 《Java语言程序设计与数据结构》编程练习答案(第七章)(一)

    <Java语言程序设计与数据结构>编程练习答案(第七章)(一) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  6. 《Java语言程序设计与数据结构》编程练习答案(第三章)(三)

    <Java语言程序设计与数据结构>编程练习答案(第三章)(三) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  7. 《Java语言程序设计与数据结构》编程练习答案(第四章)(二)

    <Java语言程序设计与数据结构>编程练习答案(第四章)(二) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  8. 《Java语言程序设计与数据结构》编程练习答案(第四章)(一)

    <Java语言程序设计与数据结构>编程练习答案(第四章)(一) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  9. 《Java语言程序设计与数据结构》编程练习答案(第二章)(二)

    <Java语言程序设计与数据结构>编程练习答案(第二章)(二) 英文名:Introduction to Java Programming and Data Structures, Comp ...

最新文章

  1. 6行Python代码实现进度条效果(Progress、tqdm、alive-progress​​​​​​​和PySimpleGUI库)
  2. 博客园左边导航菜单的问题
  3. html+li标签+高度,有时在使用Jquery插入LI元素时,JavaScript不会调整UL元素的高度
  4. SpringBoot+SweeAlert实现alert提示与前后端数据交互
  5. Eclipse调试Logcat类的说明
  6. 判断程序是否运行在虚拟机中的代码
  7. 求凸函数极值 CSF迭代法(雾)
  8. 正则表达式符号特殊详解_常用正则表达式_Java中正则表达式的使用
  9. python的数据结构包括那些_python算法与数据结构-什么是数据结构
  10. linux搭建测试环境常见问题,在Linux环境下搭建CCID测试环境
  11. AspNetPager分页控件的运用 【转】-有用
  12. Struts2标签库常用标签
  13. 【Debian】ftp安装
  14. asp.net网站后台退出后,点后退按钮仍能进,如何安全退出
  15. 来,教你写一手好SQL!
  16. 无法定位程序输入点_Z21qRegisterResourceDataiPKhs0于动态链接库***.exe上
  17. abaqus单位问题
  18. win7下快捷方式关联错误的修复
  19. 【洛谷】P1216数字三角形
  20. html 提示框 js,JavaScript实现短暂提示框功能

热门文章

  1. php新闻删除功能设计,PHP开发 新闻发布系统之新闻删除页面
  2. 介绍一款数据库管理软件
  3. python读取文件夹下所有图片
  4. 字节跳动byteDance
  5. 【经验】VMware|windows更新20H2版本后VMware虚拟机无法开启(禁用Device guard)
  6. js中向数组中添加元素unshift() 方法
  7. vue qrcodejs2生成二维码实现手机APP扫码进行web网页登录
  8. MPP大规模并行计算数据库与分布式数据库的区别
  9. R7-17 程序填空题2
  10. MySQL 8.0如何配置my.cnf