#1.1 第一個Java程序java
package my.hava.javabasis.lesson; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_1 { public static void main(String [] args) { System.out.println("Hello World"); } }
程序輸出編程
Hello World Process finished with exit code 0
#1.2 用變量作計算 ##1.2.1 讀輸入 ###如何去讀架構
package my.hava.javabasis.lesson; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("中文"); //讀取用戶輸入 Scanner in = new Scanner(System.in); System.out.println(in.nextLine()); } }
###課堂題目 每次輸入的內容只有在按下回車(Enter)鍵以後纔會被程序讀到。
A.√
B.×
正確答案: Aapp
package my.hava.javabasis.lesson; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("中文"); //讀取用戶輸入 Scanner in = new Scanner(System.in); System.out.println("echo:" + in.nextLine()); System.out.println(2+3+"=2+3=" + (2+3)); } }
程序輸出less
中文 hello world echo:hello world 5=2+3=5 Process finished with exit code 0
##1.2.2 變量測試
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("中文"); //讀取用戶輸入 Scanner in = new Scanner(System.in); // System.out.println("echo:" + in.nextLine()); int price = in.nextInt(); System.out.println("100-" + price + "=" + (100 - price)); } }
輸出ui
中文 33 100-33=67 Process finished with exit code 0
###題目 int price;
這句定義了一個變量
A.√
B.×
正確答案: A 你選對了code
###變量定義對象
以下爲例子圖片
int price; int amount; int price,amount;
###變量的名稱
###Java保留字
###變量類型
int price = 0;
##1.2.3 賦值
###異常
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("中文"); //讀取用戶輸入 Scanner in = new Scanner(System.in); // System.out.println("echo:" + in.nextLine()); //變量初始化 int price = 0, amount = 0; System.out.println(price); price = in.nextInt(); System.out.println("100-" + price + "=" + (100 - price)); } }
輸出異常
中文 0 price Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at my.hava.javabasis.lesson1.Lesson1_2.main(Lesson1_2.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Process finished with exit code 1
###代碼
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("你好"); //讀取用戶輸入 Scanner in = new Scanner(System.in); // System.out.println("echo:" + in.nextLine()); //變量初始化 int amount = 100; int price = 0; System.out.println("[amount]:" + amount); System.out.println("[price]:" + price); price = in.nextInt(); System.out.println(amount + "-" + price + "=" + (amount - price)); } }
###題目 當amount的值爲100,price的值爲20,如下哪種寫法能輸出:100-20=80
A.System.out.println("amount-price="+amount-price);
B.System.out.println(amount+"-"+price+"="+(amount-price));
C.System.out.println("amount-"+price+"="+amount-price);
D.System.out.println(amount-price=(amount-price)); 正確答案: B
###程序
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by zhanpeng on 2016/10/10. */ public class Lesson1_2 { public static void main(String [] args) { System.out.println("你好"); //讀取用戶輸入 Scanner in = new Scanner(System.in); // System.out.println("echo:" + in.nextLine()); //變量初始化 // final int amount = 100; int amount = 100; int price = 0; System.out.println("[amount]:" + amount); System.out.println("[price]:" + price); System.out.println("請輸入票面:"); amount = in.nextInt(); System.out.println("請輸入金額:"); price = in.nextInt(); System.out.println(amount + "-" + price + "=" + (amount - price)); } }
輸出結果
你好 [amount]:100 [price]:0 請輸入票面: 100 請輸入金額: 50 100-50=50 Process finished with exit code 0
##變量課件 ###讀輸入
Scanner in = new Scanner(System.in); System.out.println(in.nextLine());
###運行
###讀入一行文字
System.out.println(in.nextLine());
###字符串的+
System.out.println("Hello" + " world.");//Hello world. System.out.println("Hello" + 2);//Hello2 System.out.println("Hello" + 2 + 3);//Hello5 System.out.println(2 + 3 + "Hello");//5Hello
###try
###找零計算
Scanner in = new Scanner(System.in); int price = 0; price = in.nextInt(); int change = 100 - price; System.out.println(change);
###變量
int price = 0;
###變量定義
以下爲例子
int price; int amount; int price,amount;
###變量的名字
###Java保留字
###quiz
###賦值和初始化
=
是一個賦值運算符,表示將=
右邊的值賦給左邊的變量。###賦值 和數學不一樣,a=b在數學中表示關係,即a和b的值同樣;而在Java中,a=b表示要求計算機作一個動做:將b的值賦給a。關係是靜態的,而動做是動態的。在數學中,a=b與b=a是等價的,而在Java中,二者的意思徹底相反
###初始化 當賦值發生在定義變量的時候,就像程序中的第7行那樣,就是變量的初始化。雖然Java語言並無強制要求全部的變量都在定義的地方作初始化,可是要求全部的變量在第一次被使用(出如今賦值運算符的右邊)以前被賦值
###變量初始化
###插入
回答 price == 0,amount == 100,age == 0
###讀整數
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at my.hava.javabasis.lesson1.Lesson1_2.main(Lesson1_2.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
###表達式
###try
###變量類型
###常量
###final
###try
###tips
#1.3 表達式(浮點數、優先級和類型轉換) ##浮點數 ###身高5尺7寸?
###計算身高的程序 - 問題代碼
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by yanfa on 2016/10/10. */ public class Lesson1_3 { public static void main(String [] args){ int foot; //英尺 int inch; //英寸 Scanner in = new Scanner(System.in); System.out.println("請輸入身高:"); System.out.print("英尺:"); foot = in.nextInt(); System.out.print("英寸:"); inch = in.nextInt(); double result = (foot + inch/12) * 0.3048; System.out.println("身高:" + result); } }
錯誤輸出
請輸入身高: 英尺:5 英寸:7 身高:1.524
###爲何是1.524
###課堂測試 在Java中10/3的結果是?
正確答案:3
###計算身高的程序 - 正確代碼 **注意:**在作除法的時候先進行強制類型轉換
package my.hava.javabasis.lesson1; import java.util.Scanner; /** * Created by yanfa on 2016/10/10. */ public class Lesson1_3 { public static void main(String [] args){ int foot; //英尺 int inch; //英寸 Scanner in = new Scanner(System.in); System.out.println("請輸入身高:"); System.out.print("英尺:"); foot = in.nextInt(); System.out.print("英寸:"); inch = in.nextInt(); double result = (foot + (double)inch/12) * 0.3048; System.out.println("身高:" + result); } }
輸出結果
請輸入身高: 英尺:5 英寸:7 身高:1.7018
###浮點數
###程序改進
###double
雙精度浮點數
的第一個單詞,人們用來表示浮點數類型###浮點運算的精度
0.09999999999999987
###整數
###討論
**回答:**正確,Java程序會自動從低精度轉換成高精度。
###隨堂
###單目運算符
int a = 10; int b = -20; System.out.println(a * -b);
###結合關係
result = a = b = 3 + c; result = 2; result = (result = result * 2) * 6 * (result = 3 + result); //上面的式子先計算了左邊的result=,獲得result=4,而後在計算右邊的result=,獲得result=7。則最終結果是168
###插入
###強制類型轉換
###隨堂測試 如下哪句能把浮點數轉換成整數?
A. (int)12.4
B. int(12.4)
C. int 12.4
D. 12.4=int 正確答案: A 你選對了
###強制類型轉換
int i = 32 / 3.0; int i = (int)(32 / 3.0);
###身高?
###強制類型轉換
double b = 10.3; int a = (int)b;
###強制類型轉換
double a = 1.0; double b = 2.0; int i = (int)a /b;//int i = (int)(a/b);
double a = 1.0; double b = 2.0; double d = (double) (a /b );