189 8069 5689

java计算加法代码,java实现加法运算

急求JAVA简单加法程序代码

写了个简单的,不知道是不是你想要的,里面的main方法作了一个简单的测试。几个变量我定义为类变量。 public class PubTest {

我们提供的服务有:成都做网站、网站设计、微信公众号开发、网站优化、网站认证、东港ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的东港网站制作公司

private float fvar1, fvar2;

private int ivar1;

public PubTest(float fval1, float fval2, int ival) {

fvar1 = fval1;

fvar2 = fval2;

ivar1 = ival;

}

public PubTest() {

fvar1 = fvar2 = 0.0f;

ivar1 = 0;

}

public float getFvar1() {

return fvar1;

}

public float getFvar2() {

return fvar2;

}

public int getIvar1() {

return ivar1;

}

public void sum() {

fvar2 += fvar1;

}

public static void main(String[] args) {

PubTest pubTest = new PubTest(3.0f, 5.0f, 1);

pubTest.sum();

System.out.println(pubTest.getFvar2());

}

}

java加法计算器代码

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {

initComponents();

}

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

num1 = new javax.swing.JTextField();

num2 = new javax.swing.JTextField();

result = new javax.swing.JTextField();

addBtn = new javax.swing.JButton();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

jMenu3 = new javax.swing.JMenu();

jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Num1:");

jLabel2.setText("Num2:");

jLabel3.setText("Num3:");

addBtn.setText("Add");

addBtn.addActionListener(new jisuanAC());

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(53, 53, 53)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(addBtn)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(31, 31, 31)

.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(jLabel3))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(result)

.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))

.addGap(168, 168, 168))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(19, 19, 19)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(61, 61, 61)

.addComponent(addBtn)

.addContainerGap(81, Short.MAX_VALUE))

);

jMenu1.setText("Operation");

jMenu3.setText("Add");

jMenu1.add(jMenu3);

jMenuBar1.add(jMenu1);

jMenu2.setText("Exit");

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// /editor-fold

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}

});

}

private class jisuanAC implements ActionListener

{

public void actionPerformed(ActionEvent e) {

if(e.getSource()== addBtn)

{

int number1 = Integer.parseInt(num1.getText());

int number2 = Integer.parseInt(num2.getText());

int rs = number1 + number2;

result.setText(String.valueOf(rs));

}

}

}

// Variables declaration - do not modify

private javax.swing.JButton addBtn;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenu jMenu2;

private javax.swing.JMenu jMenu3;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JPanel jPanel1;

private javax.swing.JTextField num1;

private javax.swing.JTextField num2;

private javax.swing.JTextField result;

}

这是只有一个加法的例子!希望帮到你

麻烦大神用java帮我做一个加法的运算,全部代码,谢谢

import java.util.Scanner;

public class AA{

public static void main(String[] args){

Scanner in=new Scanner(System.in);

System.out.println("请输入加数");

int a=in.nextInt();

System.out.println("请输入被加数");

int b=in.nextInnt();

int sum=a+b;

System.out.println("a"+"+b"+"="+sum);

}

}

怎么用java写加法

java中的加法使用+符号来计算最后的结果。

具体代码如下:

public class Demo {

public static void main(String[] args) {

int x = 3;

int y = 4;

int result = x + y;

System.out.println(result);

}

}

其中需要注意的是代码需写在主方法(main方法)中,否则程序无法运行;还有int型变量的初始值是0,即如果不给int型变量赋值的话,默认这个变量的值为0。

加法的Java 中的加法

Java中的加法很简答,使用运算符+表示。例如: publicclassadd{publicstaticvoidmain(String[]args){intx=5;inty=7;intsum=x+y;System.out.println("x和y的和为"+sum);}}上述代码声明了整数x和整数y,并分别赋值了5和7。最后在声明sum,赋值为x+y,并最终输出。

最终输出结果为12。


标题名称:java计算加法代码,java实现加法运算
转载来源:http://cdxtjz.cn/article/heepes.html

其他资讯