当前位置:编程学习 > JAVA >>

Java的编程实现问题

遇到一个作业难题(对我来说)没学过C语言,Java学起来比较困难,比如下面这个怎么实现? 编写程序,实现简单计算器功能,可进行+、-、×、/、^(幂)运算,要求:按算术表达式方式输入(例:输入2 + 5 输出2 + 5 =7)。用switch,break,return语句实现。 求高手指点!我只知道怎么读一个数,表达式怎么读呀?
答案:你好,以前学习JAVA基础时练习写过一个计算器,发来给你参考一下,希望可以帮到你,如果有其他问题可以M我,fkeai@hotmail.com,代码如下:


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author fkeai
 * 用Swing实现的一个简单小计算器
 * 2010.1.7
 */
public class Calculator extends JFrame implements ActionListener{

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Calculator().launchFrame();
 }
 
 JTextField tf;
 public void launchFrame(){
  this.setLocation(300, 250);
  this.setSize(400,300);
  this.setTitle("Calculator");
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setResizable(false);
  
  
  JPanel p1 = new JPanel(new GridLayout(5,4,3,3));
  JPanel p = new JPanel(new BorderLayout());
  tf = new JTextField();
  tf.setText("0");
  tf.setHorizontalAlignment(JTextField.RIGHT);
  tf.setEditable(false);
  
  String bttext [] = {"sqrt","+/-","CE","C","7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
  JButton [] btname = new JButton[20];
  
  for(int i=0; i<bttext.length; i++){
   btname[i] = new JButton(bttext[i]);
   p1.add(btname[i]);
   btname[i].addActionListener(this);
  }
  
  p.add(tf,BorderLayout.NORTH);
  p.add(p1,BorderLayout.SOUTH);
  this.setContentPane(p);
  this.pack();
  this.setVisible(true);
 }
 
 
 
 String num = "0123456789";               
 String str = "sqrt,,CE,C,/,*,-,+,=";
 String str2 = "+/-.";
 double num1 = 0,num2 = 0,result = 0;
 char ch;
 boolean isFirst = true;
 
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  String get = e.getActionCommand();
  if(isFirst && num.indexOf(get) != -1){  
   isFirst = false;
   tf.setText("");
   tf.setText(get);
  }else if(num.indexOf(get) != -1){
   tf.setText(tf.getText() + get);
  }else if(str.indexOf(get) != -1){
   isFirst = true;
   if("+-*/".indexOf(get) != -1){
    ch = get.charAt(0);
    num1 = Double.parseDouble(tf.getText());
   }else if(get.equals("=")){
    num2 = Double.parseDouble(tf.getText());
    switch (ch) {
    case '+':
     result = num1 + num2; break;
    case '-':
     result = num1 - num2; break;
    case '*':
     result = num1 * num2;   break;
    case '/':
     result = num1 / num2;   break;
    default: result = Double.parseDouble(tf.getText());
    }
    tf.setText(String.valueOf(result));
   }else if(get.equals("sqrt")){
    num1 = Double.parseDouble(tf.getText());
    tf.setText(String.valueOf(Math.sqrt(num1)));
   }else if(get.equals("CE")){
    tf.setText("0");
    num1=0;
    num2=0;
   }else if(get.equals("C")){
    if(tf.getText().length() < 2) {tf.setText("0");}
    else tf.setText(tf.getText().substring(0,tf.getText().length() - 1));
   }
   
  }else if(str2.indexOf(get) != -1){
   if(get.equals(".") && tf.getText().indexOf(".") == -1){
    tf.setText(tf.getText() + ".");
   }else if(get.equals("+/-")){
    tf.setText("-" + tf.getText());
   }
  }
 }

}

输入用Scanner input=new Scanner(System.in);
或者 BufferedReader in=new BufferedReader(
     new InputStreamReader(System.in));
就可以从控制台输入了

输出:System.out.println("2+5="+(scanner.nextInt()+scanner.nextInt());

public class Calculator{
private String result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

private double stringToDouble(String x){
double y = Double.parseDouble(x);
return y;
}
private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}else{
result = "The divisor can't be zero!";
}
break;
}
}

public String opAdd(String x){
operate(x);
op = add;
return result;
}
public String opSubtract(String x){
operate(x);
op = sub;
return result;
}
public String opMultiply(String x){
operate(x);
op = mul;
return result;
}
public String opDivide(String x){
operate(x);
op = div;
return result;
}
public String opEquals(String x){
operate(x);
op = 0;
return result;
}
public void opClean(){
op = 0;
result = "0";
}
}

上一个:java 面向接口编程,好处在哪
下一个:北京java培训选哪个培训机构呢?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,