java控制台计算器代码 JAVA计算器代码
Java写一个计算器 控制台输入算式 可以实现先乘除后加减 没有括号功能 只有加减乘除
你自己研究下,这个代码,别人写的:
10余年专注成都网站制作,企业网站建设,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站建设,高端网页制作,对石凉亭等多个领域,拥有丰富的网站设计经验。
import java.util.Scanner;
public class T {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//String str = "54151+819*5165+15-48/189+5961";
System.out.println("请输入算式,按回车键结束:");
String str = scan.nextLine();
System.out.println(cacComplex(str));
}
private static double cacComplex(String str) {
if (str.equals(""))
return 0;
System.out.println("CAC:" + str);
str = str.replaceAll("[\\[\\{]", "(").replaceAll("[\\]\\}]", ")");
int cl = str.lastIndexOf('(');
if (cl == -1)
return cac(str);
int cr = str.indexOf(')', cl);
String left = str.substring(0, cl);
String right = str.substring(cr + 1);
String middle = str.substring(cl + 1, cr);
return cacComplex(left + cac(middle) + right);
}
public static double cac(String str) {
if (str.equals(""))
return 0;
int ml = str.indexOf('*');
int dl = str.indexOf('/');
if (ml == -1 dl == -1) {
return cacNoMD(str);
}
int index = ml == -1 ? dl : ml;
// 4+5*6*7+8
String left = str.substring(0, index);// 4+5
String m1 = lastNumber(left);
left = left.substring(0, left.length() - m1.length());
String right = str.substring(index + 1);// 6*7+8
String m2 = firstNumber(right);// m2:6
right = right.substring(m2.length());// *7+8
double d1 = Double.parseDouble(m1);
double d2 = Double.parseDouble(m2);
double tmp = 0;
if (index == ml) {
tmp = d1 * d2;
} else if (index == dl) {
tmp = d1 / d2;
}
return cac(left + tmp + right);
}
private static String lastNumber(String str) {
StringBuilder sb = new StringBuilder();
for (int i = str.length() - 1; i = 0; i--) {
char c = str.charAt(i);
if (!Character.isDigit(c) c != '.')
break;
sb.append(c);
}
return sb.reverse().toString();
}
private static String firstNumber(String str) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (!Character.isDigit(c) c != '.')
break;
sb.append(c);
}
return sb.toString();
}
private static double cacNoMD(String str) {
double ret = 0;
StringBuilder sb = new StringBuilder();
char sign = '+';
for (char c : (str + "+").toCharArray()) {
if (!Character.isDigit(c) c != '.') {
if (sb.length() == 0)
continue;
double tmp = Double.parseDouble(sb.toString());
if (sign == '+') {
ret += tmp;
} else {
ret -= tmp;
}
sb = new StringBuilder();
sign = c;
} else {
sb.append(c);
}
}
return ret;
}
}
请输入算式,按回车键结束:
54151+819*5165+15-48/189+5961
CAC:54151+819*5165+15-48/189+5961
4290261.746031746
用JAVA编写一个简单的计算器,要求如下:
然后 通过输入 显示结果,比如说:
以下是上图计算器的代码:
package Computer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Count extends JApplet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField textField = new JTextField("请输入");
String operator = "";//操作
String input = "";//输入的 式子
boolean flag = true;
// boolean flag1 = true;
// boolean flag2 = true;
public void init()//覆写Applet里边的init方法
{
Container C = getContentPane();
JButton b[] = new JButton[16];
JPanel panel = new JPanel();
C.add(textField, BorderLayout.NORTH);
C.add(panel,BorderLayout.CENTER);
panel.setLayout(new GridLayout(4, 4,5,5));
String name[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置 按钮
for(int i=0;i16;i++)//添加按钮
{
b[i] = new JButton(name[i]);
b[i].setBackground(new Color(192,192,192));
b[i].setForeground(Color.BLUE);//数字键 设置为 蓝颜色
if(i%4==3)
b[i].setForeground(Color.RED);
b[i].setFont(new Font("宋体",Font.PLAIN,16));//设置字体格式
panel.add(b[i]);
b[i].addActionListener(this);
}
b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色
b[13].setForeground(Color.RED);
}
public void actionPerformed(ActionEvent e)
{
int cnt = 0;
String actionCommand = e.getActionCommand();
if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))
input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子
else if(actionCommand.equals("C"))
input = "";
else if(actionCommand.equals("="))//当监听到等号时,则处理 input
{
input+= "="+compute(input);
textField.setText(input);
input="";
cnt = 1;
}
else
input += actionCommand;//数字为了避免多位数的输入 不需要加空格
if(cnt==0)
textField.setText(input);
}
private String compute(String input)//即1237 的 样例
{
String str[];
str = input.split(" ");
StackDouble s = new StackDouble();
double m = Double.parseDouble(str[0]);
s.push(m);
for(int i=1;istr.length;i++)
{
if(i%2==1)
{
if(str[i].compareTo("+")==0)
{
double help = Double.parseDouble(str[i+1]);
s.push(help);
}
if(str[i].compareTo("-")==0)
{
double help = Double.parseDouble(str[i+1]);
s.push(-help);
}
if(str[i].compareTo("*")==0)
{
double help = Double.parseDouble(str[i+1]);
double ans = s.peek();//取出栈顶元素
s.pop();//消栈
ans*=help;
s.push(ans);
}
if(str[i].compareTo("/")==0)
{
double help = Double.parseDouble(str[i+1]);
double ans = s.peek();
s.pop();
ans/=help;
s.push(ans);
}
}
}
double ans = 0d;
while(!s.isEmpty())
{
ans+=s.peek();
s.pop();
}
String result = String.valueOf(ans);
return result;
}
public static void main(String args[])
{
JFrame frame = new JFrame("Count");
Count applet = new Count();
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();//applet的init方法
applet.start();//线程开始
frame.setSize(350, 400);//设置窗口大小
frame.setVisible(true);//设置窗口可见
}
}
使用java实现一个控制台计算器程序。
不太完善,你再加点输入的逻辑判断什么的,这个演示只能输入正确的数据才能运行,否则会报错,仅供参考。(求采纳啊)
public class C {
static Double rs;
public static Double yunsuan(String r,Double a1, Double a2){
Double result;
if(r.equals("+")){
result=a1+a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("-")){
result=a1-a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("*")){
result=a1*a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("/")){
result=a1/a2;
System.out.println("运算结果为:"+result);
}else{
result=a1*a2;
System.out.println("运算结果为:"+result);
}
return result;
}
public static void main(String[] args) {
int ss=0;
while(ss==0){
Scanner scan=new Scanner(System.in);
System.out.println("请输入第一个数:");
Double a1=scan.nextDouble();
System.out.println("请输入运算符(+、-、*、/、%):");
String r=scan.next();
System.out.println("请输入第二个数:");
Double a2=scan.nextDouble();
rs=yunsuan(r,a1,a2);
System.out.println("请输入指令(继续运算/新的运算/结束运算)");
String ff=scan.next();
if(ff.equals("继续运算")){
System.out.println("请输入运算符(+、-、*、/、%):");
r=scan.next();
System.out.println("请输入第二个数:");
Double a3=scan.nextDouble();
rs=yunsuan(r,rs,a3);
System.out.println("请输入指令(继续运算/新的运算/结束运算)");
ff=scan.next();
}else if(ff.equals("新的运算")){
continue;
}else{
ss=1;
}
}
}
}
文章题目:java控制台计算器代码 JAVA计算器代码
网页地址:http://cdiso.cn/article/docodhj.html