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

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.

主站蜘蛛池模板: 太白县| 静宁县| 乐清市| 闻喜县| 民乐县| 教育| 醴陵市| 阿尔山市| 静海县| 清水县| 济源市| 葫芦岛市| 郑州市| 仙桃市| 开远市| 米泉市| 栖霞市| 罗城| 元阳县| 棋牌| 清流县| 枣庄市| 宜黄县| 庆云县| 天长市| 怀宁县| 常山县| 淳安县| 当涂县| 汪清县| 元谋县| 东兴市| 金堂县| 宜良县| 蓝山县| 奈曼旗| 贵定县| 临安市| 安康市| 永修县| 天气|