BigDecimal精度问题参考链接:https://blog.csdn.net/gege87417376/article/details/79550749
1 package com.wsy.type; 2 3 import java.math.BigDecimal; 4 /** 5 * 关于为为什么要使用BigDecimal(大浮点数) 这里说明一下,因为在计算机里进行浮点数(double float)计算时可能回产生非常小的误差 6 * 但是在某一些程序中是不能够有误差的,当然没有为误差的当然是最好的 7 * 所以我选择的是使用BigDecimal 8 * 注: BigDecimal需要使用String类型来构造出来,不然也会像double float一样会有精度偏差 9 * @author wsy 10 * 11 */ 12 public class Arithmetic { 13 private BigDecimal number; 14 private BigDecimal number1; 15 private BigDecimal number2; 16 private char sign; 17 public Arithmetic(BigDecimal number1, BigDecimal number2) { 18 this.number1 = number1; 19 this.number2 = number2; 20 } 21 public Arithmetic( BigDecimal number1, BigDecimal number2, char sign) { 22 this.number1 = number1; 23 this.number2 = number2; 24 this.sign = sign; 25 switch(sign) 26 { 27 case'+': 28 number = this.number1.add(number2); 29 break; 30 case'-': 31 number = this.number1.subtract(number2); 32 break; 33 case'*': 34 number = this.number1.multiply(number2); 35 break; 36 case'/': 37 number = this.number1.divide(number2); 38 break; 39 default: 40 System.out.println("input error"); 41 break; 42 } 43 System.out.println("计算后的结果为:"+number); 44 } 45 public BigDecimal add(BigDecimal number1, BigDecimal number2) 46 { 47 return number1.add(number2); 48 } 49 public BigDecimal subtract(BigDecimal number1, BigDecimal number2) 50 { 51 return number1.subtract(number2); 52 } 53 public BigDecimal multiply(BigDecimal number1, BigDecimal number2) 54 { 55 return number1.multiply(number2); 56 } 57 public BigDecimal divide(BigDecimal number1, BigDecimal number2) 58 { 59 return number1.divide(number2); 60 } 61 public BigDecimal getNumber() { 62 return number; 63 } 64 public void setNumber(BigDecimal number) { 65 this.number = number; 66 } 67 public BigDecimal getNumber1() { 68 return number1; 69 } 70 public void setNumber1(BigDecimal number1) { 71 this.number1 = number1; 72 } 73 public BigDecimal getNumber2() { 74 return number2; 75 } 76 public void setNumber2(BigDecimal number2) { 77 this.number2 = number2; 78 } 79 public char getSign() { 80 return sign; 81 } 82 public void setSign(char sign) { 83 this.sign = sign; 84 } 85 86 87 }
1 package com.wsy.test; 2 3 import java.math.BigDecimal; 4 import java.util.Scanner; 5 6 import com.wsy.type.Arithmetic; 7 8 public class ArithmeticTest { 9 10 public static void main(String[] args) { 11 Scanner in = new Scanner(System.in); 12 BigDecimal number1 = new BigDecimal(in.nextLine()); 13 BigDecimal number2 = new BigDecimal(in.nextLine()); 14 char sign = in.nextLine().charAt(0); 15 Arithmetic a1 = new Arithmetic(number1, number2, sign); 16 17 } 18 19 }
1 package com.wsy.input; 2 3 4 import java.math.BigDecimal; 5 import java.util.Scanner; 6 7 public class CylinderInput { 8 Scanner in = new Scanner(System.in); 9 private BigDecimal ridius; 10 private BigDecimal height; 11 public void scanner() 12 { 13 ridius = new BigDecimal(in.nextLine()); 14 height = new BigDecimal(in.nextLine()); 15 } 16 public void showPrompt() 17 { 18 System.out.println("请输入圆柱体的半径和高:"); 19 } 20 public BigDecimal getRidius() { 21 return ridius; 22 } 23 public void setRidius(BigDecimal ridius) { 24 this.ridius = ridius; 25 } 26 public BigDecimal getHeight() { 27 return height; 28 } 29 public void setHeight(BigDecimal height) { 30 this.height = height; 31 } 32 33 }
1 package com.wsy.method; 2 3 import java.math.BigDecimal; 4 5 public class CylinderCalculation { 6 public static BigDecimal getBottomArea(BigDecimal p ,BigDecimal ridius) 7 { 8 return p.multiply(ridius).multiply(ridius); 9 } 10 public static BigDecimal getVolume(BigDecimal p , BigDecimal ridius , BigDecimal height) 11 { 12 return p.multiply(ridius).multiply(ridius).multiply(height); 13 } 14 }
1 package com.wsy.type; 2 import com.wsy.method.*; 3 import java.math.BigDecimal; 4 5 public class Cylinder { 6 private final BigDecimal p = new BigDecimal("3.14"); 7 private BigDecimal ridius; 8 private BigDecimal height; 9 public Cylinder(BigDecimal ridius, BigDecimal height) { 10 this.ridius = ridius; 11 this.height = height; 12 } 13 public void print() 14 { 15 System.out.println("圆底半径:"+ridius); 16 System.out.println("高:"+height); 17 System.out.println("底面积:"+CylinderCalculation.getBottomArea(p , ridius)); 18 System.out.println("体积:"+CylinderCalculation.getVolume(p, ridius, height)); 19 } 20 public BigDecimal getRidius() { 21 return ridius; 22 } 23 public void setRidius(BigDecimal ridius) { 24 this.ridius = ridius; 25 } 26 public BigDecimal getHeight() { 27 return height; 28 } 29 public void setHeight(BigDecimal height) { 30 this.height = height; 31 } 32 33 }
1 package com.wsy.test; 2 3 import com.wsy.input.CylinderInput; 4 import com.wsy.type.Cylinder; 5 6 public class CylinderTest { 7 8 public static void main(String[] args) { 9 CylinderInput input = new CylinderInput(); 10 input.showPrompt(); 11 input.scanner(); 12 Cylinder c1 = new Cylinder(input.getRidius() , input.getHeight()); 13 c1.print(); 14 System.out.println("======================================================="); 15 input.showPrompt(); 16 input.scanner(); 17 Cylinder c2 = new Cylinder(input.getRidius() , input.getHeight()); 18 c2.print(); 19 } 20 21 }
1 package com.wsy.work.input; 2 import java.util.Scanner; 3 public class LibraryCardInput { 4 Scanner in = new Scanner(System.in); 5 private String accountNumber; 6 private String name; 7 private String IDCard; 8 private String address; 9 public void input() 10 { 11 System.out.println("请依次输入您的帐号、名字、身份证号、地址"); 12 accountNumber = in.nextLine(); 13 name = in.nextLine(); 14 IDCard = in.nextLine(); 15 address = in.nextLine(); 16 } 17 18 public String getAccountNumber() { 19 return accountNumber; 20 } 21 public void setAccountNumber(String accountNumber) { 22 this.accountNumber = accountNumber; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public String getIDCard() { 31 return IDCard; 32 } 33 public void setIDCard(String iDCard) { 34 IDCard = iDCard; 35 } 36 public String getAddress() { 37 return address; 38 } 39 public void setAddress(String address) { 40 this.address = address; 41 } 42 43 }
1 package com.wsy.work.type; 2 public class LibraryCard { 3 private static int bookTotal = 100; 4 private String accountNumber; 5 private String name; 6 private String IDCard; 7 private String address; 8 private int borrowedNumber = 0; 9 private int borrowableNumber = bookTotal; 10 public LibraryCard(String accountNumber, String name, String IDCard, String address) { 11 this.accountNumber = accountNumber; 12 this.name = name; 13 this.IDCard = IDCard; 14 this.address = address; 15 } 16 public void prompt() 17 { 18 System.out.println("欢迎来到图书馆"); 19 System.out.println("请输入1-4"); 20 System.out.println("1:查看书店总书数"); 21 System.out.println("2:借书"); 22 System.out.println("3:还书"); 23 System.out.println("4.退出"); 24 } 25 public void borrowBook(int thisBorrowNumber) 26 { 27 if(thisBorrowNumber > borrowableNumber) 28 { 29 System.out.println("已超过最大可借书数"); 30 return; 31 } 32 System.out.println("本次需借书:"+thisBorrowNumber+"本"); 33 borrowableNumber = borrowableNumber - thisBorrowNumber ; 34 borrowedNumber = borrowedNumber + thisBorrowNumber ; 35 System.out.println("图书还可借书:"+borrowableNumber+"本"); 36 } 37 public void returnBook(int thisReturnNumber) 38 { 39 if(thisReturnNumber > borrowedNumber ) 40 { 41 System.out.print("error"); 42 return ; 43 } 44 System.out.println("本次还书:"+thisReturnNumber+"本"); 45 borrowableNumber = borrowableNumber + thisReturnNumber; 46 borrowedNumber = borrowedNumber - thisReturnNumber; 47 System.out.println("图书馆还可借:"+borrowableNumber+"本"); 48 } 49 public static int getBookTotal() { 50 return bookTotal; 51 } 52 public static void setBookTotal(int bookTotal) { 53 LibraryCard.bookTotal = bookTotal; 54 } 55 public String getAccountNumber() { 56 return accountNumber; 57 } 58 public void setAccountNumber(String accountNumber) { 59 this.accountNumber = accountNumber; 60 } 61 public String getName() { 62 return name; 63 } 64 public void setName(String name) { 65 this.name = name; 66 } 67 public String getIDCard() { 68 return IDCard; 69 } 70 public void setIDCard(String iDCard) { 71 IDCard = iDCard; 72 } 73 public String getAddress() { 74 return address; 75 } 76 public void setAddress(String address) { 77 this.address = address; 78 } 79 public int getBorrowedNumber() { 80 return borrowedNumber; 81 } 82 public void setBorrowedNumber(int borrowedNumber) { 83 this.borrowedNumber = borrowedNumber; 84 } 85 public int getBorrowableNumber() { 86 return borrowableNumber; 87 } 88 public void setBorrowableNumber(int borrowableNumber) { 89 this.borrowableNumber = borrowableNumber; 90 } 91 92 }
1 package com.wsy.work.test; 2 import com.wsy.work.input.*; 3 import com.wsy.work.type.*; 4 import java.util.Scanner; 5 public class LibraryCardTest { 6 7 public static void main(String[] args) { 8 LibraryCardInput input = new LibraryCardInput(); 9 input.input(); 10 LibraryCard card = new LibraryCard(input.getAccountNumber() , input.getName() , input.getIDCard() , input.getAddress()); 11 Scanner in = new Scanner(System.in); 12 int n; 13 int number; 14 exit: 15 while(true) 16 { 17 card.prompt(); 18 n = in.nextInt(); 19 switch(n) 20 { 21 case 1: 22 System.out.println("本书店中还剩下"+card.getBorrowableNumber()+"本书"); 23 break; 24 case 2: 25 System.out.print("请输入您需要借多少本书:"); 26 number = in.nextInt(); 27 card.borrowBook(number); 28 break; 29 case 3: 30 System.out.print("请输入您需要还多少本书:"); 31 number = in.nextInt(); 32 card.returnBook(number); 33 break; 34 case 4: 35 System.out.println("成功退出 欢迎下次使用"); 36 break exit; 37 } 38 } 39 in.close(); 40 } 41 42 }
1 package com.wsy.work.input; 2 import java.util.Scanner; 3 public class EleChargeManagementInput { 4 Scanner in = new Scanner(System.in); 5 private double readNumberLastMonth; 6 private double readNumberThisMonth; 7 public void input() 8 { 9 System.out.println("请依次输入上月电表读数、本月电表读数"); 10 readNumberLastMonth = in.nextDouble(); 11 readNumberThisMonth = in.nextDouble(); 12 } 13 public double getReadNumberLastMonth() { 14 return readNumberLastMonth; 15 } 16 public void setReadNumberLastMonth(double readNumberLastMonth) { 17 this.readNumberLastMonth = readNumberLastMonth; 18 } 19 public double getReadNumberThisMonth() { 20 return readNumberThisMonth; 21 } 22 public void setReadNumberThisMonth(double readNumberThisMonth) { 23 this.readNumberThisMonth = readNumberThisMonth; 24 } 25 26 }
1 package com.wsy.work.test; 2 import java.util.Scanner; 3 4 import com.wsy.work.input.EleChargeManagementInput; 5 import com.wsy.work.type.EleChargeManagement; 6 public class EleChargeManagementTest { 7 8 public static void main(String[] args) { 9 Scanner in = new Scanner(System.in); 10 EleChargeManagementInput input = new EleChargeManagementInput(); 11 input.input(); 12 EleChargeManagement ele1 = new EleChargeManagement(input.getReadNumberLastMonth(), input.getReadNumberThisMonth()); 13 //显示上月、本月电表读数 14 ele1.show(); 15 //计算本月用电数 16 ele1.computeEleConsumption(); 17 //显示本月用电数 18 ele1.showEleConsumption(); 19 //显示本月电费 20 ele1.showElePrice(); 21 in.close(); 22 } 23 24 }