java代码最简单的方法,java代码最简单的方法是什么
JAVA 编写计算器 要代码最简单的
//声明需要插入的包
成都创新互联公司秉承实现全网价值营销的理念,以专业定制企业官网,成都做网站、成都网站设计、成都外贸网站建设,微信小程序定制开发,网页设计制作,成都做手机网站,全网整合营销推广帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。
import java.awt.*;
import java.lang.Object;
import java.lang.String;
import javax.swing.*;
import java.awt.event.*;
import java.awt.TextComponent;
//声明一个主类jisuanqi,继续窗口类Frame
public class jisuanqi extends Frame{
//声明16个Button
Button anjian1, anjian2, anjian3, anjian4, anjian5, anjian6, anjian7, anjian8, anjian9, anjian10, anjian11, anjian12, anjian13, anjian14,anjian15,anjian16;
//声明文本框,用于接收数字和计算
TextField numText;
//声明面板容器,用于存放各种按钮
Panel anjianPanel;
//构造方法初始化按钮
jisuanqi(String title){
super(title);
anjian1=new Button("0");
anjian2=new Button("1");
anjian3=new Button("2");
anjian4=new Button("3");
anjian5=new Button("4");
anjian6=new Button("5");
anjian7=new Button("6");
anjian8=new Button("7");
anjian9=new Button("8");
anjian10=new Button("9");
anjian11=new Button("+");
anjian12=new Button("-");
anjian13=new Button("*");
anjian14=new Button("/");
anjian15=new Button("=");
anjian16=new Button("清空");
//初始化文本框,将文本的值设置为“0”
numText=new TextField("0");
//实例化面板;
anjianPanel=new Panel();
//设置按钮
anjianPanel.setLayout(new FlowLayout());
//实例化按钮
anjian1.addActionListener(new anjianAction());
anjian2.addActionListener(new anjianAction());
anjian3.addActionListener(new anjianAction());
anjian4.addActionListener(new anjianAction());
anjian5.addActionListener(new anjianAction());
anjian6.addActionListener(new anjianAction());
anjian7.addActionListener(new anjianAction());
anjian8.addActionListener(new anjianAction());
anjian9.addActionListener(new anjianAction());
anjian10.addActionListener(new anjianAction());
anjian11.addActionListener(new anjianAction());
anjian12.addActionListener(new anjianAction());
anjian13.addActionListener(new anjianAction());
anjian14.addActionListener(new anjianAction());
anjian15.addActionListener(new anjianAction());
anjian16.addActionListener(new anjianAction());
//将各个按钮添加到面板容器中
anjianPanel.add(anjian1);
anjianPanel.add(anjian2);
anjianPanel.add(anjian3);
anjianPanel.add(anjian4);
anjianPanel.add(anjian5);
anjianPanel.add(anjian6);
anjianPanel.add(anjian7);
anjianPanel.add(anjian8);
anjianPanel.add(anjian9);
anjianPanel.add(anjian10);
anjianPanel.add(anjian11);
anjianPanel.add(anjian12);
anjianPanel.add(anjian13);
anjianPanel.add(anjian14);
anjianPanel.add(anjian15);
anjianPanel.add(anjian16);
//为文本框添加事件
numText.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
if(numText.getText().indexOf("0",0)!=-1){
numText.getText().replace("0","");
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//将文本框放置在顶部
add(numText,BorderLayout.NORTH);
//添加按钮
add(anjianPanel,BorderLayout.CENTER);
//设置文本框属性
numText.setEditable(false);
}
//没有任何操作的值
int num=0;
//numStr是文本框的值
String numStr=null;
Boolean chinage=false;
//设置按钮
public int check(){
numStr = numText.getText();
if (numStr.indexOf("+") == 0) {
numStr = numStr.replace('+', '0');
num = num + Integer.parseInt(numStr);
return num;
}else
if (numStr.indexOf("-") != -1) {
numStr = numStr.replace("-", "0");
num = num - Integer.parseInt(numStr);
return num;
}
else
if(numStr.indexOf("*")!=-1){
numStr=numStr.replace('*','0');
num = num * Integer.parseInt(numStr);
return num;
}
else
if(numStr.indexOf("/")!=-1){
numStr=numStr.replace('/','0');
try{
num = num / Integer.parseInt(numStr);
return num;
}catch(ArithmeticException e){
JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);
return num;}}
else
return num=Integer.parseInt(numStr);}
//创建事件
private class anjianAction implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getActionCommand()=="0"){
if(!numText.getText().equals("0"))
numText.setText(numText.getText()+0);}
if(event.getActionCommand()=="1")
numText.setText(numText.getText()+1);
if(event.getActionCommand()=="2")
numText.setText(numText.getText()+2);
if(event.getActionCommand()=="3")
numText.setText(numText.getText()+3);
if(event.getActionCommand()=="4")
numText.setText(numText.getText()+4);
if(event.getActionCommand()=="5")
numText.setText(numText.getText()+5);
if(event.getActionCommand()=="6")
numText.setText(numText.getText()+6);
if(event.getActionCommand()=="7")
numText.setText(numText.getText()+7);
if(event.getActionCommand()=="8")
numText.setText(numText.getText()+8);
if(event.getActionCommand()=="9")
numText.setText(numText.getText()+9);
if(event.getActionCommand()=="清空"){
numText.setText("0");
num=0;}
if(event.getActionCommand()=="+"){
//调用check();方法
check();
numText.setText("+");}
if(event.getActionCommand()=="-"){
check();
numText.setText("-");}
if(event.getActionCommand()=="*"){
check();
numText.setText("*");}
if(event.getActionCommand()=="/"){
if(numStr=="/0"){
JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);}
check();
numText.setText("/");}
if(event.getActionCommand()=="="){
check();
numText.setText(""+num);}}}
public static void main(String[] args){
jisuanqi jisuanqi=new jisuanqi("计数器");
jisuanqi.setSize(300,280);
//设置窗口大小
jisuanqi.setLocation(550,350);
//设置界面属性
jisuanqi.setVisible(true);
}}
如有疑问请继续追问,我将竭尽全力为您解答
JAVA 编写计算器 要代码最简单的
学java的时候自己编的,很简单,能够连续输入计算式后进行计算
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**简易计算器,能够进行简单的计算
*
* @see 2008.12.9
*/
public class CalculatorA
implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
// private JButton backButton;
String result="";//保存结果
StringBuilder sb = new StringBuilder();//保存要进行的计算式
int x = 0; //用来判断上一次的事件类型
String str = "123+456-789*0.=/";
ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算
public CalculatorA(){
frame = new JFrame("我的计算器v1.1");
frame.setLocation(300,300);
field = new JTextField(25);
allButtons = new JButton[16];
for(int i=0;iallButtons.length;i++){
allButtons[i]= new JButton(str.substring(i,i+1));
}
clearButton = new JButton("CLEAR");
// backButton = new JButton("——");
init();
setFondAndColor();
addEventHander();
}
public void init(){
frame.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(field);
for(int i=0;iallButtons.length;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
//southPanel.add(backButton);
frame.add(northPanel,BorderLayout.NORTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(southPanel,BorderLayout.SOUTH);
}
//设置输入字体
public void setFondAndColor(){
field.setFont(new Font("宋体",Font.BOLD,24));
field.setBackground(new Color(100,200,200));
field.setForeground(Color.RED);
//设置字体从右起始
field.setHorizontalAlignment(JTextField.RIGHT);
}
public void showMi(){
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHander(){
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(this);
// backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();//取得当前事件返回的值
if("0123456789.".indexOf(str)!=-1){
if(x == 0){ //当x为0时表示还没有进行输入
result=str;
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 1){
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 2){
sb.delete(0,sb.length());
result = result+str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 3){
result = str;
sb.delete(0,sb.length());
arrayList.clear();
field.setText(str);
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
result = str;
sb.append(str);
field.setText(str);
x = 1;
}
else{
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
}
else if("+*-/".indexOf(str)!=-1){
if(x == 0){
field.setText("");
x = 2;
}
else if(x == 1){
result = result + str;
arrayList.add(sb.toString());
arrayList.add(str);
sb.append(str);
field.setText(result);
x = 2;
}
else if(x == 2){
x = 2;
}
else if(x == 3){
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 2;
}
else{
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
}
else if("=".equals(str)){
if(x == 0){
field.setText("0");
arrayList.clear();
result = "0";
x = 3;
}
else if(x == 1){
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
else if(x == 2){
field.setText("数据格式错误.....");
arrayList.clear();
x = 0;
}
else if(x == 3){
field.setText(result);
x = 3;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 3;
}
else {
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
}
else if("CLEAR".equals(str)){
arrayList.clear();
field.setText("0");
arrayList.add("0");
x = 4;
}
else{
if(result.length()1){
result = result.substring(0,result.length()-1);
if(sb.length()0){
sb.delete(sb.length()-1,sb.length());
}
else {
sb.delete(0,1);
}
field.setText(result);
x = 5;
}
else{
result = "";
sb.delete(0,sb.length());
arrayList.clear();
field.setText("0");
x = 0;
}
}
}
public static ArrayListString getResult(ArrayListString list){
String res = null;
String[] s = {"/","*","-","+"};
int i=0;
if(list.size()1){
for(;is.length;){
if(s[i].equals("/")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));
//本地的数据格式
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("*")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("-")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getNumberInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else {
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
}
}
return list;
}
//对数字字符串进行排除不必要符号
public static String getChange(String res){
String s_temp = "";
char[] c = new char[res.length()];
for(int k=0;kc.length;k++){
c[k] = res.charAt(k);
}
for(int k=0;kc.length;k++){
if((c[k]= '0' c[k]= '9')|| c[k] == '.'){
s_temp += c[k];
}
}
return s_temp;
}
public static void main(String[] args){
new CalculatorA().showMi();
}
}
给段最简单的java代码 让我新手看一下
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
分享名称:java代码最简单的方法,java代码最简单的方法是什么
分享网址:http://azwzsj.com/article/hchihd.html