官术网_书友最值得收藏!

Integer variables

To begin, let's create a new project in NetBeans. I'm going to call mine Variables, and this time we'll allow NetBeans to create the main class for us so that we can get to coding as quickly as possible. We need to delete all the comments that are created automatically by NetBeans when we create our new project, just to keep everything as readable as possible, then we'll be good to go:

The first computers were little more than calculators, and Java, of course, retains this functionality. For example, Java can evaluate 1+1, which will evaluate to 2, of course. However, Java is pretty complicated and designed to do a lot of different things, so we need to provide context to our commands. Here, we tell Java that we'd like it to print the result of 1+1:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        System.out.println(1+1); 
    } 
     
} 

Our preceding program will run as expected:

In addition to some others, Java can perform all the basic arithmetic operations. It can do addition, subtraction, multiplication (for which we use *, not X on our keyboard), and division. If we run the following program with the input of 2 and 3, we'll see four println() commands, all of which will give the proper result of the calculations. We can, of course, change these numbers to be any combination of numbers we see fit:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        System.out.println(2+3); 
        System.out.println(2-3); 
        System.out.println(2*3); 
        System.out.println(2/3); 
    } 
} 

The following is the output of the preceding code:

Changing these lines manually is kind of a pain and quickly becomes infeasible if we're writing very complicated programs or dynamic programs that take user input.

主站蜘蛛池模板: 富源县| 韩城市| 张家川| 方正县| 乌拉特前旗| 古蔺县| 洪湖市| 泌阳县| 北安市| 石棉县| 和林格尔县| 富川| 高唐县| 杭锦旗| 施甸县| 阿拉尔市| 澄迈县| 东乌珠穆沁旗| 嫩江县| 乐陵市| 宜川县| 临澧县| 和静县| 承德县| 新乐市| 栾城县| 白银市| 潞城市| 河北省| 东乡| 宁强县| 宁远县| 汨罗市| 昌黎县| 曲阳县| 仙居县| 广州市| 尉氏县| 崇仁县| 金寨县| 浮山县|