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

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.

主站蜘蛛池模板: 岳普湖县| 随州市| 固始县| 宜黄县| 石河子市| 钟山县| 厦门市| 阿克苏市| 富阳市| 昭平县| 元朗区| 鄢陵县| 铜川市| 大石桥市| 江北区| 四子王旗| 吉水县| 湖北省| 泰顺县| 龙门县| 工布江达县| 黄石市| 中山市| 祁阳县| 神农架林区| 尚义县| 锡林浩特市| 丹东市| 塘沽区| 静乐县| 五家渠市| 剑河县| 永川市| 广水市| 桦南县| 环江| 潢川县| 寿阳县| 紫金县| 岱山县| 腾冲县|