网站建设资讯

NEWS

网站建设资讯

java初级项目源代码 Java项目源代码

求JAVA简易计算机源代码

试试这个

为清徐等地区用户提供了全套网页设计制作服务,及清徐网站建设行业解决方案。主营业务为成都网站建设、网站制作、清徐网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

public class SZJSQ extends JApplet implements ActionListener

{

boolean i=true;

private JButton num0=new JButton("0");

private JButton num1=new JButton("1");

private JButton num2=new JButton("2");

private JButton num3=new JButton("3");

private JButton num4=new JButton("4");

private JButton num5=new JButton("5");

private JButton num6=new JButton("6");

private JButton num7=new JButton("7");

private JButton num8=new JButton("8");

private JButton num9=new JButton("9");

private JButton zuok=new JButton("(");

private JButton youk=new JButton(")");

private JButton dian=new JButton(".");

private JButton NULL=new JButton("N");

private JButton plu=new JButton("+");

private JButton min=new JButton("-");

private JButton mul=new JButton("x");

private JButton div=new JButton("/");

private JButton equ=new JButton("=");

private JButton cle=new JButton("C");//清除

private JTextField space=new JTextField(30);

public void init()

{

JPanel text=new JPanel();

text.setLayout(new FlowLayout());

text.add(space);

JPanel buttons=new JPanel();

buttons.setLayout(new GridLayout(5,4));

buttons.add(num9);

buttons.add(num8);

buttons.add(num7);

buttons.add(plu);

buttons.add(num6);

buttons.add(num5);

buttons.add(num4);

buttons.add(min);

buttons.add(num3);

buttons.add(num2);

buttons.add(num1);

buttons.add(mul);

buttons.add(num0);

buttons.add(cle);

buttons.add(equ);

buttons.add(div);

buttons.add(zuok);

buttons.add(youk);

buttons.add(dian);

buttons.add(NULL);

(num9).addActionListener(this);

(num8).addActionListener(this);

(num7).addActionListener(this);

(num6).addActionListener(this);

(num5).addActionListener(this);

(num4).addActionListener(this);

(num3).addActionListener(this);

(num2).addActionListener(this);

(num1).addActionListener(this);

(num0).addActionListener(this);

(plu).addActionListener(this);

(min).addActionListener(this);

(mul).addActionListener(this);

(div).addActionListener(this);

(equ).addActionListener(this);

(cle).addActionListener(this);

(zuok).addActionListener(this);

(youk).addActionListener(this);

(dian).addActionListener(this);

setLayout(new BorderLayout());

add("North",text);

add("South",buttons);

space.setText("0");

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==num9)

{

if(i==true)

{

space.setText("9");

i=false;

}

else space.setText(space.getText()+'9');

}

if(e.getSource()==num8)

{

if(i==true)

{

space.setText("8");

i=false;

}

else space.setText(space.getText()+'8');

}

if(e.getSource()==num7)

{

if(i==true)

{

space.setText("7");

i=false;

}

else space.setText(space.getText()+'7');

}

if(e.getSource()==num6)

{

if(i==true)

{

space.setText("6");

i=false;

}

else space.setText(space.getText()+'6');

}

if(e.getSource()==num5)

{

if(i==true)

{

space.setText("5");

i=false;

}

else space.setText(space.getText()+'5');

}

if(e.getSource()==num4)

{

if(i==true)

{

space.setText("4");

i=false;

}

else space.setText(space.getText()+'4');

}

if(e.getSource()==num3)

{

if(i==true)

{

space.setText("3");

i=false;

}

else space.setText(space.getText()+'3');

}

if(e.getSource()==num2)

{

if(i==true)

{

space.setText("2");

i=false;

}

else space.setText(space.getText()+'2');

}

if(e.getSource()==num1)

{

if(i==true)

{

space.setText("1");

i=false;

}

else space.setText(space.getText()+'1');

}

if(e.getSource()==num0)

{

if(i==true)

{

space.setText("0");

i=false;

}

else space.setText(space.getText()+'0');

}

if(e.getSource()==zuok)

{

if(i==true)

{

space.setText("(");

i=false;

}

else space.setText(space.getText()+'(');

} if(e.getSource()==youk)

{

if(i==false)

space.setText(space.getText()+')');

}

if(e.getSource()==dian)

{

if(i==false)

space.setText(space.getText()+'.');

}

if(e.getSource()==plu)

{

space.setText(space.getText()+'+');

i=false;

}

if(e.getSource()==min)

{

space.setText(space.getText()+'-');

i=false;

}

if(e.getSource()==mul)

{

space.setText(space.getText()+'*');

i=false;

}

if(e.getSource()==div)

{

space.setText(space.getText()+'/');

i=false;

}

if(e.getSource()==equ)

{

space.setText(String.valueOf(Calculator(space.getText())));

i=true;

}

if(e.getSource()==cle)

{

space.setText("0");

i=true;

}

}

