简体   繁体   中英

Printing the value of a variable

Help ppl! I am debating about courses about my university degree, and I have doing some research about my choices. I am definitely going to study something to do with computing, so I have started practicing some software development in both Java and C++,. but my first Java one that I have developed does run but doesn't gave me the correct answer so I was wondering if someone would kindly help me by looking at it. Here's it and when it executed, only RESULT appears don't know why. Please take a look and help:

public class Math1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num1, num2, result;
        num1 = 90;
        num2 = 40;
        result = num1 - num2;
        System.out.println("result");
    }
}

When terminated, only RESULT I can see. Thank you in advance!

I'm wildly guessing what you're asking here, if you intend to see 50 , you need to change

System.out.println("result"); //This will print: result

to

System.out.println(result); //This will print: 50

Reason: "result" is the String literal "result" , if you want to refer to the variable result , you need to remove the quotes.

Besides that, your code seems correct to me.

You're outputting the string 'result' instead of the result variable. Use this: System.out.println(result);

Change System.out.println("result"); with this System.out.println(result);

You are using " " around your string. This, like said, it printing the string "result". Remove the quotes to reference the variable.

System.out.println(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM