标签:cto while item public string system next ddd static
int + int
double + double 、
计算 n 的阶乘
计算 a的 n次方、
import java.util.Scanner; public class Calculator { public static void main(String[] args) { //计算器界面 calInter(); Scanner sc = new Scanner(System.in); System.out.println("请输入功能选项:"); int i = sc.nextInt(); int j = 1; while(true){ switch (i){ case 1: System.out.println("结果为:"+addInt()); break; case 2: System.out.println("结果为:"+addDouble()); break; case 3: System.out.println("结果为:"+factor()); break; case 4: System.out.println("结果为:"+power()); break; case 5: exit(); j = 0; break; default: System.out.println("输入功能选项有误,您只能选择1~5之间的整数,请重新输入!"); } if(j == 0){ break; } System.out.println("请输入功能选项:"); i = sc.nextInt(); } } //计算器界面 public static void calInter(){ System.out.println("====== 欢迎使用计算器系统 ========"); System.out.println("1. int + int"); System.out.println("2. double + double "); System.out.println("3. 计算 n 的阶乘"); System.out.println("4. 计算 a的 n次方"); System.out.println("5. 退出系统"); } //Int加法 public static int addInt(){ Scanner sc = new Scanner(System.in); System.out.println("第一个int数为:"); int a = sc.nextInt(); System.out.println("第二个int数为:"); int b = sc.nextInt(); return a + b; } //double加法 public static double addDouble(){ Scanner sc = new Scanner(System.in); System.out.println("第一个double数为:"); double a = sc.nextDouble(); System.out.println("第二个数double为:"); double b = sc.nextDouble(); return a + b; } //n的阶乘 public static int factor(){ Scanner sc = new Scanner(System.in); System.out.println("阶乘n为:"); int n = sc.nextInt(); int fact = 1; for(int i = 1; i <= n ;i++){ fact *= i; } return fact; } //a的n次方 public static double power(){ Scanner sc = new Scanner(System.in); System.out.println("底数a为:"); int a = sc.nextInt(); System.out.println("幂数n为:"); int n = sc.nextInt(); return Math.pow(a,n); } //退出系统 public static void exit(){ System.out.println("退出系统"); } }
标签:cto while item public string system next ddd static
原文地址:https://www.cnblogs.com/lsm-boke/p/14631619.html