public double Calculator(String f)//科学计算

{

int i=0,j=0,k;

char c;

StringBuffer s=new StringBuffer();

s.append(f);

s.append('=');

String formula=s.toString();

char[] anArray;

anArray=new char[50];

StackCharacter mystack=new StackCharacter();

while(formula.charAt(i)!='=')

{

c=formula.charAt(i);

switch(c)

{

case '(': mystack.push(new Character(c));

i++;

break;

case ')':

while(mystack.peek().charValue()!='(')

{

anArray[j++]=mystack.pop().charValue();

}

mystack.pop();

i++;

break;

case '+':

case '-':

while(!mystack.empty()mystack.peek().charValue()!='(')

{

anArray[j++]=mystack.pop().charValue();

}

mystack.push(new Character(c));

i++;

break;

case '*':

case '/':

while(!mystack.empty()(mystack.peek().charValue()=='*'||mystack.peek().charValue()=='/'))

{

anArray[j++]=mystack.pop().charValue();

}

mystack.push(new Character(c));

i++;

break;

case' ': i++;

break;

default: while((c='0'c='9')||c=='.')

{

anArray[j++]=c;

i++;

c=formula.charAt(i);

}

anArray[j++]='#';

break;

}

}

while(!(mystack.empty()))

anArray[j++]=mystack.pop().charValue();

i=0;

int count;

double a,b,d;

StackDouble mystack1 =new StackDouble();

while(ij)

{

c=anArray[i];

switch(c)

{

case '+':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b+a;

mystack1.push(new Double(d));

i++;

break;

case '-':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b-a;

mystack1.push(new Double(d));

i++;

break;

case '*':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b*a;

mystack1.push(new Double(d));

i++;

break;

case '/':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

if(a!=0)

{

d=b/a;

mystack1.push(new Double(d));

i++;

}

else

{

System.out.println("Error!");

}

break;

default:

d=0;count=0;

while((c='0'c='9'))

{

d=10*d+c-'0';

i++;

c=anArray[i];

}

if(c=='.')

{

i++;

c=anArray[i];

while((c='0'c='9'))

{

count++;

d=d+(c-'0')/Math.pow(10,count);

i++;

c=anArray[i];

}

}

if(c=='#')

mystack1.push(new Double(d));

i++;

break;

}

}

return(mystack1.peek().doubleValue());

}

}

求几个JAVA小项目源代码,供自己学习参考

package com.test01;

import java.util.Scanner;

public class oop5 { public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// int x = 9;

// int y = 1;

int x = sc.nextInt();

int y = sc.nextInt();

int z;

z = add(x, y);

System.out.println("x的值为:" + x);

System.out.println("y的值为:" + y);

System.out.println("二者之和为:" + z);

}

/** 四种小算法 */

// 加法运算

public static int add(int a, int b) {

int c;

c = a + b;

return c;

}

// 减法运算

public static int jian(int d, int v) {

int m;

m = d - v;

return m;

}

// 乘积运算

public static int addAdd(int q, int w) {

int e;

e = q * w;

return e;

}

// 除法运算

public static int chu(int p, int k) {

int f;

f = p / k;

return f;

}

}

求JAVA入门小程序源代码

mport java.util.*;

public class HuiWen

