import java.util.Scanner; public class MethodDemo4 { /** * 运算的方法 * * @param num1 数字1 * @param num2 数字2 * @param sign 运算符 * @return 计算之后的结果 */ public static int get(int num1, int num2, char sign) { int result = 0; switch (sign) { case '+': result = num1 + num2; break; } return result; } /** * 数据的录入 */ public static void start() { Scanner scanner = new Scanner(System.in); System.out.println("请输入第一个数字:"); int num1 = scanner.nextInt(); System.out.println("请输入第二个数字:"); int num2 = scanner.nextInt(); System.out.println("请输入运算符:"); //从字符串中提取char类型 charAt(0) char sign = scanner.next().charAt(0); //运算 int result = get(num1, num2, sign); System.out.println("运算的结果为:" + result); } /** * 程序的入口 * * @param args */ public static void main(String[] args) { //启动程序 start(); } }
标签:num1,num2,int,四则运算,System,sign,result From: https://www.cnblogs.com/19981206-zxb/p/16895440.html