/**
*编写一个简单的计算器,要求在控制台输入功能对应编号,即能进行对应运算,界面如下:
*计算器的生产厂家为:北燕制造厂
*计算器的型号为:A-1234
*计算器的颜色为:银灰色
* ———————————————————————————————————————————————————————————————————
* *****************菜单*****************
* ***************1.加法*****************
* ***************2.减法*****************
* ***************3.乘法*****************
* ***************4.除法*****************
* ***************5.取余*****************
* ***************6.正弦*****************
* ***************7.余弦*****************
* ***************8.正切*****************
* ***************9.余切*****************
* ***************0.退出*****************
* ———————————————————————————————————————————————————————————————————
*/
import java.math.*;
import java.util.*;
public class Cal {
private String name;//计算器生产厂家
private String type;//型号
private String color;//颜色
public Cal (String name,String type,String color)
{
this.name=name;
this.type=type;
this.color=color;
}
public void Print()
{
System.out.println("计算器的生产厂家为:"+name);
System.out.println("计算器的型号为:"+type);
System.out.println("计算器的颜色为:"+color);
}
public void Add(double num1,double num2)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个数字:");
num1=input.nextInt();
System.out.print("请输入第二 个数字:");
num2=input.nextInt();
System.out.print("进行加法运算的结果为:"+(num1+num2));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
}
public void Sub(double num1,double num2)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个数字:");
num1=input.nextInt();
System.out.print("请输入第二 个数字:");
num2=input.nextInt();
System.out.print("进行减法运算的结果为:"+(num1+num2));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
}
public double Mul(double num1,double num2)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个数字:");
num1=input.nextInt();
System.out.print("请输入第二 个数字:");
num2=input.nextInt();
System.out.print("进行除法运算的结果为:"+(num1*num2));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return num1*num2;
}
public double Division(double num1,double num2)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个数字:");
num1=input.nextInt();
System.out.print("请输入第二 个数字:");
num2=input.nextInt();
if(num2==0)
{
System.out.println("除数不能为0");
}
else
{
System.out.print("进行除法运算的结果为:"+(num1/num2));
}
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return num1/num2;
}
public double Quyu(double num1,double num2)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个数字:");
num1=input.nextInt();
System.out.print("请输入第二 个数字:");
num2=input.nextInt();
System.out.print("进行除法运算的结果为:"+(num1%num2));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return num1%num2;
}
public double Sin(double num1)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入一个数字:");
num1=input.nextInt();
System.out.print("进行除法运算的结果为:"+(Math.sin(num1)));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return Math.sin(num1);
}
public double Cos(double num1)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入一个数字:");
num1=input.nextInt();
System.out.print("进行除法运算的结果为:"+(Math.cos(num1)));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return Math.cos(num1);
}
public double Tan(double num1)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("请输入一个数字:");
num1=input.nextInt();
System.out.print("进行除法运算的结果为:"+(Math.tan(num1)));
}
catch(InputMismatchException ex)
{
System.out.println("不能输入字符");
}
return Math.tan(num1);
}
public double Cot(double num1)
{
Scanner input=new Scanner(System.in);
System.out.print("请输入一个数字:");
num1=input.nextInt();
System.out.print("进行除法运算的结果为:"+(1/(Math.tan(num1))));
return 1/(Math.tan(num1));
}
public void Meu()
{
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("***************8.正切*****************");
System.out.println("***************9.余切*****************");
System.out.println("***************0.退出*****************");
System.out.println("———————————————————————————————————————————————————————————————————");
}
public static void main(String[] args) {
Cal s=new Cal("北燕制造厂","A-1234","银灰色");
s.Print();
s.Meu();
Scanner input=new Scanner(System.in);
System.out.print("输入想要进行操作前的序号:");
int num=input.nextInt();
double num1=0,num2=0;
switch(num)
{
case 1:
s.Add(num1,num2);
break;
case 2:
s.Sub(num1, num2);
break;
case 3:;
s.Mul(num1, num2);
break;
case 4:
try{
s.Division(num1, num2);
}
catch(ArithmeticException ex)
{
System.out.println("算术异常:除数不能为0");
}
case 5:
s.Quyu(num1, num2);
break;
case 6:
s.Sin(num1);
break;
case 7:
s.Cos(num1);
break;
case 8:
s.Tan(num1);
break;
case 9:
s.Cot(num1);
break;
default:
System.out.println("无此操作,请重新输入");
break;
}
}
}