java方法練習之簡單計算器

oneaka發表於2020-09-26

關於導包的描述正確的是 C
A. 一個原始檔中只能有一條導包語句
B. 導包語句必須寫在原始檔的第一行有效語句
C. 導包語句的關鍵字為 import
D. 位於java.util 包中的類無需導包,可以直接使用
E. 使用String類 必須書寫導包語句
F. 一個原始檔中必須有一條導包語句

這是java初學中練習的方法,簡單計算器,都是實現一些小功能,下面展示了四種,都大同小異。有的是可以支援小數的,有的是保持case程式碼很清潔,有的是把程式碼寫在了case裡

計算器1

-------------計算器系統----------------
請輸入編號選擇您需要使用的功能:
1.兩個數的加法
2.兩個數的減法
3.兩個小數的減法
4.兩個數的除法
5.一個數的階乘
6.一個數的幾次方
7.退出系統




package 雜亂練習;

import java.util.Scanner;

/*c
請輸入編號選擇您需要使用的功能:
	1.兩個數的加法
	2.兩個數的減法
	3.兩個小數的減法
	4.兩個數的除法
	5.一個數的階乘
	6.一個數的幾次方
	7.退出系*/
public class 計算器啊啊啊 {

	public static void main(String[] args) {
		while (true) {
			// TODO Auto-generated method stub
			Scanner sc = new Scanner(System.in);
			System.out.println("-------------計算器系統----------------");
			System.out.println("請輸入編號選擇您需要使用的功能:");
			System.out.println("	1.兩個數的加法");
			System.out.println("    2.兩個數的減法");
			System.out.println("	3.兩個小數的減法");
			System.out.println("	4.兩個數的除法");
			System.out.println("	5.一個數的階乘");
			System.out.println("	6.一個數的幾次方");
			System.out.println("	7.退出系*/");
			int xuanze = sc.nextInt();
			switch (xuanze) {
			case 1:
				System.out.println("您選擇的是1,請輸入第一個的數字:");
				int a = sc.nextInt();
				System.out.println("請輸入第二個數字:");
				int b = sc.nextInt();
				System.out.println(a + "+" + b + "=");
				sum(a, b);
				break;
			case 2:
				System.out.println("您選擇的是2,請輸入第一個的數字:");
				int c = sc.nextInt();
				System.out.println("請輸入第二個數字:");
				int d = sc.nextInt();
				System.out.println(c + "-" + d + "=");
				sub(c, d);
				break;

			case 3:
				System.out.println("您選擇的是3,請輸入第一個的數字:");
				double tt = sc.nextDouble();
				System.out.println("請輸入第二個數字:");
				double yy = sc.nextDouble();
				System.out.println(tt + "-" + yy + "=");
				subb(tt, yy);
				break;
			case 4:
				System.out.println("您選擇的是4,請輸入第一個的數字:");
				int e = sc.nextInt();
				System.out.println("請輸入第二個數字:");
				int f = sc.nextInt();
				System.out.println(e + "/" + f + "=");
				div(e, f);
				break;
			case 5:
				System.out.println("您選擇的是5,請輸入進行階乘的數字:");
				int g = sc.nextInt();
				System.out.println(g + "的階乘是:");
				factorial(g);
				break;
			case 6:
				System.out.println("您選擇是6,請輸入進行次方的數:");

				int j = sc.nextInt();
				System.out.println("請輸入想進行的次方數:");
				int k = sc.nextInt();
				System.out.println(j + "的" + k + "次方為:");

				cifang(j, k);
				break;
			case 7:
				System.out.println("退出系統");
				break;

			}
		}
	}

	private static void cifang(int j, int k) {
		// TODO Auto-generated method stub
		int num = 1;
		for (int i = 1; i <= k; i++) {
			num *= j;
		}
		System.out.println(num);
	}

	private static void factorial(int g) {
		// TODO Auto-generated method stub
		int num = 1;
		for (int i = 1; i <= g; i++) {
			num *= i;
		}
		System.out.println(num);
	}

	private static void div(int e, int f) {
		// TODO Auto-generated method stub
		System.out.println(e / f);
	}

	private static void subb(double cc, double dd) {
		// TODO Auto-generated method stub
		System.out.println(cc - dd);
	}

	private static void sub(int c, int d) {
		// TODO Auto-generated method stub
		System.out.println(c - d);
	}

	private static void sum(int a, int b) {
		// TODO Auto-generated method stub
		System.out.println(a + b);
	}

}

**

計算器2

簡單的計算器:
使用者鍵盤輸入兩個數字,然後提示使用者選擇(1-4)
相關運算方式
輸入1進行加法運算
輸入2進行減法運算
輸入3進行乘法運算
輸入4進行除法運算
根據選擇的運算方式呼叫相應的函式,進行運算,並返回運算結果,列印輸出到螢幕1

package 方法練習;


import java.util.Scanner;



public class 計算器 {

