Java variables 01
2016-08-19 10:39 yojiaku 阅读(256) 评论(0) 编辑 收藏 举报Understanding :
At the very beginning, I should understand clearly that every program can be understood as a progress that make a record of some inputs in order to calculate, then output the result.
And the most important thing to learn Java is you have the ability to read codes.
Then let me begin to learn Java! o(* ̄▽ ̄*)o
The first Java program, of course, is "Hello World!":(IDE:eclipse)
The codes:
package hello; public class Hello { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); } }
Let me show the catalogue:
We can see our Hello.java program is in the package "hello", (Up to now, I don't know why there is a package, however, I can learn why later) It can also throw the package in this program.
Let me add something into this easy program in order to make it more fantastic —— the "yesman" program.
The codes:
package hello; import java.util.Scanner; // 这里就出现了调用import,类似于C里的include public class Hello { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); Scanner in = new Scanner(System.in); // 输入语句 System.out.println(in.nextLine()); //输入一整行 } }
The Output:
We can learn something basic about string from "yesman" program:
System.out.println("2+3="+5); ===> 2+3=5
System.out.println("2+3="+2+3); ===> 2+3=23
System.out.println("2+3="+(2+3)); ===> 2+3=5 (字符串+数字===>全部看作字符串)
System.out.println(2+3+"=2+3="+(2+3)); ===> 5=2+3=5 (运算+字符串+运算)===> 先计算后当作字符串输出)
System.out.println(2+3+"=2+3="+2+3); ===> 5=2+3=23
And we can learn how to define variables from this program:
package hello; import java.util.Scanner; public class Hello { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int price; price = in.nextInt(); System.out.println("100-"+price+"="+(100-price)); } }
The Output:
define constants: final int amount = 100; (final为常量关键字)
define float number: double variables;
Change double type to int type:(把double类型强制转化为int类型) (int)meter; (meter 以前是double类型)
Attention:
We often use this sentence to judge two doubles are equal: Math.abs(f1 - f2) < 0.00001;