加法java
❶ java實現長整數的加法運算
對於鏈表的理解,就是 A對象的一個成員變數 next 的類型是A,那麼就是鏈表了。
❷ 用 Java 寫一個兩個整數相加的程序
代碼如下:
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));
}
}
(2)加法java擴展閱讀
運算符
運算符是一些特殊的符號,主要用於數學函數、一些類型的賦值語句和邏輯比較方面。
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的值變為6,此時a的值為6,b的值為5
4、比較運算符
比較運算符屬於二元運算符,用於程序中的變數之間,變數和自變數之間以及其他類型的信息之間的比較。比較運算符的運算結果是boolean型。當運算符對應的關系成立時,運算的結果為true,否則為false。比較運算符共有6個,通常作為判斷的依據用於條件語句中。
運算符說明:
">"比較左方是否大於右方
"<"比較左方是否小於右方
"=="比較左方是否等於右方
"> = "比較左方是否大於等於右方
"<= "比較左方是否小於等於右方
"!= "比較左方是否不等於右方
參考鏈接:Java(計算機編程語言)_網路
❸ 加法的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里的加法運算符問題
首先helloworld類中是定義了全局變數 i=33 ;j=44,以及c1='a' ;c2='b'
然後在主函數里定義了一個對象hw是new 了一個helloworld類的對象,
然後通過「hw.」的形式調用了helloworld中定義的i;j;c1;c2
n相當於 i+j;
因為c是定義為int型的,而c1,c2是char類型的,所以c相當於『a』與『b』的ASCII碼相加計算……
通過斷點清楚的看到其值的計算情況。
呃,大概就是這個樣子。
❺ JAVA中實現計算加法的功能
你需要看一下swing事件的編寫,你需要調用文本框的getText方法得到裡面的數值,然後為「=」按鈕寫監聽事件,調用第三個文本框的setText方法顯示結果。我這里有個跟你界面相同的完善版,包括加減乘除運算的,我在代碼中用加粗著重突出了事件的那部分。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class AA extends JFrame
{
protected JTextField tFirst,tSecond,tResult,tEqual;
private JComboBox cFu;
private JButton jOk;
private String sFuHao;
protected String sFirst;
float fFirst,fSecond,fResult;
private String cFuHao[] = {
"+","-","*","/"
};
public AA()
{
super( "Calculator" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
tFirst = new JTextField( 10 );
container.add( tFirst );
cFu = new JComboBox( cFuHao );
cFu.setMaximumRowCount( 3 );
container.add( cFu );
tSecond = new JTextField( 10 );
container.add( tSecond );
tEqual = new JTextField( "=", 1 );
tEqual.setEditable( false );
container.add( tEqual );
tResult = new JTextField( 10 );
tResult.setEditable( false );
container.add( tResult );
jOk = new JButton( "calculate" );
container.add( jOk );
ButtonHandler handler = new ButtonHandler();
jOk.addActionListener( handler );
setSize( 600,80 );
setVisible( true );
}
public static void main( String args[] )
{
AA application = new AA();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
private class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent event )
{
sFuHao=(String)cFu.getSelectedItem();
if((tFirst.getText()).equals("#")||(tSecond.getText()).equals("#"))
System.exit(0);
fFirst = Float.parseFloat(tFirst.getText());
fSecond = Float.parseFloat(tSecond.getText());
if(sFuHao.equals("+"))
fResult = fFirst + fSecond;
else if(sFuHao.equals("-"))
fResult = fFirst - fSecond;
else if(sFuHao.equals("*"))
fResult = fFirst * fSecond;
else
fResult = fFirst / fSecond;
tResult.setText( String.valueOf(fResult) );
}
}
}
❻ 在java中,+什麼時候是連接符,什麼時候是加法
跟字元串做拼接的時候是連接符,數值類型運算的時候是加號。
需要注意string中連續的數字要整合成一個數字,加上括弧的時候運算順序要小心,特別注意括弧前有計算數字,比如5+(2+5)。
括弧後面有乘除法運算,比如5+(2+5)/2 括弧算完了要繼續算後面的除法,之後用得到的數字加上前面的加號運算符和『5』一起運算。
(6)加法java擴展閱讀
二元加法運算符「+」在兩個操作數都是數字或都是字元串時,計算結果是顯而易見的。加號「+」的轉換規則優先考慮字元串連接,如果其中一個操作數是字元串或者轉換為字元串的對象,另外一個操作數會轉換為字元串,加號講踐行字元串的連接操作。
如果兩個操作數都不是類字元串(string-like)的,那麼都講進行算數加法運算。
「+」操作符的行為表現為:
如果其中一個操作數是對象,則對象會遵循對象到原始值的轉換規則轉換為原始類值:日期對象通過toString()方法執行轉換,其他對象則通過valueOf()方法執行轉換(如果valueOf()方法返回一個原始值的話)。
由於多數對象都不具備可用的valueOf()方法,因此它們會通過toString()方法來執行轉換。
❼ JAVA程序實現加法的程序
JAVA程序實現加法的程序:
/**
*傳入兩個整數,返回兩個數的和
*@parama
*@paramb
*@return
*/
publicintadd(inta,intb){
returna+b;
}
❽ 怎麼用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編寫一個實現加法運算的程序
public class Plus {
public static void main(String[] args) {
String fristParam = args[0];
String secondParam = args[1];
String result = plus(fristParam, secondParam);
System.out.println(result);
}
private static String plus(String _sFristParam, String _sSecondParam) {
String sResult = null;
if (isVirtual(_sFristParam) || isVirtual(_sSecondParam)) {
sResult = virualPlus(_sFristParam, _sSecondParam);
}
else {
sResult = normalPlus(_sFristParam, _sSecondParam);
}
return sResult;
}
private static String normalPlus(String _sFristParam, String _sSecondParam) {
if (_sFristParam == null || _sSecondParam == null) {
return "對不起輸入有錯,請重新輸入";
}
int nFristParam = Integer.parseInt(_sFristParam);
int nSecondParam = Integer.parseInt(_sSecondParam);
int nResult = nFristParam + nSecondParam;
String sResult = String.valueOf(nResult);
return sResult;
}
private static String virualPlus(String _sFristParam, String _sSecondParam) {
String sFirstActual = getActual(_sFristParam);
String sFirstVirtual = getVirtual(_sFristParam);
String sSecondActual = getActual(_sSecondParam);
String sSecondVirtual = getVirtual(_sSecondParam);
String sResult = null;
int nFirstActual = 0;
int nFirstVirtual = 0;
int nSecondActual = 0;
int nSecondVirtual = 0;
int nVirtual = 0;
int nActual = 0;
if (sFirstActual == null || sFirstVirtual == null || sSecondActual == null ||
sSecondVirtual == null) {
return "對不起輸入的虛數有錯,請重新輸入";
}
nFirstActual = Integer.parseInt(sFirstActual);
nFirstVirtual = Integer.parseInt(sFirstVirtual);
nSecondActual = Integer.parseInt(sSecondActual);
nSecondVirtual = Integer.parseInt(sSecondVirtual);
nVirtual = nFirstVirtual + nSecondVirtual;
nActual = nFirstActual + nSecondActual;
String sVirtual = String.valueOf(nVirtual);
String sActual = String.valueOf(nActual);
sResult = sActual + "+" + sVirtual + "i";
return sResult;
}
private static String getVirtual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[1] != null) ? members[1].substring(0, 1) : null;
return sResult;
}
private static String getActual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[0] != null) ? members[0] : null;
return sResult;
}
private static boolean isVirtual(String _sParam) {
return (_sParam.indexOf("i") != -1) ? true : false;
}
}
自己改一下吧,基本上沒問題
❿ Java 加法計算問題
importjava.awt.*;
importjava.awt.event.*;
publicclassTest{
publicstaticvoidmain(String[]arga){
newMyFrame1().LaunchFrame();
}
}
classMyFrame1extendsFrame{
publicTextFieldt1,t2,t3;
publicvoidLaunchFrame(){
t1=newTextField(10);
t2=newTextField(10);
t3=newTextField(15);
Labellb=newLabel("+");
Buttonb=newButton("=");
MyMonitormt=newMyMonitor(this);
b.addActionListener(mt);
setLayout(newFlowLayout());
add(t1);
add(lb);
add(t2);
add(b);
add(t3);
pack();
setVisible(true);
}
}
{
privateMyFrame1mf=newMyFrame1();
publicMyMonitor(MyFrame1mf){
this.mf=mf;
}
publicvoidactionPerformed(ActionEvente){
intm1=Integer.parseInt(mf.t1.getText());
intm2=Integer.parseInt(mf.t2.getText());
intm3=m1+m2;
mf.t3.setText(""+(m3));
}
}
我修改了一下你的代碼,看看能不能發現問題。