	public static void main(String[] args) {
		/*3.簡單的計算器:

		使用者鍵盤輸入兩個數字,然後提示使用者選擇(1-4)
		相關運算方式(輸入1進行加法運算、輸入2進行減法運算
				、輸入3進行乘法運算、輸入4進行除法運算)

		根據選擇的運算方式呼叫相應的函式,進行運算,並返回運算結果,列印輸出到螢幕1
*/
		Scanner scanner=new Scanner(System.in);
		while (true) {
			System.out.println("請使用者輸入兩個數字");
			System.out.println("a:");
			int a = scanner.nextInt();
			System.out.println("b");
			int b = scanner.nextInt();
			System.out.println("請您選擇計算器功能:");
			int xuan = scanner.nextInt();
			switch (xuan) {
			case 1:
				sum(a, b);
				break;

			case 2:
				sub(a, b);
				break;

			case 3:
				mul(a, b);
				break;

			case 4:
				div(a, b);
				break;

			}
		}
	}

	private static void div(int a, int b) {
		// TODO Auto-generated method stub
		int div=a/b;
		System.out.println("您選擇了4,a/b="+div);
	}

	private static void mul(int a, int b) {
		// TODO Auto-generated method stub
		int mul=a*b;
		System.out.println("您選擇了3,a*b="+mul);
	}

	private static void sub(int a, int b) {
		// TODO Auto-generated method stub
		int sub=a-b;
		System.out.println("您選擇了2,a-b="+sub);
	}

	private static void sum(int a, int b) {
		// TODO Auto-generated method stub
		int sum=a+b;
		System.out.println("您選擇了1,a+b="+sum);
	}

}

計算器3

-------------計算器系統----------------
請輸入編號選擇您需要使用的功能
1.兩個數的加法
2.兩個數的減法
3.兩個數的乘法
4.兩個數的除法
5.一個數的階乘
6.一個數的幾次方
7.退出系統

package 方法練習;

import java.util.Scanner;



public class 加減乘除計算器 {

	private static int a;
	private static int b;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("-------------計算器系統----------------");
		System.out.println("請輸入編號選擇您需要使用的功能:");
		System.out.println("1.兩個數的加法");
		System.out.println("2.兩個數的減法");
		System.out.println("3.兩個數的乘法");
		System.out.println("4.兩個數的除法");
		System.out.println("5.一個數的階乘");
		System.out.println("6.一個數的幾次方");
		System.out.println("7.退出系統");
		System.out.println("歡迎來到簡單計算器,請輸入您要實現的功能代號");

		int g = scanner.nextInt();
		switch (g) {
		case 1:
			sum(a, b);
			main(null);
			break;

		case 2:
			sub(a, b);
			main(null);
			break;

		case 3:
			mul(a, b);
			main(null);
			break;

		case 4:
			div(a, b);
			main(null);
			break;

		case 5:
			factorial(a);
			main(null);
			break;

		case 6:
			cifang(a, b);
			main(null);
			break;

		case 7:
			tuichu();
			main(null);
			break;
		}
	}

	private static void sum(int a, int b) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了1,兩個數相加,請輸入兩個數");
		System.out.println("a:");
		int a1 = scanner.nextInt();
		System.out.println("b:");
		int b1 = scanner.nextInt();
		System.out.println("a+b=" + (a1 + b1));
	}

	private static void sub(int a, int b) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了2,兩個相減,請輸入兩個數");
		System.out.println("a:");
		int a1 = scanner.nextInt();
		System.out.println("b:");
		int b1 = scanner.nextInt();
		System.out.println("a-b=" + (a1 - b1));
	}

	private static void mul(int a, int b) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了3,兩個數相乘,請輸入兩個數");
		System.out.println("a:");
		int a1 = scanner.nextInt();
		System.out.println("b:");
		int b1 = scanner.nextInt();
		System.out.println("a*b=" + a1 * b1);
	}

	private static void div(int a, int b) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了4,兩個數相除,請輸入兩個數");
		System.out.println("a:");
		int a1 = scanner.nextInt();
		System.out.println("b:");
		int b1 = scanner.nextInt();
		System.out.println("a÷b=" + a1 / b1);
	}

	private static void factorial(int a) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了5,請輸入要進行階乘運算的數");
		System.out.println("a:");
		int a1 = scanner.nextInt();
		int fact = 1;
		for (int i = 1; i <= a1; i++) {
			fact *= i;
		}
		System.out.println("a!=" + fact);
	}

	private static void cifang(int a, int b) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("您選了6,某個數的次方");
		System.out.println("請輸入要次方的數a:");
		int a1 = scanner.nextInt();
		System.out.println("請輸入次方的次數b:");
		int b1 = scanner.nextInt();
		int cifang = 1;
		for (int i = 1; i <= b1; i++) {
			cifang *= a1;
		}
		System.out.println(a1+"的"+b1+"的次方=" + cifang);
	}

	private static void tuichu() {
		// TODO Auto-generated method stub
		System.out.println("您選擇了7.退出程式,歡迎下次使用!!!");
	}

}

