Second, the basic data and calculation of Java

2.1 Keyword

Some Java language is given a special meaning of the word and can not be used.

2.2 Identifier

① is the name of some of our programmers write code in the process of self-definition.

② variable name, class name, function name, interface name and so on .....

③ main function is actually the main identifier is written just died.

Composition Rules 2.2.1 identifiers

① _ letters, numbers, underscores, consisting of $ $.

② where the number can not start.

③ strict case-sensitive.

④ Do not use the built-in Java class name and the keyword.

⑤ In order to improve reading identifier, try to play a meaningful name, usually expressed in English words, even if the alphabet.

Composition 2.2.2 specification identifier

There are three nomenclature

① small camel nomenclature:

If an identifier is a word that consists of several words, then in addition to the first letter of the first word lowercase, uppercase first letter of another word 

myName yourBoy 

What are the main applications in place: variable name function name

② large camel nomenclature:

If an identifier is composed of multiple words, the first letter of all words in uppercase

MyName YourBoy

What are the main applications in place: the class interface

③ all capital nomenclature:

If an identifier is composed of multiple words, then each word should be capitalized, connection between words _

MAX_VALUE  DEFAULT_SIZE

What are the main applications in place: define constants

2.3 Notes

Why should NOTE: The following code written in too many cases, the programmer easy to forget that the meaning of existence is to prompt the programmer comments

Note the code itself does not belong to the category, not into byte code file annotations

classification:

① single-line comments: // content change behavior until the stop is a comment

② multi-line comments: / * comment * content /

③ documentation comments: / ** * Notes content / (we can program documentation comments to make manual (page documents), javadoc.exe tools, front (class javadoc Demo01_01.java + public))

Note: a single line which can set a plurality of rows, a plurality of rows which can be set single line, multi-line multi-line can not be set.

2.4 hex constants and comments

Constant: that some of the data that appears directly in the program, while the independent variable is called

Constants are divided into:

① integer: binary integer (0b beginning), octal integer (0 at the beginning), decimal integer, integer hexadecimal (0x at the beginning).

② float: General fractional, decimal scientific notation.

③ characters: letters, symbols contained in single quotes by a digital data.

④ string: data by one or more numerals, letters, symbols contained in double quotes, our most commonly used constant type.

⑤ Boolean: it has only two values ​​true false true and false representation of these two concepts, true false is actually a representation of the exact keyword of a certain value.

⑥ empty: null keywords.

2.5 Variable data type

2.5.1 variables (data itself can be changed)

Essence: ① is mainly used to store some variable number of temporary calculation data among generated; 

② variable is essentially a temporary storage area in memory of a process in which certain of;  

③ each variable space will be randomly assigned address (real physical memory address) when it is created; 

④ data variable space must be maintained in the process of change is compatible (at least the same type);  

⑤ If the physical address directly to the access code by the variable data space, then it is possible to access not;  

⑥ In order to avoid uncertainty address, introduced the concept of variable names from the variable name - address table to ensure that the relationship between the variable name and variable (the underlying implementation);

⑦ In fact, the address of the variable space which is actually when you pointer in the C language in secondary schools;  

⑧ constant data in the variable space must be among you? No, all constants are constant pool among the variable space which is kept constant in the constant pool of addresses!

2.5.2 Data Types

purpose:

① In order to limit the current variable among the data stored (data variables which must be compatible)

② In order to limit the size of the current variable space

Classification of data types: basic data types, reference data types

Basic data types

Reference data types

Type Conversion

1, integer types of digital default type is int. But when with the byte, short and behind the char integer in the range of self-representation of numbers, then the integer is automatically transformed into byte, short or char type. Once in operation, however, then all the numbers will be converted to an int, and the result must be declared as int. Even if the two numbers are involved in computing the byte, or short range, the obtained result value is still within the short range or byte, it must be declared as int.

2.表达式中国的类型将严格保持和表达式中最高等级操作数相同的类型,即除了1的情况外,如果表达式最高级为long类型,那么结果也必须为long类型,如果为double类型,那么结果就必须为double类型

3.两个float进行运算,结果也为float类型

2.6 运算符

算数运算符

赋值运算符

基本的赋值运算符: =

扩展的赋值运算符:+=,-=,*=,/=,%=

比较运算符

逻辑运算符

位运算符

三目运算符

运算符的优先级

2.7例题及代码

import java.util.Scanner;//导包
public class Work1{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);//用户输入
        System.out.print("输入一个摄氏温度:");
        double Celsius = input.nextDouble();
        double Fahrenheit = (9.0/5)*Celsius+32;
        System.out.println(Celsius+"摄氏度是"+Fahrenheit+"华氏度");
    }
}

import java.util.Scanner;
public class Work3{
    public static void main(String[] agrs) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入一个数 0 到 1000: ");
        int number = input.nextInt();
        int numbera = number % 10;
        int numberb = number / 10;
        int numberc= numberb % 10;
        int numberd= numberc % 10;
        
        int sum = numbera  + numberc + numberd;
        System.out.println("各个数字相加是: " + sum );
    }
}

import java.util.Scanner;
public class Work5 {
public static void main(String[] args) {
double FirstMonthMoney,SecondMonthMoney,ThirdMonthMoney,FourthMonthMoney,FifthMonthMoney,
SixthMonthMoney,MoneyMonthSaveAmount,TestAccountValue;
System.out.print("输入每月储蓄金额:");
Scanner scanner = new Scanner(System.in);
MoneyMonthSaveAmount = MoneyInput.nextDouble();

FirstMonthMoney = 100*(1 + 0.00417);
SecondMonthMoney = (100 + FirstMonthMoney)*(1 + 0.00417);
ThirdMonthMoney = (100 + SecondMonthMoney)*(1 + 0.00417);
FourthMonthMoney = (100 + ThirdMonthMoney)*(1 + 0.00417);
FifthMonthMoney = (100 + FourthMonthMoney)*(1 + 0.00417);
SixthMonthMoney = (100 + FifthMonthMoney)*(1 + 0.00417);

System.out.println("第六个月后,帐户值为 $" + SixthMonthMoney);

}
}

import java.util.Scanner;
public class Work6{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入 x1 和 y1: ");
        System.out.print("输入 x2 和 y2: ");
        double b=scanner.nextDouble();
        double d=scanner.nextDouble();
        double a=scanner.nextDouble();
        double c=scanner.nextDouble();        
        double  y= (b-a)*(b-a)+(d-c)*(d-c);
        double distance = Math.pow(y, 0.5);//power 求开平方(0.5)
        System.out.print("两点之间的距离是: " + distance);
    }
}

 

 

发布了6 篇原创文章 · 获赞 0 · 访问量 131

Guess you like

Origin blog.csdn.net/q1220668269/article/details/104225104