開發軟件_Scanner鍵盤錄入_Random隨機數_流程控制語句
- 一、開發軟件idea(MAC版)
- 1、軟件安裝-安裝社區版
- 2、中英文設置
- 3、保存時格式化配置
- 4、注釋和代碼對不齊
- 5、idea快捷鍵
- 二、鍵盤錄入--Scanner
- 1、next和nextInt
- 2、next和nextLine區別
- 三、Random隨機數
- 1、使用
- 2、隨機范圍
- 四、流程控制語句
- 1、switch語句
- 1、基本使用
- 2、case穿透性
- 3、執行語句重復時
- 2、if語句
- 1、if格式
- 2、if else格式
- 3、if else if else格式
- 3、for循環語句
- 練習1
- 練習2
- 練習3
- 4、while循環語句
- 5、do....while循環語句
- 6、循環控制關鍵字
- 7、死循環
- 8、嵌套循環
- 練習1
- 綜合練習
一、開發軟件idea(MAC版)
1、軟件安裝-安裝社區版
idea社區版(免費)
2、中英文設置
3、保存時格式化配置
4、注釋和代碼對不齊
解決方法
設置-Editor-Code Style- Java-Code Generation
設置-Editor-Code Style- XML-Code Generation
5、idea快捷鍵
快捷鍵 | 功能 |
---|---|
Alt+Enter | 導入包,自動修正代碼(重中之重) |
Ctrl+Y | 刪除光標所在行 |
Ctrl+D | 復制光標所在行的內容,插入光標位置下面 |
Ctrl+Alt+L | 格式化代碼 |
Ctrl+/ | 單行注釋 |
Ctrl+Shift+/ | 選中代碼注釋,多行注釋,再按取消注釋 |
Alt+Shift+上下箭頭 | 移動當前代碼行 |
二、鍵盤錄入–Scanner
1、概述:是java定義好的一個類
2、作用:將數據通過鍵盤錄入的形式放到代碼中參與運行
3、位置:java.util
1、next和nextInt
變量名.nextInt() 輸入整數int
型的
變量名.next() 輸入字符串 String
型的
public static void main(String[] args) {// 創建對象Scanner sc = new Scanner(System.in);// 整數intint detal = sc.nextInt();System.out.println("detal = " + detal);// 字符串String data2 = sc.next();System.out.println("data2 = " + data2);}
}
2、next和nextLine區別
變量名.next():錄入字符串 -> 遇到空格和回車就結束錄入了
變量名.nextLine():錄入字符串 -> 遇到回車就結束錄入了public class Demo03Scanner {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String data = sc.next();System.out.println("data = " + data);String data1 = sc.nextLine();System.out.println("data1 = " + data1);}
}
三、Random隨機數
1、使用
1、創建對象? Random 變量名 = new Random()2、調用方法,生成隨機數? 變量名.nextInt(). ---->在int的取值范圍內隨機一個整數public class Demo01 {public static void main(String[] args) {// 創建對象Random rd = new Random();int data = rd.nextInt();System.out.println("data = " + data);}
}
2、隨機范圍
public class Demo01 {public static void main(String[] args) {// 創建對象Random rd = new Random();/**指定隨機數nextInt(int bound) 0-(bound-1)a.nextInt(10) -> 0-9b.在1-10之間隨機一個數: nextInt(10)+1 -> (0-9)+1 -> 1-10c.在1-100之間隨機一個數:nextInt(100)+1 -> (0-99)+1 -> 1-100d.在100-999之間隨機一個數: nextInt(900)+100 -> (0-899)+100 -> 100-999*/ int data = rd.nextInt(10)+1;System.out.println("data = " + data);}
}
四、流程控制語句
1、switch語句
1、基本使用
switch能匹配的數據類型: byte short int char 枚舉類型 String類型格式: switch(變量){case 常量值1:執行語句1;break;case 常量值2:執行語句2;break;case 常量值3:執行語句3;break;case 常量值4:執行語句4;break;...default:執行語句n;break;}
public class Demo01 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個整數");int data = sc.nextInt();switch (data) {case 1:System.out.println("學習Java第一天");break;case 2:System.out.println("學習Java第二天");break;case 3:System.out.println("學習Java第三天");break;case 4:System.out.println("學習Java第四天");break;default:System.out.println("學習Java第N天");break;}}
}
2、case穿透性
如果沒有break,就會出現case的穿透性,程序就一直往下穿透執行,直到遇到了break或者switch代碼執行完畢了,就停止了public class Demo01 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個整數");int data = sc.nextInt();switch (data) {case 1:System.out.println("學習Java第一天");case 2:System.out.println("學習Java第二天");case 3:System.out.println("學習Java第三天");case 4:System.out.println("學習Java第四天");break;default:System.out.println("學習Java第N天");break;}}
}
3、執行語句重復時
public class Demo02 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個整數");int month = sc.nextInt();switch(month){case 12:case 1:case 2:System.out.println("冬季");break;case 3:case 4:case 5:System.out.println("春季");break;case 6:case 7:case 8:System.out.println("夏季");break;case 9:case 10:case 11:System.out.println("秋季");break;default:System.out.println("什么情況,你家有這個月份?");}}
}
2、if語句
1、if格式
if(boolean表達式){執行語句;
}public class Demo01 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int data1 = sc.nextInt();int data2 = sc.nextInt();if(data1==data2){System.out.println("兩個數相等");}}
}
2、if else格式
public class Demo02 {public static void main(String[] args) {// 任意給出一個整數,請用程序實現盤算該整數是奇數還是偶數,并在控制臺輸出該整數是奇數還是偶數Scanner sc = new Scanner(System.in);int data = sc.nextInt();if(data/2==0){System.out.println("這個整數是偶數 " + data);}else{System.out.println("這個整數是奇數 " + data);}}
}
3、if else if else格式
if(boolean表達式){執行語句1}else if(boolean表達式){執行語句2}else if(boolean表達式){執行語句3}...else{執行語句n}public class Demo08ElseIf {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int data1 = sc.nextInt();int data2 = sc.nextInt();if (data1>data2){System.out.println("data1大于data2");}else if(data1<data2){System.out.println("data1小于data2");}else (data1==data2){System.out.println("data1等于data2");}}
}
3、for循環語句
1.格式:for(初始化變量;比較;步進表達式){循環語句 -> 哪段代碼循環執行,就將哪段代碼放到此處}2.執行流程:a.先走初始化變量b.比較,如果是true,走循環語句,走步進表達式(初始化的變量的值進行變化) c.再比較,如果還是true,繼續走循環語句,走步進表達式d.再比較,直到比較為false,循環結束了
public class Demo01 {public static void main(String[] args) {for (int i = 0; i < 7; i++) {System.out.println("今天是學習Java的第"+i+"天");}}
}
練習1
// 求1-3之間的數據和,并把求和結果輸出public class Demo02 {public static void main(String[] args) {int sum = 0;for (int i = 1; i <= 3; i++) {sum+=i;}System.out.println("sum = " + sum);}
}
練習2
// 求出1-100的偶數和public class Demo02 {public static void main(String[] args) {int sum = 0;for (int i = 1; i <= 100; i++) {if(i%2==0){sum+=i;}}System.out.println("sum = " + sum);}
}
練習3
//統計1-100之間的偶數個數public class Demo02 {public static void main(String[] args) {int sum = 0;for (int i = 0; i < 100; i++) {if(i%2==0){sum+=1;}}System.out.println("num = " + sum);}
}
4、while循環語句
while(比較){循環語句;步進表達式}public class Demo01 {public static void main(String[] args) {int i = 0;while (i<5){System.out.println("今天學習Java");i++;}}
}
練習:世界最高山峰是珠穆朗瑪峰(8844.43米=8844430毫米),假如我有一張足夠大的紙,它的厚度是0.1毫米。請問,我折疊多少次,可以折成珠穆朗瑪峰的高度?public class Demo01 {public static void main(String[] args) {// 1、定義一個變量表示山峰。mountionint mountion = 8844430;// 2、定義一個變量表示紙的厚度 paperdouble papaer = 0.1;// 3、定義一個變量表示折紙的次數。countint count = 0;// 利用while循環比較,如果paper<mountion,就循環對折while(papaer<mountion){papaer*=2;count++;}System.out.println("count = " + count);}
}
5、do…while循環語句
1.格式:初始化變量;do{循環語句;步進表達式}while(比較);2.執行流程:a.初始化變量b.走循環語句c.走步進表達式d.判斷,如果是true,繼續循環,直到比較為false,循環結束3.特點:至少循環一次
public class Demo01DoWhile {public static void main(String[] args) {int i = 0;do{System.out.println("我愛java");i++;}while(i<5);}
}
6、循環控制關鍵字
1.break:a.在switch中代表結束switch語句b.在循環中代表結束循環 2.continue:結束當前本次循環,直接進入下一次循環,直到條件為false為止
public class Demo03Scanner {public static void main(String[] args) {for (int i = 0; i < 5; i++) {if (i == 3) {//結束循環//break;// 結束本次循環continue;}System.out.println("今天是學習Java的第" + i + "天");}}
}
7、死循環
比較條件一直為true
8、嵌套循環
1.概述:循環中還有循環
2.執行流程:
先執行外層循環,再進入內層循環,內層循環就一直循環,直到內層循環結束,外層循環進入下一次循環,直到外層循環都結束了,整體結束
練習1
// 打印直角三角形public class Demo03Scanner {public static void main(String[] args) {for (int j = 1; j < 5; j++) {for (int i = 0; i < j; i++) {System.out.print("* ");}System.out.println();}}
}*
* *
* * *
* * * *
綜合練習
1.創建Scanner和Random對象
2.調用Random中的nextInt(100)+1在1-100之間隨機一個數 rdNumber
3.調用Scanner中的nextInt()方法 鍵盤錄入一個要猜的數 scNumber
4.如果scNumber大于rdNumber,證明猜大了
5.如果scNumber小于rdNumber,證明猜小了
6.如果scNumber等于rdNumber,證明猜中了
public class Demo01Exam {public static void main(String[] args) {//1.創建Scanner和Random對象Scanner sc = new Scanner(System.in);Random rd = new Random();//2.調用Random中的nextInt(100)+1在1-100之間隨機一個數 rdNumberint rdNumber = rd.nextInt(100) + 1;while(true){//3.調用Scanner中的nextInt()方法 鍵盤錄入一個要猜的數 scNumberSystem.out.println("請您猜一個數:");int scNumber = sc.nextInt();//4.如果scNumber大于rdNumber,證明猜大了if (scNumber>rdNumber){System.out.println("對不起,您猜大了!");}else if (scNumber<rdNumber){//5.如果scNumber小于rdNumber,證明猜小了System.out.println("對不起,您猜小了!");}else{//6.如果scNumber等于rdNumber,證明猜中了System.out.println("恭喜您,猜中了!");break;}}}
}