計算器4
--------歡迎使用計算機系統-------

  1. int +int
  2. double + double
  3. int -int
  4. double -double
  5. int +int+int
  6. double +double+double
  7. 計算n的階乘
  8. 計算a的n次方
    請輸入對應的編號選擇對應功能!!
package 課堂練習九一八;

import java.util.Scanner;

public class 方法計算器 {

	private static int a;
	private static int b;
	private static int aqqq;
	private static int b1;
	private static int ac;
	private static int cdd;
	private static double d;
	private static double e;
	private static int aaa;
	private static int oooo;
	private static int cs;
	private static double de;
	private static double fqq;
	private static double f;
	private static int jj;
	private static int cc;
	private static int nn;
	private static int iui;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		System.out.println("--------歡迎使用計算機系統-------");
		System.out.println("1. int +int");
		System.out.println("2. double + double");
		System.out.println("3. int -int");
		System.out.println("4. double -double");
		System.out.println("5. int +int+int");
		System.out.println("6. double +double+double");
		System.out.println("7. 計算n的階乘");
		System.out.println("8. 計算a的n次方");
		System.out.println("請輸入對應的編號選擇對應功能!!");

		int num = sc.nextInt();

		switch (num) {

		case 1:

			insum(a, b);
			break;
		case 2:

			dousum(aqqq, b1);
			break;
		case 3:

			injian(ac, cdd);
			break;
		case 4:

			dooujian(d, e);
			break;
		case 5:

			insumm(aaa, oooo, cs);
			break;
		case 6:

			dousumm(de, fqq, f);
			break;
		case 7:

			fact(jj);
			break;
		case 8:

			cifang(cc, nn);
			break;

		}
		main(null);
	}

	private static void cifang(int cc, int nn) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的是計算a的n次方功能");
		System.out.println("請輸入想計算次方的數:");
		int qq = sc.nextInt();
		System.out.println("請輸入想進行多少次方的數:");
		int rr = sc.nextInt();
		int cifang = 1;
		for (int i = 1; i <= rr; i++) {

			cifang *= qq;
		}

		System.out.println(qq + "的" + rr + "次方是" + cifang);

	}

	private static void fact(int jj) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的是計算n的階乘的功能");
		System.out.println("請輸入進行階乘運算的數字:");
		int oo = sc.nextInt();
		int fff = 1;
		for (int i = 1; i <= oo; i++) {
			fff *= i;
		}
		System.out.println(oo + "進行階乘運算後結果為:" + fff);

	}

	private static void dousumm(double d, double e, double f) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算三個double型數的相加");
		System.out.println("請輸入第一個double型別的數:");
		double y = sc.nextDouble();
		System.out.println("請輸入第二個double型別的數:");
		double ee = sc.nextDouble();
		System.out.println("請輸入第三個double型別的數:");
		double s = sc.nextDouble();
		double sum = y + ee + s;
		System.out.println(y + "+" + ee + "+" + s + "的和為:" + sum);

	}

	private static void insumm(int a, int b, int c) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算三個int型數的相加");
		System.out.println("請輸入第一個int型別的數:");
		int y = sc.nextInt();
		System.out.println("請輸入第二個int型別的數:");
		int ee = sc.nextInt();
		System.out.println("請輸入第三個int型別的數:");
		int s = sc.nextInt();
		int sum = y + ee + s;
		System.out.println(y + "+" + ee + "+" + s + "的和為:" + sum);

	}

	private static void dooujian(double d, double e) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算兩個double型數的相減");
		System.out.println("請輸入第一個double型別的數:");
		double y = sc.nextDouble();
		System.out.println("請輸入第二個double型別的數:");
		double ee = sc.nextDouble();
		double jian = y - ee;
		System.out.println(y + "-" + ee + "=" + jian);

	}

	private static void dousum(double d, double e) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算兩個double型數的相加");
		System.out.println("請輸入第一個double型別的數:");
		double y = sc.nextDouble();
		System.out.println("請輸入第二個double型別的數:");
		double ee = sc.nextDouble();
		double jia = y + ee;
		System.out.println(y + "+" + ee + "=" + jia);

	}

	private static void injian(int a, int b) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算兩個int型數的相減");
		System.out.println("請輸入第一個int型別的數:");
		int y = sc.nextInt();
		System.out.println("請輸入第二個int型別的數:");
		int ee = sc.nextInt();
		int jian = y - ee;
		System.out.println(y + "-" + ee + "=" + jian);

	}

	private static void insum(int a, int b) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("您選擇的功能是計算兩個int型數的和");
		System.out.println("請輸入第一個int型別的數:");
		int y = sc.nextInt();
		System.out.println("請輸入第二個int型別的數:");
		int ee = sc.nextInt();
		int jian = y + ee;
		System.out.println(y + "+" + ee + "=" + jian);

	}

}

相關文章