{

public static void main(String[] args)

{

Scanner in=new Scanner(System.in);

System.out.println("please input a String:");

String st=in.nextLine();

String s=st.toLowerCase();

int i=0;

int j=s.length()-1;

boolean t=true;

char first=s.charAt(i);

char last=s.charAt(j);

for(;ij;)

{

while(!(first='a'first='z'))

{

i++;

first=s.charAt(i);

}

while(!(last='a'last='z'))

{

j--;

last=s.charAt(i);

}

if(i=j)

break;

if(first==last)

{

i++;

j--;

first=s.charAt(i);

last=s.charAt(j);

}

else

{

t=false;

break;

}

}

if(t)

System.out.println(st+"是回文串");

else

System.out.println(st+"不是回文串");

}

}

//判断是否为回文字串

//什么实用的程序,说具体点!你要计算器的程序吗?

//下面是计算器的程序,把分拿来吧!

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator

{

public static void main(String[] args)

{

CalculatorFrame frame=new CalculatorFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class CalculatorFrame extends JFrame

{

public CalculatorFrame()

{

setTitle("Calculator");

CalculatorPanel panel=new CalculatorPanel();

add(panel);

pack();

}

}

class CalculatorPanel extends Panel

{

public CalculatorPanel()

{

setLayout(new BorderLayout());

result=0;

lastCommand="=";

start=true;

flag=true;

display=new JButton("0");

display.setEnabled(false);

add(display,BorderLayout.NORTH);

ActionListener insert=new InsertAction();

ActionListener command=new CommandAction();

panel=new JPanel();

panel.setLayout(new GridLayout(4,5));

addButton("7",insert);

addButton("8",insert);

addButton("9",insert);

addButton("/",command);

addButton("CE",command);

addButton("4",insert);

addButton("5",insert);

addButton("6",insert);

addButton("*",command);

addButton("Backspace",command);

addButton("1",insert);

addButton("2",insert);

addButton("3",insert);

addButton("-",command);

addButton("sqrt",command);

addButton("0",insert);

addButton(".",insert);

addButton("=",command);

addButton("+",command);

addButton("1/x",command);

add(panel,BorderLayout.CENTER);

}

private void addButton(String label,ActionListener listener)

{

JButton button=new JButton(label);

button.addActionListener(listener);

panel.add(button);

}

private class InsertAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String input=event.getActionCommand();

if(startflag)

{

display.setText("");

start=false;

}

if(flag)

display.setText(display.getText()+input);

}

}

private class CommandAction implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

String command=event.getActionCommand();

if(command.equals("CE"))

{

display.setText("0");

start=true;

flag=true;

command="=";

}

else

if(startflag)

{

if(command.equals("-"))

{

display.setText(command);

start=false;

}

else

if((command.equals("1/x")||command.equals("sqrt"))flag)

calculate(Double.parseDouble(display.getText()),command);

else

if(flag)

lastCommand=command;

}

else

{

if(command.equals("Backspace")flag)

{

String s=display.getText();

char[] s1=s.toCharArray();

if(s.length()=2)

{

String s2=new String(s1,0,s.length()-1);

display.setText(s2);

}

else

{

display.setText("0");

start=true;

}

}

else if(flag)

{

calculate(Double.parseDouble(display.getText()),command);

lastCommand=command;

start=true;

}

}

}

}

public void calculate(double x,String command)

{

if(lastCommand.equals("+")) result+=x;

else if(lastCommand.equals("-")) result-=x;

else if(lastCommand.equals("/"))

{

if(x!=0)

result/=x;

else

{

display.setText("除数不能为0");

start=false;

flag=false;

return;

}

}

else if(lastCommand.equals("*")) result*=x;

else if(command.equals("1/x"))

{

if(x!=0)

result=1/x;

else

{

display.setText("除数不能为0");

start=false;

flag=false;

return;

}

}

else if(command.equals("sqrt"))

{

if(x=0)

result=Math.sqrt(x);

else

{

display.setText("函数输入无效");

start=false;

flag=false;

return;

}

}

else if(lastCommand.equals("=")) result=x;

display.setText(""+result);

}

private JButton display;

private JPanel panel;

private double result;

private String lastCommand;

private boolean start;

private boolean flag;

}


分享名称:java初级项目源代码 Java项目源代码
文章URL:http://cdweb.net/article/hpjjjd.html