java流程控制課堂筆記
1. 用戶交互Scanner
Scanner對象
-
java.util.Scanner包中Scanner類獲取用戶的輸入
-
基本語法:
Scanner scanner = new Scanner(System.in);
-
通過Scanner類的next()與nextLine()方法獲取輸入的字符串,在讀取前我們一般需要 使用hasNext()與hasNextLine()判斷是否還有輸入的數據
next():
1.一定要讀取到有效字符後纔可以結束輸入
2.對輸入有效字符之前遇到的空白,next()方法會自動去掉
3.只有輸入有效字符後纔將其後面輸入的空白作爲分隔符或者結束符
4.next()不能得到帶有空格的字符串
nextLine():
1.以Enter爲結束符,即nextLine()方法返回的是輸入回車之前的所有字符
2.可以獲得空白
實例代碼
import java.util.Scanner;
public class Test{
public static void main(String[] args){
//創建一個掃描器對象,用於接收鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判斷用戶有沒有輸入字符串
if(scanner.hasNext()){
String str = scanner.next(); //輸入:123 123 123
System.out.println(str);}
//輸出:123
//凡是io流的類如果不關閉會一直佔用資源,要養成好習慣,用完就關掉
scanner.close();
}
}
import java.util.Scanner;
public class Test{
public static void main(String[] args){
//創建一個掃描器對象,用於接收鍵盤數據
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判斷用戶有沒有輸入字符串
if(scanner.hasNextLine()){
String str = scanner.nextLine(); //輸入:1234 1234 12
System.out.println(str);} //輸出:1234 1234 12
//凡是io流的類如果不關閉會一直佔用資源,要養成好習慣,用完就關掉
scanner.close();
}
}
2.Scanner進階
實例代碼
import java.util.Scanner;
public class Test{
public static void main(String[] args){
/*需求:輸入多個數字,並求其總和與平均值,
每輸入一個數據,通過輸入非數字來結束輸入並輸出結果*/
Scanner scanner = new Scanner(System.in);
int i = 0;
double sum = 0;
System.out.println("請輸入數據:");
while(scanner.hasNextDouble()){
double d = scanner.nextDouble();
i++;
sum = sum + d;
System.out.println("當前輸入第"+i+"個數據,數據的和爲"+sum);
}
System.out.println(i+"個數的和爲"+sum);
System.out.println(i+"個數的平均值爲"+(sum/i));
scanner.close();
}
}
3. java三種算法結構
順序結構
java的基本算法結構
選擇結構
- if單選擇結構
if(布爾表達式){
//布爾表達式爲true時執行的語句
}
- if雙選擇結構
if(布爾表達式){
//布爾表達式爲true時執行的語句
}else{
//布爾表達式爲false時執行的語句
}
- if多選擇結構
if(布爾表達式1){
//布爾表達式1爲true時執行的語句
}else if(布爾表達式2){
//布爾表達式2爲true時執行的語句
}else if(布爾表達式3){
//布爾表達式3爲true時執行的語句
}else{
//如果以上布爾表達式都不爲true時執行代碼
}
實例代碼
import java.util.Scanner;
public class Test{
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("優秀");
}else if(score < 90 && score >= 80){
System.out.println("良好");
}else if(score < 80 && score >= 60){
System.out.println("及格");
}else if(score < 60 && score >= 0){
System.out.println("不及格");
}else{
System.out.println("輸入成績不合法");
}
scanner.close();
}
}
- 嵌套if結構
if(布爾表達式1){
//如果布爾表達式1的值爲true執行代碼
if(布爾表達式2){
//如果布爾表達式2的值爲true執行代碼
}
}
- switch多選擇結構
switch(expression){
case value1:
//語句
break;//可選
case value2:
//語句
break;//可選
//...可以有任意數量的case語句
default://可選
//語句
}
switch case語句中的變量類型可以是:
- byte、short、int或者char
- 從jdk7開始,switch支持字符串String 類型了
- case 標籤必須爲字符串常量或字面量
public class Test{
public static void main(String[] args) {
char grade = 'C';
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;
default:
System.out.println("未知等級");
}
}
}
用IDEA實現反編譯:將Test.class文件複製粘貼到Test.java所在的包中
循環結構
- while循環
while(布爾表達式){
//循環內容
}
注意點:只要布爾表達式爲true,循環就會一直執行下去,形成死循環
實例代碼
/*需求:計算1~100的和*/
public class Test{
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i <= 100){
sum = sum + i;
i++;
}
System.out.println("和爲"+sum); //和爲5050
}
}
- do…while循環
do{
//代碼語句
}while(布爾表達式);
while 和 do-while 的區別:
1. while先判斷後執行,dowhile是先執行後判斷
2. do…while總是保證循環體會至少執行一次
實例代碼
/*需求:計算1~100的和*/
public class Test{
public static void main(String[] args) {
int i = 1;
int sum = 0;
do{
sum = sum + i;
i++;
}while(i <= 100);
System.out.println("和爲"+sum); //和爲5050
}
}
- for循環
for循環語句是支持迭代的一種通用結構,是最有效、最靈活的循環結構
for(初始化;布爾表達式;更新){
//代碼語句
}
實例代碼1
/*需求:分別計算1~100的奇數和與偶數和*/
public class Test{
public static void main(String[] args) {
int oddsum = 0;
int evensum = 0;
for (int i = 0; i <= 100; i++) {
if(i%2==1){
oddsum += i;
}else{
evensum += i;
}
}
System.out.println("奇數的和爲"+oddsum); //奇數的和爲2500
System.out.println("偶數的和是"+evensum); //偶數的和是2550
}
}
實例代碼2
/*需求:九九乘法表*/
public class Test{
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}
}
- 在java5中引進一種主要用於數組的增強型for循環
for(聲明語句:表達式){
//代碼句子
}
**聲明語句**:聲明新的局部變量,該變量的類型必須和數組元素的類型匹配。
其作用域限定在循環語句塊,其值與此時數組元素的值相等
**表達式**:表達式是要訪問的數組名,或者是返回值爲數組的方法
實例代碼
public class Test{
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定義了一個數組
//遍歷數組的元素
for (int number : numbers) {
System.out.println(number);
}
}
}
4.break、continue
break用法
- 在任何循環語句的主體部分,均可用break控制循環的流程
- 用於強行退出循環,不執行循環中剩餘的語句
continue用法
- 語句用在循環語句體中,用於終止某次循環過程,即跳過循環體中尚未執行的語句,接着進行下一次是否執行循環的判定
5.打印三角形及Debug
實例代碼
public class Test{
/*打印三角形*/
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
for(int j = 5; j >= i; j--){
System.out.print(" ");
}
for(int j = 0; j <= i; j++){
System.out.print("*");
}
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
- Debug:調試,可以對縮寫代碼程序一步一步執行 ,後期詳解