1.順序結構
程序從上往下按順序執行
2.分支結構
2.1單分支 if
if(條件=boolen){
代碼
};
if(條件=boolen){
}else{
}
if(){
}else if(){
}else{
}
2.2多分支 swith
switch(值){ //byte short int char 1.5enum 1.7String
case 值1: //只能做==比較
代碼1;
[break;]可有可無
case 值2:
代碼2;
default:
代碼;
}
2.3 if switch區別
if(很複雜的條件){
}else if(){
}else if(){
}
if 好處(可以寫複雜的邏輯) 不好處 執行比較慢
swicth 好處(判斷的過程效率更高) 不好處 只能做==(固定值)
3.循環結構 for while do…while
循環 重複不停的做同樣的事
爬樓梯 1--->5樓
操場跑圈 1--->5圈
1-5圈
從哪裏開始 1圈 起點
從哪裏結束 5圈 判斷 終點的條件
跑起來變化 增減
程序中想要執行一個正常的循環 和現實生活類似 需要滿三個條件(必要)
初始值 終點判定條件 變化量
3.1 for
允許將三個必要條件都寫在()內
for(初始值 ; 終點判定條件 ; 變化量){
好多好多執行的條件
}
public class TestFor {
public static void main(String[]args){
int round =1;
for (;round<=5;){
System.out.println("跑到第"+round+"圈了");
round++;
}
//循環執行完畢round值爲6
System.out.println("循環執行完畢"+round);
//變量的生命週期問題(變量 棧內存空間 從聲明開始創建出來 用完即回收)
}
}
3.2 while
循環想要執行 需要三個必要條件 初始值 終點判定條件 變化量
初始值;
for(;終點判定條件;){
好多好多執行;
變化量;
}
允許將三個條件都寫在()內 不是不需
初始值;
while(終點判定條件){ //()內只允許寫一個
好多好多執行;
變化量;
}
我理解就是一個for循環的變體
變量定義在循環外 生命週期長了
變化量放在循環內(注意上下的順序可能對執行產生影響)
public class TestWhile1 {
public static void main(String[] args){
int i=1;
while(i<=5 ) {
System.out.println("循環執行中" +i);
i++;
}
System.out.println("循環執行完畢"+i); //6
//循環過程中輸出多少次 5
//循環執行完畢後 i=6
}
}
3.3 do…while
while先判斷後執行 條件不滿足不執行啦
do...while 先執行後判斷 條件不滿足 至少執行一次
do{
好多好多執行;
變化量;
} while(終點判定條件)
3.4關鍵字break,continue
3.4.1 break 終止循環
public class TestBreak {
public static void main(String[] args){
int i=1;
int j=1;
for(;i<=5 ;i++) {
for(;j<=5 ;j++) {
if(j==3){
break; //終止 中斷
}
System.out.println("i= "+i+" j="+j);
} //i==1 j==3 執行兩次輸出 i==2 j==?從幾開始數?3 i==3 j==3
}
System.out.println(i); //6
System.out.println(j); //3
//循環過程中輸出多少次 2
//循環執行完畢後 i=6 j=3
/**
for(int i=1;i<=5 ;i++) {
for(int j=1;j<=5 ;j++) {
if(j==3){
break; //終止 中斷
}
System.out.println("i= "+i+" j="+j);
}
}
//循環過程中輸出多少次 10
//循環執行完畢後 i=6 j=3
*/
/**
for(int i=1;i<=5 ;i++) {
if(i==3){
break; //終止 中斷
}
System.out.println(i); //輸出 1 2
}
*/
}
}
3.4.2 continue 終止本次循環,進入下次循環
public class TestContinue {
public static void main(String[] args){
int i=1;
int j=1;
for(;i<=5 ;i++) {
for(;j<=5 ;j++) {
if(j==3){
continue; //終止 中斷
}
System.out.println("i= "+i+" j="+j);
}//i==1 j==6 輸出四次 i==2 j==?從幾開始數?6 i==3 j==6
}
//循環過程中輸出多少次 4
//循環執行完畢後 i=6 j=6
/**
for(int i=1;i<=5 ;i++) {
if(i==3){
continue; //終斷本次循環,進入下次循環
}
System.out.println(i);
}
//循環過程中輸出多少次 20
//循環執行完畢後 i=6 j=6
*/
}
}
3.4.3 循環標記
public class TestBreakAndContinue {
public static void main(String[] args){
ok:for(int i=1;i<=5 ;i++) {
for(int j=1;j<=5 ;j++) {
if(j==3){
continue ok; //終止 中斷
} //j==3停住一次 繼續下一次i++ i=2
System.out.println("j="+j);
}
}
//循環過程中輸出多少次 10
//循環執行完畢後 i=6 j=3
/**
int i=1;
int j=1;
ok:for(;i<=5 ;i++) {
ko:for(;j<=5 ;j++) {
if(j==3){
break ok; //終止 中斷
}
System.out.println("i= "+i+" j="+j);
}
}
System.out.println("i= "+i);
System.out.println("j= "+j);
*/
/**
ok:for(int i=1;i<=5 ;i++) {
ko:for(int j=1;j<=5 ;j++) {
if(j==3){
break ok; //終止 中斷
}
System.out.println("i= "+i+" j="+j);
}
}
//循環過程中輸出多少次 2
//循環執行完畢後 i=1 j=3
//break爲什麼終斷的是裏層循環? 不看層次問題 兩個循環一模一樣
//如果j==3 break 中斷外面的循環---?? 給循環取名字 循環標記
*/
}
}
4練習?
設計一個小程序 幫我學習英文(星期七個單詞)
用戶輸入的1---> monday
src.zip原碼
lib提高好的內庫 Scanner引用 開發者給我們提供的一個類文件
int x=1;
想要使用Scanner需要如下三步:
1.在類的上面的第一行 import java.util.Scanner;
2.需要輸入之前Scanner y = new Scanner(System.in);//對象
3.通過y.讓他來做事 int =nexInt();
String=nextLine();都可以讀取輸入的信息;
import java.util.Scanner;
public class StudyEnglish { //if(){} else實現
public static void main(String[] args){
/*
想要使用Scanner需要如下三步:
1.在類的上面的第一行 import java.util.Scanner;
2.需要輸入之前Scanner y = new Scanner(System.in);//對象
3.通過y.讓他來做事 int =nexInt();
*/
Scanner input =new Scanner(System.in);
System.out.println("請你輸入一個數字,我來輸出星期");
int day = input.nextInt(); //可以讀取我們輸入的文字 input,nextLine();讀取字符串
if(day == 1){
System.out.println("monday");
}else if(day==2) {
System.out.println("tuesday");
} else if(day==3) {
System.out.println("wednesday");
} else if(day==4) {
System.out.println("thursday");
} else if(day==5) {
System.out.println("friday");
} else if(day==6) {
System.out.println("Saturday");
} else if(day==7) {
System.out.println("Sunday");
} else {
System.out.println("我管你是星期幾");
}
}
}
import java.util.Scanner;
public class TestSwitch { //switch實現
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.println("請您輸入一個數字");
int i=input.nextInt();
switch(i) {
case 1:
System.out.println("monday");
break;
case 2:
System.out.println("tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("thursday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("我管你是星期幾error");
}
}
}
程序要求
1.可讀性
起名字 規範代碼 縮進 註釋
2.健壯性
程序嚴謹 判斷時邏輯要嚴謹
3.實現功能的基礎上 能不能做優化(代碼結構--簡單 減少冗餘 性能 內存空間)
if else小任務
1.利用if語句實現一個 判斷給定月份對應的季節
month==5 345春天 678夏天 9 10 11秋天 12 1 2冬天
import java.util.Scanner;
public class Month {
public static void main(String[] args ){
//1.創建一個month變量 用來存儲一個月份值(Scanner)
Scanner input =new Scanner(System.in);
System.out.println("請輸入月份,我來判斷季節");
int month = input.nextInt();//幫我們讀取輸入的數字 input.nextLine;讀取字符串
//2.通過month存儲的值 進行季節的判斷
/*if(month>=3 && month <=5){
System.out.println("是春天");
}else if(month>=6 && month <=8){
System.out.println("是夏天");
} else if(month>=9 && month <=11){
System.out.println("是秋天");
}else if(month==1 || month ==2 || month==12){
System.out.println("是冬天");
}else{
System.out.println("無效數字");
}*/
//優化結構
if(month<1 || month>12) {
System.out.println("您輸入的數字不符和月份,數據有誤");
}else if(month>=3 && month <=5){
System.out.println("是春天");
}else if(month>=6 && month <=8){
System.out.println("是夏天");
} else if(month>=9 && month <=11){
System.out.println("是秋天");
}else{
System.out.println("是冬天");
}
}
}
2.利用if語句實現一個 判定學生成績對應的區間
不及格 60-70及格 70-80中 80-90良 90-100優秀 滿分 數據有誤
import java.util.Scanner;
public class Grade {
public static void main(String[] args ){
Scanner input =new Scanner(System.in);
System.out.println("請輸入成績");
int grade = input.nextInt();
if(grade>0 && grade <60){
System.out.println("不及格");
}else if(grade>=60 && grade <70){
System.out.println("及格");
}else if(grade>=70&& grade <80){
System.out.println("中");
}else if(grade>=80 && grade <90){
System.out.println("良");
}else if(grade>=90 && grade <100){
System.out.println("優秀");
}else if(grade ==100){
System.out.println("滿分");
}else{
System.out.println("無效數字");
}
}
}
3.擴展 利用if實現一個隨機搖骰子的小遊戲
隨機搖一個骰子的點數 1-6 123 456
玩家利用Scanner輸入 猜數字
利用if比較 猜對了 猜錯了 Math.random()
import java.util.Scanner;
import java.util.Random;
public class GuessNumber {
public static void main(String[] args ){
//1.隨機搖骰子的過程,隨機產生一個骰子點數 1-6整數
int num = (int)(Math.random() * 5 + 1);
//2.讓玩家猜測數字
Scanner input =new Scanner(System.in);
System.out.println("請輸入一個數字")
int a = input.nextInt();
//3.比較點數和猜測結果
if(a==num){
System.out.println("骰子爲"+num+",猜對了") ;
}else{
System.out.println("骰子爲"+num+",猜錯了") ;
}
}
}
switch小任務
1.利用switch語句實現一個 判定學生成績對應的區間
不及格 60-70及格 70-80中 80-90良 90-100優秀 滿分 數據有誤
import java.util.Scanner;
public class SwitchGrade {
public static void main (String[] args){
//1.先輸入成績
Scanner input = new Scanner(System.in);
System.out.println("請輸入學習成績,判斷區間");
int grade =input.nextInt();
//2.判斷成績
switch (grade/10){ //61/10 6
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: //優化 去除重複內容
System.out.println("不及格");
break;
case 6:
System.out.println("及格");
break;
case 7:
System.out.println("中等");
break;
case 8:
System.out.println("良");
break;
case 9:
System.out.println("優秀");
break;
case 10:
//一百多分100-109 優化
if(grade==100){
System.out.println("滿分");
break;
}
default:
System.out.println("不及格,或數據錯誤");
break;
}
}
}
2.利用Scanner輸入一個值(代表一個星期幾 4)
爲小茗同學制定一個學習計劃
1 3 5學習語文
2 4 6學習數學
7
public class SwitchLearn {
public static void main(String[] args){
//1.先輸入星期幾
Scanner input = new Scanner(System.in);
System.out.println("請輸入星期幾");
int day =input.nextInt();
//2.判斷星期幾做什麼
switch(day){
/* case 1 :
System.out.println("星期1學習語文");
break;
case 2 :
System.out.println("星期2學習數學");
break;
case 3 :
System.out.println("星期3學習語文");
break;
case 4 :
System.out.println("星期4學習數學");
break;
case 5 :
System.out.println("星期5學習語文");
break;
case 6 :
System.out.println("星期6學習數學");
break;
case 7 :
System.out.println("星期天休息");
break;
default :
System.out.println("輸入的數字不是星期");
*/
//優化
switch(day){
case 1 :
case 3 :
case 5 :
System.out.println("學習編程");
break;
case 2 :
case 4 :
case 6 :
System.out.println("學習英語");
break;
case 7 :
System.out.println("用英語編程");
break;
default :
System.out.println("輸入的數字不是星期");
}
}
}
for 小任務
1.
操場上有一百多人,讓他們排隊
三個人一組 多一人,四個人一組,多兩個,五個人一組 多兩個
求解 操場上的人數多少?
解:設 操場的人數x
x%3 ==1 x%4==2 x%5==2
計算機想要找到x的值 不是通過計算得來的 通過最笨拙的方法 挨個嘗試
想要挨個的嘗試 需要一個範圍
讓計算機幫我們 在100-200之間 挨個嘗試一下 哪一個數字符號上述條件
public class ForDemoOne {
public static void main(String[] args) {
for(int x=100;x<=200;x++ ){
if(x%3==1 && x%4==2 && x%5==2 ){
System.out.println("操場人數有"+x+"個"); //142
}
}
}
}
2.甲乙丙丁四個人加工零件 加工的總零件數爲370個
如果甲加工的零件數多10
如果乙加工的零件數少20
如果丙加工的零件數乘以2
如果丁加工的零件數除以2'
則四個人加工的零件數就相等了
求 四個人加工的零件個數分別是多少?
思路:
設相等的數爲x;
甲+10=x;
乙-20=x;
丙*2=x;
丁/2=x;
甲+乙+丙+丁===> (x-10)+(x+20)+(x/2)+(x*2)= 370;
x x 0 2x 95 最大 最小預計 45
public class ForDemoTwo {
public static void main(String[]args ){
/* for(int x=1;x<=370;x++) {
if((x-10)+(x+20)+(x/2)+(x*2) ==370 ) {
System.out.println("甲加工的零件數爲"+ (x-10)+ " x="+x);//70 80
System.out.println("乙加工的零件數爲"+ (x+20)+ " x="+x);//100 80
System.out.println("丙加工的零件數爲"+ (x/2) + " x="+x);//40 80
System.out.println("丁加工的零件數爲"+ (x*2) + " x="+x);//160 80
}
}*/
//優化 判斷估計初始值 和最後值
for(int x=45;x<=95;x++) {
if((x-10)+(x+20)+(x/2)+(x*2) ==370 ) {
System.out.println("甲加工的零件數爲"+ (x-10)+ " x="+x);//70 80
System.out.println("乙加工的零件數爲"+ (x+20)+ " x="+x);//100 80
System.out.println("丙加工的零件數爲"+ (x/2) + " x="+x);//40 80
System.out.println("丁加工的零件數爲"+ (x*2) + " x="+x);//160 80
}
}
}
}
3.雞兔同籠問題
小雞 小兔子 關在同一個籠子裏 小雞兩隻腳 小兔子四隻腳
小雞+小兔子 總數50只 腳的總數160只
求 小雞 和小兔子各多少隻
思路
設雞x只
則兔有 50-x 只
雞*2+ 兔*4 =160 ====> x*2+(50-x)*4 == 160
public class ForDemoThree {
public static void main(String [] args){
for(int x=1; x<=50;x++){
if(x*2 + (50-x)*4 == 160){
System.out.println("雞有 "+x+"只"); //20
System.out.println("兔有 "+ (50-x) +"只"); //30
}
}
}
}
4.通過循環找到三位數的水仙花數
153---->1 5 3 各自立方和
1+ 125+ 27=153
100-999之間挨個嘗試 滿足上述規則數字 153 370 371 407
思路
數x
百位 a = x/100
十位 b = (x%100)/10
個位 c = x%10
a*a*a +b*b*b +c*c*c ==x;
public class ForDemoFour {
public static void main(String [] args){
/* for(int x=100; x<=999;x++){
int a = x/100;
int b = (x%100)/10;
int c = x%10;
if( a*a*a +b*b*b +c*c*c == x){
System.out.println("水仙花數是 "+x); //153 370 371 407
}
}*/
//優化
for(int x=100; x<=999;x++){
if(Math.pow(x/100,3) +Math.pow((x%100)/10,3)+Math.pow(x % 10,3) == x){
System.out.println("水仙花數是 "+x); ////153 370 371 407
}
}
}
}
for循環嵌套
需求 控制檯打印*
1.控制檯輸出1行 ****
2.控制檯輸出4行 ****
3.控制檯輸出4行
*
**
***
4.控制檯輸出4行
*
**
***
****
import java.util.Scanner;
public class DrawStar {
public static void main(String [] args){
for(int i=1;i<=4;i++){ //控制4行
//畫佔位符
for(int j=1;j<=4-i;j++) {
System.out.print(" ");
}
//畫星星
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
}
/* 3
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
} */
/*2.
int count=4; //每一行星星的數量隨意
for(int i=1;i<=4;i++){
for(int j=1;j<=count;j++) {
System.out.print("*");
}
System.out.println();
}*/
//讓計算機幫我們做四次同樣的事
//i j 分別控制着什麼? i控制行數 j控制的是每一行星星的個數
//i==5 5<=4 false
// j==5 5<=4 false
//1.複用性
/* System.out.println("尊敬的客戶,您需要畫幾個");
Scanner input =new Scanner(System.in);
int count=input.nextInt();
for(int i=1;i<=count;i++){
System.out.print("*");
}*/
}
}
嵌套任務1
*******
*** ***
** **
* *
思路:
輸出4行 外面循環i=4次
i=1時 輸出j = 7 次* 1234567
i=2時 當j= 4時 輸出空格 123 4 567
i=3時 當j= 3 4 5 輸出空格 12 345 67
i=4時 當j= 2 3 4 5 6 輸出空格 1 23456 7
import java.util.Scanner;
public class Demo1{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("請輸入行數");
int line =input.nextInt();
//******* 畫星星 換行 i=1 7
//*** *** 畫星星 畫空格 畫星星 換行 i=2 3 1 3
//** ** 畫星星 畫空格 畫星星 換行 i=3 2 3 2
//* * 畫星星 畫空格 畫星星 換行 i=4 1 5 1
for(int i=1;i<=line; i++){ //控制4行
if(i==1){ //第1行規則
for(int j=1;j<=2*line - 1; j++) { //畫星星
System.out.print("*");
}
}else{ //後三行規則
//畫星星
for(int j=1;j<=(line+1)-i;j++){
System.out.print("*");
}
//畫空格
for(int j=1;j<= 2*i-3;j++){
System.out.print(" ");
}
//畫星星
for(int j=1;j<=(line+1)-i;j++){
System.out.print("*");
}
}
System.out.println(); //換行
}
}
}
2.數字金字塔
1
121
12321
1234321
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
/** 空格 左數字 右數字 空格
*
* 1 1 i=1 3 1-1 0 3
* 2 1 2 1 i=2 2 1-2 1-1 2
* 3 1 2 3 2 1 i=3 1 1-3 2-1 1
* 4 1 2 3 4 3 2 1 i=4 0 1-4 3-1 0
*/
Scanner input = new Scanner(System.in);
System.out.println("請輸入行數");
int line =input.nextInt(); //行數
for (int i = 1; i <= line; i++) { // i 控制行
//空格佔位
for (int j = 1; j <= line - i; j++) {
System.out.print(" ");
}
//左 數字 小到大 j++
for (int j = 1; j <= i; j++) {
System.out.print(j+" ");
}
//右 大到小 j--
for (int j = i-1; j >=1; j--) {
System.out.print(j+" ");
}
// 空格
for (int j = 1; j <= line - i; j++) {
System.out.print(" ");
}
//換行
System.out.println("");
}
}
}
3 輸出乘法口訣
1*1=1
1*2=2 2*2=4
public class Demo3 {
public static void main(String []args){
/**
* 1 * 1 =1 輸出次數 1 與外循環的行數相同
* 1 * 2 =2 2 * 2 =4 2
* 3
* 行數 9
* 解析 循環* 行數 = 結果
* 第二個循環循環 次數爲
*/
for(int i=1;i<= 9;i++ ){
for(int j=1;j<=i ;j++ ){
//\t 轉義 表格
System.out.print(j +" * "+i +" = "+j*i+"\t");
}
System.out.println("");
}
}
}
4 .找尋2-100之間的素數(質數)
找尋2-100之間的素數(質數) 只能被1和本身整除的數字 2 3 5 7 11
2是素數
3是素數
4不是素數
5是素數
6不是素數
public class Demo4 {
public static void main(String[] args) {
/**
* 判斷2-100的素數 從2開始 到100 每個數判斷一次 並輸出
* 只能被1和本身整除的數字
* 判斷是否素數
* 將循環到的數i 依次除 2 到 i-1 都有餘數就素數
* 當出現餘爲0時就不是素數 此時需要記錄 此數不是素數
*/
boolean bl=true;
//判斷 是否素數 i/2 break 優化循環
for (int j = 2; j <= i/2; j++) {
if (i % j == 0) {
bl=false;
System.out.println(i +" 不是素數");
break; //跳出此次循環
}
}
//輸出
if (bl){
System.out.println(i +" 是素數");
}
}
}
}