當前位置:首頁 » 編程語言 » java連乘

java連乘

發布時間: 2023-07-12 04:53:15

❶ 矩陣連乘的問題java.lang.: 3

int m[][]=new int[3][3];
int s[][]=new int[3][3];

數組[3] 下標為 0-2

所以MatrixChain方法中的
for(int i=1;i<=n;i++){
m[i][i]=0;
}
這里的 m[3][3] 就越界了

❷ java編一個計算器的代碼

界面漂亮堪御尺比系統自帶穗拆兆計算器,功能完美加減乘除開平方等等猜租全部具備,還有清零按鈕,小數點的使用,連加連乘功能完全參考系統官方計算器經過長期調試改進而成,馬上拷貝代碼拿去試試看吧,絕不後悔!

代碼如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Counter {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();
}
}

class CounterFrame extends JFrame {

public CounterFrame() {

JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();

menuFile.setText("文件");
menuFile1.setText("編輯");
menuFile2.setText("查看");
menuFile3.setText("幫助");

menuFileExit.setText("退出");

menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
menuBar.add(menuFile3);

setTitle("計算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}

protected void windowClosed() {

System.exit(0);
}
}

class Tool {

public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;

}

public JTextField getfield()
{
return (this.field);
}

}

class Allpanel extends JPanel {

public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}

class Centercenter extends JPanel {

static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();

static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;

static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";

/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};

for(int i=0;i<20;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);

if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();

if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}

else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();

}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}

}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}

begin="no";
to="yes";

}
});
}

if(i==17)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}

if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}

if(!Vec.contains(".")&&!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}

}
});
}

if(i==18)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
jq=Double.parseDouble(ma);

if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}

if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}

else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}

vc.add("a");
vc1.add("v1");
to="/";
}
});
}

if(i==4)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
cq=Double.parseDouble(ma);

text.setText(String.valueOf(Math.sqrt(cq)));
}
});
}

if(i==8)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}

if(i==19)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}

else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}

else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}

else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}

else
{
jg=String.valueOf(dy);
}
}

text.setText(jg);

Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;

}
});
}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
this.add(jb1);
this.add(jb2);
this.add(jb3);

jb1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
String back=Tool.getinstance().getfield().getText();
text.setText(backmethod(back));
Centercenter.Vec.remove(Centercenter.Vec.size()-1);

}

});

jb3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{

text.setText("0.");
Centercenter.Vec.clear();
Centercenter.Vec.add(".");
Centercenter.vc.add("a");
Centercenter.begin="yes";
Centercenter.vc1.clear();
Centercenter.what=null;
Centercenter.tool=0;

}

});

}

public String backmethod(String str)
{
return str.substring(0,str.length()-1);
}

}

class Centerpanel extends JPanel {

public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}

class Centerwest extends JPanel {

public Centerwest() {
this.setLayout(new GridLayout(4,1,3,3));
this.add(new JButton("MC"));
this.add(new JButton("MR"));
this.add(new JButton("MS"));
this.add(new JButton("M+"));

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}

---------------------------------------------------------------------------
=============《按你要求特意後改過的最簡單功能的代碼如下》========================

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Counter2 {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();
}
}

class CounterFrame extends JFrame {

public CounterFrame() {

setTitle("計算器");
setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}

protected void windowClosed() {

System.exit(0);
}
}

class Tool {

public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;

}

public JTextField getfield()
{
return (this.field);
}

}

class Allpanel extends JPanel {

public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}

class Centercenter extends JPanel {

static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();

static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;

static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";

/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};

for(int i=0;i<16;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);

if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();

if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}

else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();

}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}

}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}

begin="no";
to="yes";

}
});
}

if(i==14)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}

if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}

if(!Vec.contains(".")&&!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}

}
});
}

if(i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==11)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
jq=Double.parseDouble(ma);

if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}

if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}

else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}

vc.add("a");
vc1.add("v1");
to="/";
}
});
}

if(i==7)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}

if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)

{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}

else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}

else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}

else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}

else
{
jg=String.valueOf(dy);
}
}

text.setText(jg);

Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;

}
});
}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

}

}

class Centerpanel extends JPanel {

public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}

class Centerwest extends JPanel {

public Centerwest() {

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}

------------------------------------------------------------

才子_輝祝您愉快!

熱點內容
什麼是白標和伺服器 發布:2025-02-08 21:15:50 瀏覽:481
租完伺服器怎麼搭建ip 發布:2025-02-08 21:11:47 瀏覽:394
c語言賦值後 發布:2025-02-08 21:08:40 瀏覽:756
dosphp 發布:2025-02-08 21:01:27 瀏覽:703
sm3雜湊演算法 發布:2025-02-08 20:55:00 瀏覽:286
抽獎源碼帶後台 發布:2025-02-08 20:33:54 瀏覽:226
歐博中央空調原始密碼是多少 發布:2025-02-08 20:33:47 瀏覽:335
運動使人快樂緩解壓力 發布:2025-02-08 20:27:01 瀏覽:98
linux命令大文件 發布:2025-02-08 20:25:06 瀏覽:897
C蟻群演算法 發布:2025-02-08 20:21:25 瀏覽:513