如何在混沌演練的場景中降低應用的MTTR,必須需要根據監控定位,然後人工進行反饋進行處理嗎?是否可以自動化,是否有方案可以降低混沌演練過程中的影響?以此達到快速止血,進一步提高系統的穩定性。本篇文章將根據一些思考和實踐來解答以上問題。 ...
Next和NextLine
Java5的新特性,帶來了java.util.Scanner類,提供了人機交互的作用。我們可以通過它獲取用戶的輸入。
public class Demo2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);//創建一個掃描器對象,用於接收鍵盤數據
s.next();//獲取數據
s.nextLine();//獲取多行數據
s.hasNext();//判斷還是否有下一個數據
s.hasNextLine();//判斷還是否有下一行數據
}
}
-
next():
-
一定要讀取到字元才能結束輸入。(才能使程式繼續運行)
-
不會記錄空白,並且以空格為結束符。
-
不能得到帶有空格的字元串。
-
-
nextLine():
-
以Enter為結束符。
-
可以獲得空白。
-
Scanner的獲取類型
Scanner類提供了多種方法,支持獲取不同數據類型的數據。
例如:
Scanner s = new Scanner(System.in);
int a = s.nextInt();//獲取一個int類型,如果不是int型則彈出錯誤
s.hasNextInt();//判斷輸入的是否為int類型,返回布爾數值。
System.out.println(a);
s.close();
練習:輸入多個數字,求其總和與平均值;每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果。
public class Demo02_practice {
//練習:輸入多個數字,求其總和與平均值;每輸入一個數字用回車確認,
// 通過輸入非數字來結束輸入並輸出執行結果。
public static void main(String[] args) {
//創建掃描器
Scanner scanner = new Scanner(System.in);
//創建接收變數,double類型記得要加上L,不然會出現數據錯誤問題
double sum = 0L;
int count=0;
//判斷是否還有double數值輸入,非double則跳出迴圈
while (scanner.hasNextDouble()){
sum += scanner.nextDouble();
count++;
}
System.out.println("總數為:"+sum);
System.out.println("平均數為:"+sum/count);
//回收掃描器
scanner.close();
}
}
順序結構
字面意思,除了特殊情況外,代碼都是一行一行的執行,就像寫字一樣,一行一行寫。
選擇結構(IF語句)
單選擇結構
小練習:判斷輸入的數值是否為hello
-
.equals 方法和 str=="hello" 的區別在於:
-
.equals 方法是對比數值是否相同;
-
str=="hello"是對比兩個變數之間的記憶體地址塊是否是同一塊。
-
public class IFDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入內容:");
String str = scanner.nextLine();
if (str.equals("Hello")){
System.out.println(str);
}
System.out.println("輸入的值不是hello");
scanner.close();
}
}
雙選擇結構
小練習:考試分數大於等於60就是幾個,小於60就不及格
public class IFDemo2 {
public static void main(String[] args) {
//考試分數大於等於60就是幾個,小於60就不及格
//解決方案A
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
scanner.close();
if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
//解決方案B:三元運算符
String str = score>=60?"及格":"不及格";
System.out.println(str);
}
}
多選擇結構
小練習:考試分數大於等於60就是幾個,小於60就不及格,並且對及格成績每10分進行定級,滿分則顯示滿分。超出分值範圍應當顯示不合法。
public class IFDemo03 {
public static void main(String[] args) {
//考試分數大於等於60就是幾個,小於60就不及格
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
scanner.close();
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 && score>=0){
System.out.println("不及格");
}else {
System.out.println("成績不合法");
}
}
}
嵌套結構
小練習:輸入一個數1~100,猜出這個數值是多少?
小提示:採用二分查找法。
switch多選擇結構
-
case
-
穿透關鍵詞,可以根據switch上的變數,對比case後的數值,相同則執行case下的語句。
-
-
break
-
中斷語句,跳出迴圈或當前代碼塊。
-
拓展:反編譯
通常我們Java文件在運行時,都需要編譯為.class 文件才能執行。
IDEA 程式為我們提供了反編譯的功能。將.class文件拖入IDEA程式中,即會自動進行反編譯。
實際上我們看到的switch語句,是通過編寫為這種方式,進行運行的。
迴圈結構
while迴圈
小練習:計算1+2....+100
public class whileDemo1 {
public static void main(String[] args) {
int i=1;
int sum=0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
do……while迴圈
小練習:計算1+2....+100,觀察do..while 和whlie的區別
public class WhileDemo02 {
public static void main(String[] args) {
int i=1;
int sum=0;
do{
sum+=i;
i++;
} while (i<=100);
System.out.println(sum);
}
}
for迴圈(經常使用,十分重要)
是最有效,最靈活的迴圈結構。
關於for迴圈有以下幾點說明:
-
最先執行初始化步驟。
-
可以聲明一種類型,但可初始化一個或多個迴圈控制變數,也可以是空語句。
-
之後,檢測布爾值,如果是true則運行迴圈。false則迴圈終止。
-
執行一次迴圈後,更新迴圈控制變數(迭代因數控制迴圈變數的增減)
//FOR語句的死迴圈寫法
for(;;){
}
//FOR語句的是三個部分都可以是空語句。
練習1:計算0到100之間的奇數和偶數的和
public class ForDemo01 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
//快捷鍵100.for
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
oddSum += i;
} else {
evenSum += i;
}
}
System.out.println("奇數和為:" + oddSum);
System.out.println("偶數和為:" + evenSum);
}
}
練習2:用while或for迴圈輸出1-1000之間能被5整除的數,並且每行輸出3個
public class ForDemo02 {
public static void main(String[] args) {
int j = 0;
for (int i = 1; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
j++;
if (j % 3 == 0 && j != 0) {
System.out.println();
}
}
}
}
}
練習3:列印九九乘法表
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j)+"\t");
}
System.out.println();
}
}
}
增強型for迴圈
public class ForDemo04 {
public static void main(String[] args) {
//定義數組的兩種方式:不初始化,但是指定數組範圍;
int[] number=new int[5];
//初始化數組
int[] number2={12,213,31,1213,14,5,54,23};
for (int i : number2) {//遍曆數組
System.out.println(i);
}
}
}
break & continue
簡而言之:
-
break:跳出迴圈體;
-
continue:跳過單次迴圈;
-
功能2:跳到指定標簽的迴圈體如下;(不建議使用)
-
outer:for (int i = 11; i < 15; i++) {
for (int j = 2; j < i / 2; j++) {
if (i % j == 0) {
continue outer;
}
}
}
練習
題目:輸入一個數值,根據這個數值列印一個等邊三角形
代碼:
public class TestDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int n = scanner.nextInt();
for (int j = n; j > 0; j--) {
for (int i = j; i > 0; i--) {
System.out.print(" ");
}
for (int i = 0; i < (n - j) * 2 + 1; i++) {
System.out.print("*");
}
System.out.println();
}
} else {
System.out.println("輸入信息有誤,請輸入整數");
}
//解題思路
//當N=5時,要輸出1個*;以此類推得出如下思路;最後總結公式:(n-j)*2+1;可以解這個公式
// 5---1
// 4---3
// 3---5
// 2---7
// 1---9
}
}
輸出如下:
這種類型的演算法題,對加深JavaSE代碼的理解非常有幫助;有能力的小伙伴可以進階的研究一下貪心演算法中的背包問題。是一到經典的動態規劃類型題目。
### 部分截圖取自,B站UP:遇見狂神說;菜鳥教程