小弟是初学者,很多东西都不大懂。最近写了一个简易的计算器,就是可以做加减乘除的那种,那图形界面的编译的时候没问题,执行的时候图形也出来了,就是在最关键的时刻 按下‘=’的时候输出不了结果,提示了一大推的异常。小弟是初学者啊 异常信息看不大懂,编写此程序也是靠以前学习C语言时写程序的那点编程思想,所以这个程序个人感觉非常不好,兴许有更简单的方法,但小弟现在很想知道我这个鸟程序哪里异常了,肯定要找出错误 以后才不会再犯蛤。小弟是自学的,没经过系统的学习java,所以很多地方都不规范请谅解
如果有前辈有耐心愿意帮忙 再下感激万分!![import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame
{
private JTextField f1;
private Container cp;
private String [] btn={"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."};
JButton []b=new JButton[16];
public static String s="";
public Calculator()
{
setTitle("简易计算器");
setSize(300,300);
setLocation(100,100);
cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
f1=new JTextField(26);
jp1.add(f1);
jp3.setLayout(new GridLayout(4,4));
for(int i=0;i<btn.length;i++)
{
b[i]=new JButton(btn[i]);
jp3.add(b[i]);
b[i].addActionListener(new CalButton());
}
cp.add(jp1,BorderLayout.NORTH);
cp.add(jp3,BorderLayout.CENTER);
addWindowListener(new WindowDestroyer());
}
class CalButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("="))
{
f1.setText(PushAndPop.getResult());
return;
}
s=s+e.getActionCommand();
f1.setText(s);
}
}
}
class WindowDestroyer extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
class PushAndPop
{
public static String getResult()
{
String sss=new String();
char []c=new char[20];
char ch,chh;
int j=0;
double temp,tempp;
PushAndPop2.push2('#');
for(int i=0;i<Calculator.s.length();i++)
{
ch=Calculator.s.charAt(i);
if(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/')
{
c[j++]=ch; //用字符数组c来存放由数字组成的一个数据
}
else
{
temp=Double.parseDouble(sss.copyValueOf(c)); //把字符数组转换成字符串,然后再转换成double型
PushAndPop2.push(temp);
if(PushAndPop2.getTop()=='#')
{
PushAndPop2.push2(ch);
continue;
}
if(PushAndPop2.cmp(PushAndPop2.getTop(),ch)>0)
{
//两个数据从数据栈中弹出,一个操作符从操作栈中弹出,传递给pushAndPop类中operate方法,进行运算
temp=PushAndPop2.operate(PushAndPop2.pop(),PushAndPop2.pop(),PushAndPop2.pop2());
PushAndPop2.push(temp); //运算结果入栈
}
else
{
PushAndPop2.push2(ch);
}
j=0;
}
}
temp=PushAndPop2.operate(PushAndPop2.pop(),PushAndPop2.pop(),PushAndPop2.pop2());
String ss;
ss=new String();
ss=ss.valueOf(temp); //把双精度浮点型转换成字符串
return ss; //以字符串的形式返回计算结果
}
}
class PushAndPop2
{
static double [] digit=new double[20];
static int top=0;
static char [] ch=new char[20];
static int top1=0;
public static void push(double temp)
{
digit[top++]=temp;
}
public static void push2(char chh)
{
ch[top1++]=chh;
}
public static double pop()
{
return digit[top--];
}
public static char pop2()
{
return ch[top1--];
}
public static char getTop()
{
return ch[top];
}
public static int cmp(char a,char b)
{
if((a=='+'||a=='-')&&(b=='*'||b=='/'))
{
return 0;
}
return 1;
}
public static double operate(double a,double b,char d)
{
switch (d)
{
case '+':return (a+b);
case '-':return (a-b);
case '*':return (a*b);
case '/':return (a/b);
}
return 0.00;
}
}
class test1
{
public static void main(String[] arges)
{
Calculator c=new Calculator();
c.setVisible(true);
}
}
如果有前辈有耐心愿意帮忙 再下感激万分!![import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame
{
private JTextField f1;
private Container cp;
private String [] btn={"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."};
JButton []b=new JButton[16];
public static String s="";
public Calculator()
{
setTitle("简易计算器");
setSize(300,300);
setLocation(100,100);
cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
f1=new JTextField(26);
jp1.add(f1);
jp3.setLayout(new GridLayout(4,4));
for(int i=0;i<btn.length;i++)
{
b[i]=new JButton(btn[i]);
jp3.add(b[i]);
b[i].addActionListener(new CalButton());
}
cp.add(jp1,BorderLayout.NORTH);
cp.add(jp3,BorderLayout.CENTER);
addWindowListener(new WindowDestroyer());
}
class CalButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("="))
{
f1.setText(PushAndPop.getResult());
return;
}
s=s+e.getActionCommand();
f1.setText(s);
}
}
}
class WindowDestroyer extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
class PushAndPop
{
public static String getResult()
{
String sss=new String();
char []c=new char[20];
char ch,chh;
int j=0;
double temp,tempp;
PushAndPop2.push2('#');
for(int i=0;i<Calculator.s.length();i++)
{
ch=Calculator.s.charAt(i);
if(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/')
{
c[j++]=ch; //用字符数组c来存放由数字组成的一个数据
}
else
{
temp=Double.parseDouble(sss.copyValueOf(c)); //把字符数组转换成字符串,然后再转换成double型
PushAndPop2.push(temp);
if(PushAndPop2.getTop()=='#')
{
PushAndPop2.push2(ch);
continue;
}
if(PushAndPop2.cmp(PushAndPop2.getTop(),ch)>0)
{
//两个数据从数据栈中弹出,一个操作符从操作栈中弹出,传递给pushAndPop类中operate方法,进行运算
temp=PushAndPop2.operate(PushAndPop2.pop(),PushAndPop2.pop(),PushAndPop2.pop2());
PushAndPop2.push(temp); //运算结果入栈
}
else
{
PushAndPop2.push2(ch);
}
j=0;
}
}
temp=PushAndPop2.operate(PushAndPop2.pop(),PushAndPop2.pop(),PushAndPop2.pop2());
String ss;
ss=new String();
ss=ss.valueOf(temp); //把双精度浮点型转换成字符串
return ss; //以字符串的形式返回计算结果
}
}
class PushAndPop2
{
static double [] digit=new double[20];
static int top=0;
static char [] ch=new char[20];
static int top1=0;
public static void push(double temp)
{
digit[top++]=temp;
}
public static void push2(char chh)
{
ch[top1++]=chh;
}
public static double pop()
{
return digit[top--];
}
public static char pop2()
{
return ch[top1--];
}
public static char getTop()
{
return ch[top];
}
public static int cmp(char a,char b)
{
if((a=='+'||a=='-')&&(b=='*'||b=='/'))
{
return 0;
}
return 1;
}
public static double operate(double a,double b,char d)
{
switch (d)
{
case '+':return (a+b);
case '-':return (a-b);
case '*':return (a*b);
case '/':return (a/b);
}
return 0.00;
}
}
class test1
{
public static void main(String[] arges)
{
Calculator c=new Calculator();
c.setVisible(true);
}
}
解决方案 »
- java中转型问题
- JNI在本地代码中接收STRING数组的问题
- SWT用布局后,如何得到已布局的WIDGET的实际大小?
- jdbc运行的时间到底怎么会事 它的性能又如何提高
- 怎么获得整首个的歌词!
- 使用java.text.DecimalFormat类,如何将类似 1,234,567.1 数字转化成 1234567.1呢?
- date型数据的打印问题?
- 用Jbuilder做开发工具,如何开始,我已有了一些J2EE的框架文件。
- 有关JAVA字符集的详细系统资料哪位兄弟或MM有啊!好着急呀!遇到了问题。
- java socket 出错!
- 数字格式化的一些问题
- 请高手帮我改一下冒泡算法。我觉得这么写是正确的可是错了。
import java.awt.*;
import java.awt.event.*;class Calculator extends JFrame {
private JTextField f1;
private Container cp;
private String[] btn = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"+", "-", "*", "/", "=", "." }; JButton[] b = new JButton[16]; public Calculator() {
setTitle("简易计算器");
setSize(300, 300);
setLocation(100, 100);
cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel jp3 = new JPanel();
f1 = new JTextField(26);
jp3.setLayout(new GridLayout(4, 4));
for (int i = 0; i < btn.length; i++) {
b[i] = new JButton(btn[i]);
jp3.add(b[i]);
b[i].addActionListener(new CalButton());
}
cp.add(f1, BorderLayout.NORTH);
cp.add(jp3, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} class CalButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("=")) {
f1.setText(calc(f1.getText())+"");
return;
}
f1.setText(f1.getText()+e.getActionCommand());
}
}
static double calc(String expr) {
for(int i=1; i<expr.length(); i++) {
char c = expr.charAt(i);
if(c=='+' || c=='-') {
double a = Double.parseDouble(expr.substring(0, i));
double b = calc(expr.substring(i+1));
return c=='+' ? a+b : a-b;
}
if(c=='*' || c=='/') {
double a = Double.parseDouble(expr.substring(0, i));
int j = i+1;
for(;j<expr.length(); j++) {
char op = expr.charAt(j);
if(op=='+' || op=='-' || op=='*' || op=='/') break;
}
double b = Double.parseDouble(expr.substring(i+1,j));
return calc((c=='*' ? a*b : a/b) + expr.substring(j));
}
}
return Double.parseDouble(expr);
}
}public class Test1 {
public static void main(String[] arges) {
Calculator c = new Calculator();
c.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;class Calculator extends JFrame
{
private JTextField f1;
private Container cp;
private String [] btn={"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."};
JButton []b=new JButton[16];
public static String s="";
public Calculator()
{
setTitle("简易计算器");
setSize(300,300);
setLocation(100,100);
cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
f1=new JTextField(26);
jp1.add(f1);
jp3.setLayout(new GridLayout(4,4));
for(int i=0;i<btn.length;i++)
{
b[i]=new JButton(btn[i]);
jp3.add(b[i]);
b[i].addActionListener(new CalButton());
}
cp.add(jp1,BorderLayout.NORTH);
cp.add(jp3,BorderLayout.CENTER);
addWindowListener(new WindowDestroyer());
}
class CalButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("="))
{
f1.setText(PushAndPop.getResult());
return;
}
s=s+e.getActionCommand();
f1.setText(s);
}
}
}class WindowDestroyer extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}class PushAndPop
{
public static String getResult()
{
String sss=new String();
char []c=new char[20];
char ch,chh;
int j=0;
double temp,tempp;
PushAndPop2.push2('#');
for(int i=0;i<Calculator.s.length();i++)
{
ch=Calculator.s.charAt(i);
if(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/')
{
c[j++]=ch; //用字符数组c来存放由数字组成的一个数据
}
else
{
/*
* 这里还要指定要转化的个数
*/
temp=Double.parseDouble(sss.copyValueOf(c, 0, j)); //把字符数组转换成字符串,然后再转换成double型
PushAndPop2.push(temp);
if(PushAndPop2.getTop()=='#')
{
PushAndPop2.push2(ch);
/*
* 这里要把j清0,否则数字会堆积起来
*/
j = 0;
continue;
}
/*
* 必须把多余的符号都先清除,否则结果会异常
*/
while(PushAndPop2.cmp(PushAndPop2.getTop(),ch)>0)
{
/*
* 如果为'#'说明已经清光了,结束
*/
if (PushAndPop2.getTop() == '#')
break;
//两个数据从数据栈中弹出,一个操作符从操作栈中弹出,传递给pushAndPop类中operate方法,进行运算
/*
* 出栈顺序和计算顺序是反的,要改过来
*/
double num2 = PushAndPop2.pop();
double num1 = PushAndPop2.pop();
temp=PushAndPop2.operate(num1, num2, PushAndPop2.pop2());
PushAndPop2.push(temp); //运算结果入栈
}
//else
//{
PushAndPop2.push2(ch);
//}
j=0;
}
}
/*
* 最后一个数没有压入栈
*/
temp=Double.parseDouble(sss.copyValueOf(c, 0, j));
PushAndPop2.push(temp);
/*
* 这里也要把所有的符号都处理掉
*/
while (PushAndPop2.getTop() != '#')
{
/*
* 出栈顺序和计算顺序是反的
*/
double num2 = PushAndPop2.pop();
double num1 = PushAndPop2.pop();
temp=PushAndPop2.operate(num1, num2, PushAndPop2.pop2());
PushAndPop2.push(temp);
}
String ss;
ss=new String();
ss=ss.valueOf(temp); //把双精度浮点型转换成字符串
return ss; //以字符串的形式返回计算结果
}
}class PushAndPop2
{
static double [] digit=new double[20];
static int top=0;
static char [] ch=new char[20];
static int top1=0;
public static void push(double temp)
{
digit[top++]=temp;
}
public static void push2(char chh)
{
ch[top1++]=chh;
}
public static double pop()
{
/*
* 这里和下面都应该是先减
*/
return digit[--top];
} public static char pop2()
{
return ch[--top1];
}
public static char getTop()
{
/*
* top应该是top-1
*/
return ch[top - 1];
}
public static int cmp(char a,char b)
{
if((a=='+'||a=='-')&&(b=='*'||b=='/'))
{
return 0;
}
return 1;
}
public static double operate(double a,double b,char d)
{
switch (d)
{
case '+':return (a+b);
case '-':return (a-b);
case '*':return (a*b);
case '/':return (a/b);
}
return 0.00;
}
}public class test1
{
public static void main(String[] arges)
{
Calculator c=new Calculator();
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}