華為:未來幾年將推出 7.5-5 分鐘快充 200 公里的全棧平臺解決方案
java流程控制
1.Scanner物件
java.util.Scanner,通過Scanner類來獲取使用者的輸入
基本語法:Scanner s = new Scanner(System.in);
通過Scanner類的next(),nextLine()方法獲取輸入的字串,讀取前先用hasNexr(),hasNextLine()判斷
(1).next()
- 遇到空格結束,會忽略有效字元前的空格
- 一定要獲取字元
package Scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { // 建立一個掃描器物件,用於接受鍵盤資料 Scanner scanner = new Scanner(System.in); System.out.println("使用next方法接收:"); // 判斷使用者有沒有輸入字串 if (scanner.hasNext()){ // 使用next方法接收: String str = scanner.next(); System.out.println("輸出的內容為:"+str); } // 凡是屬於io流的的類如果不關閉會一直佔用資源,要用完就關 scanner.close(); } }
(2).nextLine()
- 一Enter為結束符
- 可以獲得空白
package Scanner; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入:"); if (scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println("輸出內容:"+str); } scanner.close(); } }
2.Scanner進階使用方法
(1).資料型別
package Scanner; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { int i = 0; float f = 0.0f; Scanner scanner = new Scanner(System.in); System.out.println("請輸入一個整數:"); if(scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println("整數資料:"+i); }else { System.out.println("你輸入的不是整數!"); } System.out.println("請輸入一個小數:"); if(scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("小數資料:"+f); }else { System.out.println("你輸入的不是小數!"); } scanner.close(); } }
2.順序結構
從上到下,一句一句來
package struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
3.選擇結構
(1).if單選擇結構
if(boolean){
// 如果Boolean==true執行的語句。
}
package struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入內容:");
String s = scanner.nextLine();
// equals:判斷字串是否相等
if(s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
(3).if雙選擇結構
if(boolean){
// 如果Boolean==true執行
}else {
// 如果Boolean==false執行
}
package struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
// 考試成績大於60就及格,小於60不及格
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score >= 60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
(3).if多選擇語句
if(){
}else if(){
}else if(){
}else {
}
package struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score == 100){
System.out.println("恭喜滿分!");
}else if(score<100 && score>=90){
System.out.println("A");
}else if(score<90 && score>=80){
System.out.println("B");
}else if(score<80 && score>=70){
System.out.println("C");
}else if(score<70 && score>=60){
System.out.println("D");
}else if(score<60){
System.out.println("不及格");
}else {
System.out.println("你輸入的是啥?!");
}
}
}
(4).巢狀if語句
(5).switch多選擇結構
switch和case合用
switch(x){
case value:
// 語句
break;//可選
default:
// 語句
}
支援變數型別:byte,short,int,char
從Javase7開始,支援String型別
case標籤必須為字串常量或字面量
package struct;
import java.util.Scanner;
public class SwtchDemo01 {
public static void main(String[] args) {
// case穿透
// switch 匹配一個具體的值
Scanner scanner = new Scanner(System.in);
String grade = scanner.next();
// 反編譯 (idea)
switch (grade){
case "A":
System.out.println("優秀");
break;
case "B":
System.out.println("良好");
break;
case "C":
System.out.println("及格");
break;
case "D":
System.out.println("再接再厲");
break;
case "E":
System.out.println("掛科");
break;
default:
System.out.println("你是什麼等級?!");
}
}
}
迴圈結構
java5引入了增強型for迴圈
(1).while迴圈
最基本的迴圈(莫要寫死迴圈)
while(Boolean表示式){
// 迴圈內容
}
package struct;
public class WhileDemo01 {
public static void main(String[] args) {
// 輸出1~100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package struct;
public class WhileDemo02 {
public static void main(String[] args) {
while (true){
// 等待客戶端響應
// 定時檢查
// ......
}
}
}
package struct;
public class WhileDemo03 {
public static void main(String[] args) {
// 1加到100
int sum = 0;
int i = 1;
while(i<=100){
sum += i;
i++;
}
System.out.println(sum);
}
}
(3).do...while迴圈
do{
// 程式碼語句
}while(Boolean表示式);
do...while至少保證迴圈體執行一次
package struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
// 先判斷再執行
do {
sum += i;
i++;
}while (i <= 100);
}
}
package struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("--------------");
do {
System.out.println(a);
a++;
}while (a < 0);
}
}
(4).for迴圈
支援迭代的一種通用結構,最有效,最靈活的結構
for迴圈的執行次數在執行前確定的
for(初始化;布林表示式;更新){
// 程式碼語句;
}
例項:
- 1-100奇數,偶數和
package struct;
public class ForDemo02 {
public static void main(String[] args) {
// 計算0-100之間奇數和偶數的和
int a = 0;
int b = 0;
for (int i = 0; i <= 100; i++) {
if(i % 2 == 0){
a+=i;
}else {
b+=i;
}
}
System.out.println("奇數和為:"+b);
System.out.println("偶數和為:"+a);
}
}
- 1-1000之間能被5整除的數
package struct;
public class ForDemo03 {
public static void main(String[] args) {
// 輸出1-1000之間能被5整除的數,一行三個
for (int i = 1; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i+"\t");
}
if(i % (5*3) == 0){
System.out.print("\n");
}
}
// println 輸出會換行
// print 輸出不會換行
}
}
- 九九乘法表
package struct;
public class FoeDemo04 {
public static void main(String[] args) {
// 先列印第一列
// 迴圈巢狀
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i) + "\t");
}
System.out.println();
}
}
}
(5).增強型的for迴圈
java5新增的用於陣列和集合的
for(宣告型別 :表示式){
// 程式碼句子
}
類似python中的for迴圈(遍歷)
package struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};
for(int i = 0;i<numbers.length;i++){
System.out.println(numbers[i]);
}
// 遍歷陣列的元素
for(int x : numbers){
System.out.println(x);
}
}
}
break,continue
(1).break
在迴圈任何主體部分均可使用,強制退出迴圈,不執行迴圈剩餘的語句。
package struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if(i==30){
break;
}
}
System.out.println("123");
}
}
(2).continue
終止某次迴圈,進行下一次迴圈的判定
package struct;
public class ContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if(i%10==0){
System.out.println();
continue;
// 執行下一次迴圈的判斷
}
System.out.print(i);
}
}
}
(3).goto關鍵字(無條件限制轉移符)
java中保留了goto關鍵字,但是沒有使用,但是break和continue有goto的影子--加標籤的break和continue(例如:label:)
package struct;
public class LabelDemo {
// 列印101-150之間的質數
public static void main(String[] args) {
int count = 0;
// 不建議使用
outer:for(int i = 101;i<150;i++){
for(int j = 2;j<i/2;j++){
if (i%j==0){
continue outer;
}
}
System.out.println(i+" ");
}
}
}
例項:
列印三角形:
package struct;
public class TestDemo01 {
public static void main(String[] args) {
// 列印三角形
for (int i = 1; i <= 5; i++) {
for(int j = 5;j>=i;j--){ // 左空白
System.out.print(" ");
}
for(int j = 1;j<=i;j++){ // 左半三角形
System.out.print("*");
}
for(int j = 1;j<i;j++){ // 右半三角形
System.out.print("*");
}
for(int j = 5;j>=i;j--){ // 右空白
System.out.print(" ");
}
System.out.println();
}
}
}