1、打開eclipse,創(chuàng)建一個Java工程,在此工程里新建一個類;
創(chuàng)新互聯建站是一家專業(yè)的成都網站建設公司,我們專注成都網站建設、成都網站設計、網絡營銷、企業(yè)網站建設,買友情鏈接,廣告投放平臺為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯網理念。從網站結構的規(guī)劃UI設計到用戶體驗提高,創(chuàng)新互聯力求做到盡善盡美。
2、在新建的類中,添加4個運算類;
3、在主方法中調用對應的方法即可完成分數的加減乘除運算了。
具體實現代碼如下:
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;
}
}
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中的加法使用+符號來計算最后的結果。
具體代碼如下:
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。
代碼如下:
public class Test {
public static int add(int a,int b){
return a+b;
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入第一個數");
int a = scanner.nextInt();
System.out.println("請輸入第二個數");
int b = scanner.nextInt();
System.out.println("和為:"+add(a,b));
}
}
擴展資料
運算符
運算符是一些特殊的符號,主要用于數學函數、一些類型的賦值語句和邏輯比較方面。
1、賦值運算符
賦值運算符以符號“=”表示,它是一個二元運算符(對兩個操作數作處理),其功能是將右方操作數所含的值賦給左方的操作數。
例如:
1? int a = 100;
2、算術運算符
運算符說明? :
“+” 加? ;“-” 減? ;“*” 乘 ;? “/”除 ; “%” 取余數
3、自增和自減運算符
自增和自減是單目運算符,可以放在操作元之前,也可以放在操作元之后。操作元必須是一個整型或浮點型變量。自增、自減運算符的作用是使變量的值增1或減1。放在操作元前面的自增、自減運算符,會先將變量的值加1或減1,然后再使該變量參與表達式的運算。放在操作元后面的自增、自減運算符,會先使變量參與表達式的運算,然后再將該變量的值加1或減1。
例如:
假設a=5
1? b=++a;//先將a的值加1,然后賦值給b,此時a的值為6,b的值為6
2? b=a++;//先將a的值賦值給b,再將a的值變?yōu)?,此時a的值為6,b的值為5
4、比較運算符
比較運算符屬于二元運算符,用于程序中的變量之間,變量和自變量之間以及其他類型的信息之間的比較。比較運算符的運算結果是boolean型。當運算符對應的關系成立時,運算的結果為true,否則為false。比較運算符共有6個,通常作為判斷的依據用于條件語句中。
運算符說明:
""比較左方是否大于右方
""?比較左方是否小于右方
"=="比較左方是否等于右方
" = "比較左方是否大于等于右方
"= "比較左方是否小于等于右方
"!= "比較左方是否不等于右方
參考鏈接:Java(計算機編程語言)_百度百科