import javax.swing.*;
創(chuàng)新互聯(lián)公司服務(wù)項目包括饒平網(wǎng)站建設(shè)、饒平網(wǎng)站制作、饒平網(wǎng)頁制作以及饒平網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,饒平網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到饒平省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 幫助運算的布爾變量
private double storedNumber; // 目前的結(jié)果
private char lastOperator; // 表示上一運算符
private JTextField operation; // 結(jié)果欄
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 運算符
private JButton[] numbers; // 數(shù)字
// 構(gòu)造者
public Calculator()
{
setTitle("Calculator");
// 初始化變量
dotExist = false; // 表示當前的數(shù)是否有小數(shù)點
operated = false; // 表示任意運算符是否被按下
equaled = false; // 表示等號是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口變量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 將窗口物體放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 對按鈕進行反應(yīng)的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 進行運算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并顯示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
public class Excer{
public static void main(String args[])
{
Excer ex=new Excer();
int x=0;
int y=0;
ex.math(x,y);
}
void math(int x,int y){
MyMath mm = new MyMath();
System.out.println("x="+x+" ,y="+y);
System.out.println("x+y="+mm.plus(x,y));
System.out.println("x-y="+mm.minus(x,y));
System.out.println("x*y="+mm.multi(x,y));
System.out.println("x/y="+mm.div(x,y));
}
}
class MyMath
{
int plus(int a,int b)
{
return(a+b);
}
int minus(int a,int b)
{
return(a-b);
}
int multi(int a,int b)
{
return(a*b);
}
float div(int a,int b)
{
return ((float)a/b);
}
}
public class MyComputer implements ActionListener{
private boolean appendnumber = false;
private boolean append = false;
private boolean flag = false;
private String temp1 = null;
private String op1 = null;
private JTextField jtf = new JTextField("0.",28);
public MyComputer(){
init();
}
private void init() {
/*
* 下面都是在畫計算機界面你想看也可以,
* 不看也行,具體計算在public void
* actionPerformed(ActionEvent e)
* 里
*/
JFrame frame = new JFrame("我的計算器");
JPanel panelTop = new JPanel();
panelTop.setLayout(new FlowLayout());
panelTop.add(jtf);
JPanel panelBotton = new JPanel();
String[] str = {"BackSpace","CE","C","+",
"7","8","9","-",
"4","5","6","*",
"1","2","3","/",
"0","+/-",".","="};
JButton[] jb = new JButton[str.length];
panelBotton.setLayout(new GridLayout(5,4));
for(int i = 0;i jb.length;i++)
{
jb[i] = new JButton(str[i]);
panelBotton.add(jb[i]);
jb[i].addActionListener(this);
}
frame.add(panelTop,BorderLayout.NORTH);
frame.add(panelBotton);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
new MyComputer();
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();//獲得button里的數(shù)據(jù)
/*
* "0123456789".indexOf(comm)!=-1是判斷button里獲得的數(shù)據(jù)
* 是0---9的情況
*/
if ("0123456789".indexOf(comm)!=-1) {
if(!appendnumber){
jtf.setText(comm);
appendnumber = true;
}else{
jtf.setText(jtf.getText()+comm);
}
}
/*
* 判斷button里獲得的數(shù)據(jù)是加減乘除的情況
*/
if("+-*/".indexOf(comm)!=-1){
//在遇到加減乘除的符號時我們先存儲剛輸入的值
temp1 = jtf.getText();
//然后存儲符號
op1 = comm;
/*
* 然后可以重新輸入數(shù)據(jù)到輸入框,所以這時候我們
* 不能追加數(shù)據(jù),設(shè)置追加數(shù)據(jù)標志為false
*/
appendnumber = false;
}
/*
* 判斷button里獲得的數(shù)據(jù)是等號的情況
*/
else if("=".equals(comm)) {
if(temp1==null||"".equals(temp1))return;
/*
* 遇到等號的時候,我們?nèi)〕鰟偛疟4娴臄?shù)據(jù)和當前輸入的數(shù)據(jù)做運算
*/
double num1 = Double.parseDouble(temp1);//轉(zhuǎn)化數(shù)據(jù)成double型的
double num2 = Double.parseDouble(jtf.getText());//轉(zhuǎn)化數(shù)據(jù)成double型的
//取出剛才保存的符號看是加減乘除的那種運算
if("+".equals(op1)){
num1+=num2;
}
if("-".equals(op1)){
num1-=num2;
}
if ("*".equals(op1)) {
num1*=num2;
}
if("/".equals(op1)){
num1/=num2;
}
jtf.setText(num1+"");
appendnumber = false;
}
/*
* 判斷button里獲得的數(shù)據(jù)是小數(shù)點的情況
*/
else if (".".equals(comm)) {
if(jtf.getText().indexOf(".")==-1){
append = true;
}
if (append) {
jtf.setText(jtf.getText()+".");
append = false;
appendnumber = true;
}
}
/*
* 判斷button里獲得的數(shù)據(jù)是在數(shù)據(jù)前面加正負號的情況
*/
else if ("+/-".equals(comm)) {
if(!flag){
jtf.setText("-"+jtf.getText());
flag = true;
}
else{
String result = jtf.getText().substring(1, jtf.getText().length());
jtf.setText(result);
flag = false;
}
}
/*
* 判斷button里獲得的數(shù)據(jù)是清除輸入框的情況
*/
else if ("C".equals(comm)) {
jtf.setText("0.");
appendnumber = false;
}
/*
* 判斷button里獲得的數(shù)據(jù)是清除輸入框的情況
*/
else if ("CE".equals(comm)) {
jtf.setText("0.");
appendnumber = false;
}
/*
* 判斷button里獲得的數(shù)據(jù)是輸入框的數(shù)據(jù)退一位的情況
*/
else if ("BackSpace".equals(comm)) {
if(jtf.getText()==null||"".equals(jtf.getText()))return;
String result = jtf.getText().substring(0, jtf.getText().length()-1);
jtf.setText(result+"");
appendnumber = true;
}
}
}
很簡單的東西??!你們?nèi)シ帜敲炊嗲闆r?。∵@個東西啊一個人寫半個小時就OK了,如果你會寫的話,其實初學(xué)者們別因為別人怎么怎么說就嚇倒,其實這寫起來都沒多少代碼的,都是些小東西而已。最重要的是你要搞懂來龍去脈就不難了,別聽一些人吹誰誰牛X這類的,其實你認真的去做,認真的去思考,你也不比他們差。
java編程實現(xiàn)分數(shù)的加減乘除運算的步驟如下:
1、打開eclipse,創(chuàng)建一個Java工程,在此工程里新建一個類;
2、在新建的類中,添加4個運算類;
3、在主方法中調(diào)用對應(yīng)的方法即可完成分數(shù)的加減乘除運算了。
具體實現(xiàn)代碼如下:
public?class?Demo?{
public?static?void?main(String[]?args)?{
System.out.println(jia(1,?2));
System.out.println(jian(1,?2));
System.out.println(cheng(1,?2));
System.out.println(chu(1,?2));
}
//加法運算
private?static?float?jia(float?x,float?y)?{
return?x?+?y;
}
//減法運算
private?static?float?jian(float?x,float?y)?{
return?x?-?y;
}
//乘法運算
private?static?float?cheng(float?x,float?y)?{
return?x?*?y;
}
//除法運算
private?static?float?chu(float?x,float?y)?{
return?x?/?y;
}
}
用Java編寫的實現(xiàn)加減乘除的程序如下
import?java.awt.BorderLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JComboBox;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
public?class?Calculator?extends?JFrame?implements?ActionListener{
JLabel?jl1=new?JLabel("第一個數(shù)");
JLabel?jl2=new?JLabel("運算符");
JLabel?jl3=new?JLabel("第二個數(shù)");
JLabel?jl4=new?JLabel("結(jié)果:");
JTextField?jtf1=new?JTextField(8);
JTextField?jtf2=new?JTextField(8);
JTextField?jtf3=new?JTextField(8);
String?a[]={"+","-","*","/"};
JComboBoxString?jcb=new?JComboBoxString(a);
JButton?jb1=new?JButton("計算");
JButton?jb2=new?JButton("退出");
JPanel?jp=new?JPanel();
Calculator(){
setTitle("計算器");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp.setLayout(null);
jl1.setBounds(30,?30,?80,?20);
jl2.setBounds(110,?30,?80,?20);
jl3.setBounds(190,?30,?80,?20);
jtf1.setBounds(30,?60,?70,?20);
jcb.setBounds(110,?60,?70,20);
jtf2.setBounds(190,?60,?70,?20);
jl4.setBounds(80,?110,?40,?20);
jtf3.setBounds(110,?110,?100,?20);
jb1.setBounds(60,?160,?60,?25);
jb2.setBounds(170,?160,?60,?25);
jp.add(jl1);jp.add(jl2);jp.add(jl3);
jp.add(jtf1);jp.add(jcb);jp.add(jtf2);
jp.add(jl4);jp.add(jtf3);
jp.add(jb1);jp.add(jb2);
add(jp,BorderLayout.CENTER);
setSize(300,?300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public?static?void?main(String[]?args)?{
new?Calculator();
}
@Override
public?void?actionPerformed(ActionEvent?ae)?{
if(ae.getSource()==jb1){
String?c=((String)?jcb.getSelectedItem()).trim();
if(jtf1.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,?"第一個數(shù)不能為空");
jtf1.requestFocus();
return;
}
if(jtf2.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,?"第二個數(shù)不能為空");
jtf2.requestFocus();
return;
}
double?num1=Double.parseDouble(jtf1.getText().trim());
double?num2=Double.parseDouble(jtf2.getText().trim());
double?num3?=?0;
switch(c){
case?"+":num3=num1+num2;break;
case?"-":num3=num1-num2;break;
case?"*":num3=num1*num2;break;
case?"/":
if(num2==0){
JOptionPane.showMessageDialog(this,?"除數(shù)不能為0");
jtf2.requestFocus();
return;
}else{
num3=num1/num2;
break;
}
}
jtf3.setText(String.valueOf(num3));
}
if(ae.getSource()==jb2){
System.exit(0);
}
}
}
運行結(jié)果
//JAVA編程:四則運算(接收用戶輸入的2個操作數(shù),和運算符),計算之后,輸出結(jié)果~~~~
import java.util.Scanner;
public class 四則運算 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入第一個數(shù)字:");
int a = sc.nextInt();
System.out.print("請輸入運算符號:");
String str = sc.next();
char ch = str.charAt(0);
System.out.print("請輸入第二個數(shù)字:");
int b = sc.nextInt();
switch(ch)
{
case '+':
System.out.println(a+"+"+ b + "="+(a+b));
break;
case '-':
System.out.println(a+"-"+ b+ "="+(a-b));
break;
case '*':
System.out.println(a+"*"+ b+ "="+(a*b));
break;
case '/':
if(b==0){
System.out.println("被除數(shù)為零,運算無意義!");
break;
}
else {
System.out.println(a+"/"+ b+ " = "+(a/b));
break;
}
default:
System.out.println("運算符是無意義字符!");
break;
}
}
}
望采納~~~~~~~~