问答详情
源自:3-3 Java中的赋值运算符

求教这个three=one+two为什么要写入?

public class HelloWorld{

    public static void main(String[] args) {

        int one = 10 ;

        int two = 20 ;

        int three = 0 ;

        three=one+two;

        /*↖(^ω^)↗加油哦    */

        

        System.out.println("three = one + two ==> "+three);

        three+=one;

        System.out.println("three += one ==> "+three);

        three-=one;

        System.out.println("three -= one ==> "+three);

        three*=one;

        System.out.println("three *= one ==> "+three);

        three/=one;

        System.out.println("three /= one ==> "+three);

        three%=one;

        System.out.println("three %= one ==> "+three);

        }

既然已经赋予了three=0了、为什么还写入、既然写入前边为什么不加入int

提问者:慕娘1537045 2017-02-05 11:37

个回答

  • higginson
    2017-02-05 11:55:32
    已采纳

    int three  = 0既声明了three又初始化赋值为0,后让one和two加和赋值给three,再输入three的值显示在控制台,这时候打印出来才是 three = one + two ==> 30,否则打印出来是 three = one + two ==> 0,与要求就不符合了

  • 慕粉2019272469
    2017-02-05 11:58:38

    因为最开始已经赋予了three 变量类型int,就无需再加入int。