Java程序,使用开关盒生成计算器
以下程序接受两个整数变量,将一个运算符作为运算符。根据所选的运算符,程序执行相应的操作并打印结果。
示例
import java.util.Scanner;
public class ab39_CalculatorUsingSwitch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 1st number ::");
int a = sc.nextInt();
System.out.println("Enter value of 2nd number ::");
int b = sc.nextInt();
System.out.println("Select operation");
System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
char ch = sc.next().charAt(0);
switch(ch) {
case 'a' :
System.out.println("Sum of the given two numbers: "+(a+b));
break;
case 's' :
System.out.println("Difference between the two numbers: "+(a-b));
break;
case 'm' :
System.out.println("Product of the two numbers: "+(a*b));
case 'd' :
System.out.println("Result of the division: "+(a/b));
break;
default :
System.out.println("Invalid grade");
}
}
}输出结果
Enter value of 1st number :: 52 Enter value of 2nd number :: 85 Select operation Addition-a: Subtraction-s: Multiplication-m: Division-d: a Sum of the given two numbers: 137
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短