第十三章

  1. package cn.jbit.test;  
  2.   
  3. public class testCalculator {  
  4.     public static void main(String[] args) {  
  5.         Calculator calcu = new Calculator();// 实例化  
  6.         calcu.Show();// 调用业务处理方法  
  7.     }  
  8. }  
  9.   
  10. package cn.jbit.test;  
  11.   
  12. import java.util.Scanner;  
  13.   
  14. public class Calculator {  
  15.   
  16.     public void Show() {// 业务处理  
  17.         Scanner input = new Scanner(System.in);  
  18.         System.out.println("请选择运算:1.加法 2.减法  3.乘法 4.除法:");  
  19.         int op = input.nextInt();  
  20.         int b = 0;// 记录次数  
  21.         for (int i = 0; i < 3; i++) {  
  22.             if (op != 1 && op != 2 && op != 3 && op != 4) {// 判断输入数字是否非法  
  23.                 System.out.println("您的输入有误,请输入1~4任意整数");  
  24.                 op = input.nextInt();// 接收  
  25.                 b = i;  
  26.                 continue;  
  27.             } else {  
  28.                 System.out.println("请输入第一个数:");  
  29.                 double num1 = input.nextDouble();  
  30.                 System.out.println("请输入第二个数:");  
  31.                 double num2 = input.nextDouble();  
  32.                 System.out.print("***运算结果是:" + Open(op, num1, num2));  
  33.                 break;  
  34.             }  
  35.         }  
  36.         if (b == 2) {// 判断  
  37.             System.out.println("输入错误已满,退出系统,谢谢您的使用!");  
  38.         }  
  39.     }  
  40.   
  41.     public double Open(int op, double num1, double num2) {// 调用运算方法  
  42.         if (op == 1) {// 输入1 调用加法  
  43.             return Add(num1, num2);  
  44.         } else if (op == 2) {// 输入2 调用减法  
  45.             return Minus(num1, num2);  
  46.         } else if (op == 3) {// 输入3 调用乘法  
  47.             return Multiple(num1, num2);  
  48.         } else if (op == 4) {// 输入4 调用除法  
  49.             return Divide(num1, num2);  
  50.         }  
  51.         return 0;  
  52.     }  
  53.   
  54.     public double Add(double num1, double num2) {// 加法  
  55.         return (num1 + num2);  
  56.     }  
  57.   
  58.     public double Minus(double num1, double num2) {// 减法  
  59.         return (num1 - num2);  
  60.     }  
  61.   
  62.     public double Multiple(double num1, double num2) {// 乘法  
  63.         return (num1 * num2);  
  64.     }  
  65.   
  66.     public double Divide(double num1, double num2) {// 除法  
  67.         return (num1 / num2);  
  68.     }  
  69. }  
  70.   
  71.   
  72.   
  73.   
  74.   
  75.   
  76.   
  77.   
  78.   
  79.   
  80.   
  81. package cn.jbit.test;  
  82.   
  83. public class testTest3 {  
  84.     public static void main(String[] args) {  
  85.         // 简答题3 模拟简单的贷款月供计算器  
  86.         Test3 test = new Test3(); // 实例化  
  87.         test.Show();// 调用Show方法  
  88.     }  
  89. }  
  90. package cn.jbit.test;  
  91.   
  92. import java.util.Scanner;  
  93.   
  94. public class Test3 {  
  95.   
  96.     public void Show() {// 定义初始方法  
  97.         Scanner input = new Scanner(System.in);  
  98.         System.out.println("请输入贷款金额:");  
  99.         double loan = input.nextDouble();// 贷款金额  
  100.         System.out.println("请选择贷款年限:1.3年(36个月)   2.5年(60个月)   3.20年(240个月)");  
  101.         int yearchoice = input.nextInt();// 贷款年限  
  102.         int j = 0;// 纪录输入次数  
  103.         for (int i = 0; i < 3; i++) {  
  104.             j = i;  
  105.             if (yearchoice == 1) {  
  106.                 System.out.println("您输入的贷款金额是:" + loan + "元" + "\t"  
  107.                         + "您选择的贷款年限是:" + "3年(36个月)" + "\n" + "\n" + "***您的月供是:"  
  108.                         + loan(loan, yearchoice) + "元");  
  109.                 break;  
  110.             }  
  111.             if (yearchoice == 2) {  
  112.                 System.out.println("您输入的贷款金额是:" + loan + "元" + "\t"  
  113.                         + "您选择的贷款年限是:" + "5年(60个月)" + "\n" + "\n" + "***您的月供是:"  
  114.                         + loan(loan, yearchoice) + "元");  
  115.                 break;  
  116.             }  
  117.             if (yearchoice == 3) {  
  118.                 System.out.println("您输入的贷款金额是:" + loan + "元" + "\t"  
  119.                         + "您选择的贷款年限是:" + "20年(240个月)" + "\n" + "\n"  
  120.                         + "***您的月供是:" + loan(loan, yearchoice) + "元");  
  121.                 break;  
  122.             } else {  
  123.                 System.out.println("您的输入有误,请输入1~3任意整数");  
  124.                 yearchoice = input.nextInt();  
  125.                 continue;  
  126.             }  
  127.         }  
  128.         if (j == 2) {  
  129.             System.out.println("输入错误次数已满,退出系统,谢谢您的使用!!");  
  130.         }  
  131.     }  
  132.   
  133.     private double loan(double loan, int yearchoice) {// 定义loan方法 计算month 月供  
  134.         if (yearchoice == 1) {  
  135.             double month = (loan * 6.03 / 100 + loan) / 36;// 36个月月供  
  136.             return month;  
  137.         }  
  138.         if (yearchoice == 2) {  
  139.             double month = (loan * 6.12 / 100 + loan) / 60;// 60个月月供  
  140.             return month;  
  141.         }  
  142.         if (yearchoice == 3) {  
  143.             double month = (loan * 6.39 / 100 + loan) / 240;// 240个月月供  
  144.             return month;  
  145.         }  
  146.         return 0;  
  147.     }  
  148. }  
  149. //第四题  
  150. //**** 原始代码片段无法运行  
  151. //错误1   没有实例化  
  152. //错误2 方法调用错误  
  153. package cn.jbit.test.JianDaTi;  
  154.   
  155. import java.util.*;  
  156.   
  157. public class JavaTest {// 修改后代码片段  
  158.     public static void main(String[] args) {  
  159.         JavaTest java=new JavaTest();// 实例化  
  160.         Scanner input = new Scanner(System.in);  
  161.         System.out.print("请输入第一个数:");  
  162.         int num1 = input.nextInt();  
  163.         System.out.print("请输入第二个数:");  
  164.         int num2 = input.nextInt();  
  165.         System.out.print("两数的平均值为:" + java.avg(num1, num2));  
  166.     }  
  167.   
  168.     public int avg(int big, int small) {  
  169.         int money = 0;  
  170.         money = (big + small) / 2;  
  171.         return money;  
  172.     }  
  173. }  
  174.   
  175.   
  176.   
  177.   
  178.   
  179.   
  180.   
  181.   
  182.   
  183.   
  184.   
  185.   
  186. //第五题 三角形  
  187.   
  188. package cn.jbit.test;  
  189.   
  190. public class testTriangle {  
  191.   
  192.     public static void main(String[] args) {  
  193.         Triangle triangle = new Triangle();  
  194.         triangle.shaPe();  
  195.     }  
  196. }  
  197.   
  198. package cn.jbit.test.JianDaTi;  
  199.   
  200. import java.util.Scanner;  
  201.   
  202. public class Triangle {  
  203.     public boolean isTriangle(int a, int b, int c) {// 判断是否构成三角形  
  204.         if (a + b > c && a + c > b && b + c > a) {  
  205.             return true;  
  206.         }  
  207.         return false;  
  208.     }  
  209.   
  210.     public void shaPe() {// 判断构成何种三角形  
  211.         Scanner input = new Scanner(System.in);  
  212.         char d = 'y';// 判断是否录入学生信息  
  213.         while (d == 'y') {  
  214.             System.out.println("请输入第一条边:");  
  215.             int a = input.nextInt();  
  216.             System.out.println("请输入第二条边:");  
  217.             int b = input.nextInt();  
  218.             System.out.println("请输入第三条边:");  
  219.             int c = input.nextInt();  
  220.             isTriangle(a, b, c);  
  221.             if (isTriangle(a, b, c) == false) {  
  222.                 System.out.println("无法构成三角形");  
  223.             } else {  
  224.                 if (a + b == c * c || a + c == b * b || b + c == a * a) {  
  225.                     System.out.println("这是一个直角三角形");  
  226.                 }  
  227.                 if (a * a > b * b + c * c || b * b > a * a + c * c  
  228.                         || c * c > a * a + b * b) {  
  229.                     System.out.println("这是一个钝角三角形");  
  230.                 } else {  
  231.                     System.out.println("这是一个锐角三角形");  
  232.                 }  
  233.             }  
  234.             System.out.println("继续吗?(y继续,输入y以外任意字符结束)");  
  235.             d = input.next().charAt(0);  
  236.   
  237.         }  
  238.   
  239.     }  
  240. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值