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

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.

主站蜘蛛池模板: 徐闻县| 辛集市| 祁阳县| 潼关县| 泸定县| 肇源县| 兰西县| 临安市| 德阳市| 阿拉尔市| 曲阜市| 柳河县| 桃园市| 仲巴县| 随州市| 济宁市| 霍林郭勒市| 泰兴市| 二手房| 伊通| 四平市| 岫岩| 上思县| 偃师市| 民乐县| 白山市| 阿拉善盟| 江陵县| 武宣县| 南岸区| 阿荣旗| 磐石市| 休宁县| 庆元县| 许昌市| 门源| 宣化县| 银川市| 潜江市| 兴隆县| 克什克腾旗|