import java.util.*;
/**
@@author by沉默
@@version 1.0
*/
class Operation{
private double a;
private double b;
Scanner in=new Scanner(System.in);
void jia(){
System.out.println("请输入被加数");
a=in.nextDouble();
System.out.println("请输入加数");
b=in.nextDouble();
System.out.println("计算结果为:"+(a+b));
}
void sub(){
System.out.println("请输入被减数");
double a=in.nextDouble();
System.out.println("请输入减数");
double b=in.nextDouble();
System.out.println("计算结果为:"+(a-b));
}
void cheng(){
System.out.println("请输入被乘数");
double a=in.nextDouble();
System.out.println("请输入乘数");
double b=in.nextDouble();
System.out.println("计算结果为:"+(a*b));
}
void div()throws Exception{
System.out.println("请输入被除数");
double a=in.nextDouble();
System.out.println("请输入除数");
double b=in.nextDouble();
if(b<=0)
throw new Exception("除数不能为负数");
System.out.println("计算结果为:"+(a/b));
}
void yu(){
System.out.println("请输入被余数");
double a=in.nextDouble();
System.out.println("请输入余数");
double b=in.nextDouble();
System.out.println("计算结果为:"+(a%b));
}
void print99(){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}
void zh(){
System.out.println("请选择你要转换的类型");
System.out.println("1、十进制转二进制");
System.out.println("2、十进制转八进制");
System.out.println("3、十进制转十六进制");
int type=in.nextInt();
if(type==1){
System.out.println("请输入你要转换的数值");
int num=in.nextInt();
toBin(num);
}
if(type==2){
System.out.println("请输入你要转换的数值");
int num=in.nextInt();
toBa(num);
}if(type==3){
System.out.println("请输入你要转换的数值");
int num=in.nextInt();
toHex(num);
}
}
void trans(int num,int base,int offset){//转换方法trans
if(num==0){
System.out.println(0);
return;
}
char[] chs ={'0','1','2','3' //1、建立一个表,包含所有进制,最多32位
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
char[] arr = new char[32]; //2、num&base值
int pos = arr.length; //建立一个数组指针
while(num!=0){//循环开始,条件不为0
int temp = num & base;
arr[--pos] = chs[temp];
num=num>>>offset;
}
for(int i=pos;i<arr.length;i++){
System.out.print(arr[i]);
}
}
/*
十进制---->二进制
*/
void toBin(int num){
trans(num,1,1);
}
/*
十进制---->八进制
*/
void toBa(int num){
trans(num,7,3);
}
/*
十进制---->十六进制
*/
void toHex(int num){
trans(num,15,4);
}
}
public class test{
public static void main(String[] args){
Scanner in=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、打印9*9乘法表");
System.out.println("7、进制转换");
Operation op=new Operation();
int type=in.nextInt();
if(type==1){
op.jia();
}else if(type==2){
op.sub();
}else if(type==3){
op.cheng();
}else if(type==4){
try
{
op.div();
}
catch (Exception e)
{
System.out.println("除数不能为0");
}
}else if(type==5){
op.yu();
}else if(type==6){
op.print99();
}else if(type==7){
op.zh();
}
}
}
|