加强版计算器

package com.SC;

import java.util.Scanner;

public class operate {

/**

* 做一个简单的计算器

*/

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double num1 = 0;

double num2 = 0;

boolean flag = true;

boolean flag1 = true;

double sum = 0;

while(flag){

if(flag1){

System.out.println("请输入第一个数:");

num1 = sc.nextDouble();

}else{

num1 = sum;

}

System.out.println("请输入运算符(+,-,*,%,/):");

String operat = sc.next();

System.out.println("请输入第二个数:");

num2 = sc.nextDouble();

if(flag){

if(operat.equals("+")){

sum = num1+num2;

}else if(operat.equals("-")){

sum = num1-num2;

}else if(operat.equals("*")){

sum = num1*num2;

}else if(operat.equals("%")){

sum = num1%num2;

}else if(operat.equals("/")){

sum = num1/num2;

}

System.out.println(sum);

}

System.out.println("请输入1.继续操作2.重新操作3.退出程序");

String str = sc.next();

if("继续操作".equals(str)){

flag1 = false;

}else if("重新操作".equals(str)){

flag1 = true;

}else if("退出程序".equals(str)){

System.out.println("谢谢使用!");

break;

}

}

}

}

你可能感兴趣的:(加强版计算器)