我開始學習Java ..我很興奮。
首先代碼不返回的結果我想..添加之間有什麼區別?
import java.util.Scanner;
public class Yusuf
{
public static void main(String args[])
{
Scanner text = new Scanner(System.in);
int a,b;
System.out.print("Enter first number:");
a = text.nextInt();
System.out.print("Enter second number:");
b = text.nextInt();
System.out.print("a + b = " + a+b);
}
}
這段代碼的結果是 「A + B = 1525」(舉例來說,如果A = 15和B = 25(我給隨機數))
上面爲什麼代碼沒有此代碼的工作,例如:
import java.util.Scanner;
public class Yusuf
{
public static void main(String args[])
{
Scanner text = new Scanner(System.in);
int a,b,c;
System.out.print("Enter first number:");
a = text.nextInt();
System.out.print("Enter second number:");
b = text.nextInt();
c = a+b;
System.out.print("a + b = " + c);
}
}
此代碼相同的數字返回40 .. 的區別是什麼?絕對我需要使用不同的變量?
在某種意義上的操作優先權.. – Joseph 2012-01-31 01:29:02
@Joseph - 實際上,這是在正常的操作符優先級/優先級之後。 – 2012-01-31 01:31:25
「運算符優先級」是編程中通常使用的術語。首先對parens中的事物進行評估,然後將某些運算符先於其他人進行評估,如下所列:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html - 請注意,您可以「嵌套」括號,例如'(a + b)*(x /(y-6))' - 最內層的表達式首先被評估... – nnnnnn 2012-01-31 01:34:17