From 53a86cfb0f91d39f1cc27bdf23f27921676103ea Mon Sep 17 00:00:00 2001 From: polyhymnia <2281017491@qq.com> Date: Tue, 5 Jan 2021 20:29:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8B=E5=98=89=E7=82=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Demo1.java" | 28 ++++++++++ .../Demo2.java" | 21 ++++++++ .../Demo3.java" | 31 +++++++++++ .../Demo4.java" | 21 ++++++++ .../Method3_area.java" | 52 +++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 "\345\256\213\345\230\211\347\202\234/Demo1.java" create mode 100644 "\345\256\213\345\230\211\347\202\234/Demo2.java" create mode 100644 "\345\256\213\345\230\211\347\202\234/Demo3.java" create mode 100644 "\345\256\213\345\230\211\347\202\234/Demo4.java" create mode 100644 "\345\256\213\345\230\211\347\202\234/Method3_area.java" diff --git "a/\345\256\213\345\230\211\347\202\234/Demo1.java" "b/\345\256\213\345\230\211\347\202\234/Demo1.java" new file mode 100644 index 0000000..496c7b7 --- /dev/null +++ "b/\345\256\213\345\230\211\347\202\234/Demo1.java" @@ -0,0 +1,28 @@ +package com.md.Work; + +import java.util.Scanner; + +import javax.naming.spi.DirStateFactory.Result; + +//定义一个函数,用于计算m的n次幂,m和n在主函数中由用户输入, +//调用自定义函数进行计算,并输出结果 +public class Demo1 { + public static void number(int n , int m) { + int result = 1; + for (int i = 1; i <= n ; i++) { + result = m*result; + } + System.out.println("结果为" + result); + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入底数"); + int m = scanner.nextInt(); + System.out.println("输入次数"); + int n = scanner.nextInt(); + + number(n , m); + + + } +} diff --git "a/\345\256\213\345\230\211\347\202\234/Demo2.java" "b/\345\256\213\345\230\211\347\202\234/Demo2.java" new file mode 100644 index 0000000..ac8a6c5 --- /dev/null +++ "b/\345\256\213\345\230\211\347\202\234/Demo2.java" @@ -0,0 +1,21 @@ +package com.md.Work; + +import java.util.Scanner; + +//在主函数中从键盘接收X, Y , Z3个数,编写函数计算这3个数的立方和并返回计算结果: +//S=X3+Y3+Z3 +public class Demo2 { + public static double number(double X , double Y , double Z) { + double result = X*X*X + Y*Y*Y + Z*Z*Z; + return result; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入三个数"); + double X = scanner.nextInt(); + double Y = scanner.nextInt(); + double Z = scanner.nextInt(); + + System.out.println("最后结果" + number(X, Y, Z)); + } +} diff --git "a/\345\256\213\345\230\211\347\202\234/Demo3.java" "b/\345\256\213\345\230\211\347\202\234/Demo3.java" new file mode 100644 index 0000000..7e4d366 --- /dev/null +++ "b/\345\256\213\345\230\211\347\202\234/Demo3.java" @@ -0,0 +1,31 @@ +package com.md.Work; + +import java.util.Scanner; + +//用户输入整数n,计算1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!) +public class Demo3 { + public static int number(int n) { + int sum = 1; + int a = 0; + + for (int i = 1; i <= n; i++) { + for (int k = 1; k <= i; k++) { + int inside = 1; + for (int j = 1; j <= k; j++) { + inside *= j; + } + a = inside + a ; + } + + } + return a; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入一个正整数"); + int n = scanner.nextInt(); + + System.out.println("结果" + number(n)); + } +} diff --git "a/\345\256\213\345\230\211\347\202\234/Demo4.java" "b/\345\256\213\345\230\211\347\202\234/Demo4.java" new file mode 100644 index 0000000..37200e9 --- /dev/null +++ "b/\345\256\213\345\230\211\347\202\234/Demo4.java" @@ -0,0 +1,21 @@ +package com.md.Work; + +import java.util.Scanner; + +public class Demo4 { + public static void area(double r) { + double result = r*r*3.14; + System.out.println("面积:"+result); + } + public static void perimiter(double r) { + double result = 2*r*3.14; + System.out.println("周长:"+result); + } + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + double r = scan.nextDouble(); + + area(r); + perimiter(r); + } +} diff --git "a/\345\256\213\345\230\211\347\202\234/Method3_area.java" "b/\345\256\213\345\230\211\347\202\234/Method3_area.java" new file mode 100644 index 0000000..dc7cdd3 --- /dev/null +++ "b/\345\256\213\345\230\211\347\202\234/Method3_area.java" @@ -0,0 +1,52 @@ +package com.md.lession8; + +import java.util.Scanner; + +/* + * 根据用户不同的选择计算不同形状的面积(三角形、正方形、长方形)(用不同方式实现 + * :1.参数和面积在自定义函数中输入和输出 + * 2.参数在主函数中接收 + * 3.参数在主函数中接收并且在主函数中要计算面积和) + * + */ +public class Method3_area { + public static double triangle(double a , double h) { + double result = a*h/2; + return result; + } + public static double squre(double a ) { + double result = a*a; + return result; + } + public static double rectangle(double a , double b) { + double result = a*b; + return result; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String STR = scanner.next(); + switch (STR) { + case "三角形": + System.out.println("请输入宽"); + double a = scanner.nextDouble(); + System.out.println("请输入高"); + double b = scanner.nextDouble(); + System.out.println("三角形的面积:"+triangle(a, b)); + break; + + case "正方形": + System.out.println("请输入边长"); + double c = scanner.nextDouble(); + System.out.println("正方形的面积"+squre(c)); + break; + + case "长方形": + System.out.println("请输入长"); + double d = scanner.nextDouble(); + System.out.println("请输入宽"); + double e = scanner.nextDouble(); + System.out.println("长方形的面积:"+rectangle(d,e)); + break; + } + } +} -- Gitee