Everybody大家好,今天咋們來一起探討一下關於流程控制語
首先,我們知道他有三種結構,分別是順序,選擇,循環
順序結構
關於順序結構咋們就不在多多講解了,順序結構就是代碼從主函數開始逐行向下運行,簡單的來說就是按順序運行。順序結構是一種宏觀的代碼運行結構。
選擇結構
選擇結構他顧名思義就是,給出條件,你進行選擇,選擇適合你,在進行程序的執行
他有兩種格式,分別是if-else語句 和 switch語句
if-else語句
他的基本結構是這樣的,基本格式
if(條件表達式){
滿足條件表達式時,執行的代碼;
}else{
不滿足條件表達式時,執行的代碼;
}
單if語句
if(條件表達式){
當滿足條件表達式時,執行的代碼;
}
嵌套if-else形式
if(條件1){
當滿足條件表達式時,執行的代碼;
if(條件2){
當滿足條件表達式二時,執行的代碼;
}else{
當不滿足條件表達式二時,執行的代碼;
}
}else{
當不滿足條件表達式一時,執行的代碼;
}
if-else if
if(條件1){滿足條件表達式一執行的代碼;
}else if(條件2){滿足條件表達式二執行的代碼;
}else if(條件3){滿足條件表達式三執行的代碼;
}else{以上條件均不滿足時,執行此代碼;
}
switch語句
咋們來看一看他們的基本格式
switch(變量){
case 值1:
執行語句1;
break;
case 值2:
執行語句2;
break;
case 值3:
執行語句3;
break;
........
deafult:
執行語句n;
break;
}
咋們來舉一個簡單的例子
switch (h){
case 2:dayOfweek = "Monday";
break;
case 3:dayOfweek = "Tuesday";
break;
case 4:dayOfweek = "Wednesday";
break;
case 5:dayOfweek = "Thursday";
break;
case 6:dayOfweek = "Friday";
break;
case 0:dayOfweek = "Saturday";
break;
case 1:dayOfweek = "Sunday";
break;
}
當h爲的2時候,執行case2,爲3的時候,執行case3,以此類推;當都不滿足的時候,執行deafult語句
循環結構
循環結構主要是解決重複性執行的代碼,有for語句和While語句
循環有四要素:
1.循環初始化:指的就是循環從什麼時候開始執行
2.循環繼續條件:什麼情況循環繼續執行/反過來說,循環何時結束
3.循環體:就是我們需要多次循環執行的代碼塊
4.循環的步長
for語句(已知循環的次數)
for(循環初始化;循環繼續條件;循環的步長){
循環體
}
咋們舉個例子
*
**
***
****
for(int line=1;line<=4;line++){
for(int i = 1;i<=line;i++){
System.out.println("*");
}
}
第一個for裏面,line表示行數,第二個for裏面,是需要執行的語句,輸出*,在for循環裏面,個人認爲line++和++line是一個意思,別搞混了,都是先+1在執行;
*
**
***
**6-|6-i|
*
for(int i= 1;i<=11;i++){
for(int j = 1;j<=i&&j<=12-i;j++);{
System.out.print("*");
}
System.out.println();
}
while循環(不知循環次數但是已知結束條件)
while 循環的語法如下 :
while( 循環繼續條件){
// 循環體
語句;
}While(true){
}
死循環:循環一直不出來,CPU在一直執行循環體
for(;;){
System.out.println("for死循環")
}
While(true){
System.out.println("While死循環")
}
實戰演練
3.1(代數:解一元二次方程)可以使用下面的公式求一元二次方程ax^2+bx+c=0的兩個根
/*
數據:a b c delt r1 r2
步驟:
1.提示用戶輸入abc三個參數
2.計算delt=b*b-4*a*c
3.判斷delt的值
3.1 delt>0
輸出兩個解
3.2 delt==0
輸出一個解
3.3 delt<0
無實數解
*/
import java.util.Scanner;
class Demo03_01{
public static void main(String[] args){
//1.提示用戶輸入abc三個參數
Scanner scanner=new Scanner(System.in);
System.out.print("請輸入a,b,c:");
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
//2.計算delt=b*b-4*a*c
double delt=b*b-4*a*c;
//3.判斷delt的值
if(delt>0){
double r1=(-b+Math.sqrt(delt))/(2*a);
double r2=(-b-Math.sqrt(delt))/(2*a);
System.out.printf("r1=%.2f,r2=%.2f",r1,r2);
}else if(delt==0){
double r=(-b-Math.sqrt(delt))/(2*a);
System.out.printf("r=%.2f",r);
}else{
System.out.println("無實數解!");
}
}
}
3.2
可以使用編程練習題1.13中給出的Crammer規則解線性方程組
/*
數據:a b c d e f
步驟:
1.輸入數值
2.判斷k的值
3.計算x y
*/
import java.util.Scanner;
public class Text02 {
public static void main(String [] args) {
//1.輸入數值
Scanner input = new Scanner(System.in);
System.out.print("請輸入a、b、c、d、e、f的值:");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();
//2.判斷k的值
double k = a * d - b * c ;
if(k == 0)
System.out.println("無結果 ");
else {
//3.計算x y
double x = (e * d - b * f)/(a * d - b * c);
double y = (a * f - e* c)/(a * d - b * c);
System.out.println("x的值:" + x +"y的值:" + y);
}
}
}
3.3編寫一個程序,提示用戶輸入代表今天日期的數字(週日爲0)
數據:今天的周幾 未來的天數 未來的周幾
(今天的周幾+未來的天數)%7=未來的周幾
步驟:
1.輸入今天是周幾
2.輸入未來的幾天
3.打印未來的幾天是周幾
import java.util.Scanner;
public Class Test3{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("今天是周幾:");
int today = in.nextInt();
System.out.println("未來的幾天:";)
int future = in.nextInt();
int futureDay = (today + future)%7;
String todayStr="";
String futureDayStr="";
if(today==0){
todayStr="週日";
}else if(today==1){
todayStr="週一";
}else if(today==2){
todayStr="週二";
}else if(today==3){
todayStr="週三";
}else if(today==4){
todayStr="週四";
}else if(today==5){
todayStr="週五";
}else if(today==6){
todayStr="週六";
}
if(futureDay==0){
futureDayStr="週日";
}else if(futureDay==1){
futureDayStr="週一";
}else if(futureDay==2){
futureDayStr="週二";
}else if(futureDay==3){
futureDayStr="週三";
}else if(futureDay==4){
futureDayStr="週四";
}else if(futureDay==5){
futureDayStr="週五";
}else if(futureDay==6){
futureDayStr="週六";
}
System.out.println("今天是"+todayStr+",未來的日子是"+futureDayStr);
}
}
}
}
3.4
編寫一個程序,提示用戶輸入一個三位的整數,然後確定它是否是迴文數字,從左到右,以及從右到左都是一樣的話,這個數字稱爲迴文數字。
步驟:1.先輸入一個Sum=0
2.sum=sum*10+num%10
num/=10
3.幾位數字就上面的的循環語句就循環幾次
import java.util.Scanner;
pubilc class Test4{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入一個數字:");
int num = scanner.nextInt();
int temp = num;
int sum = 0;
sum = sum*10+num%10;
num/=10;
sum = sum*10+num%10;
num/=10;
sum = sum*10+num%10;
num/=10;
if(sum==temp){
System.out.println("是迴文");
}
else{
System.out.println("不是迴文");
}
}
3.5 假設你想開發一個玩彩票的遊戲,程序隨機產生一個兩位數的彩票
數據:電腦隨機產生一個兩位數字 用戶輸入一個兩個數字
要是用戶輸入的數字與電腦的數字完全匹配,獎金10000
用戶的輸入與電腦的數字,順序不匹配,數字一樣 獎金3000
用戶輸入與電腦之匹配一個數字 獎金1000
步驟:
1.提示用戶輸入一個兩位數字
2.計算機隨機產生一個兩位數字
3.將兩者進行對比 評選獎金
10000 com == usr
3000 com/10 == usr%10 && com%10 == usr/10
1000 com/10 == usr/10 || com/10 ==usr%10 || com%10 == usr/10 || com%10 == usr%10
import java.util.*;
public class Test5{
pubilc static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("請輸入你得彩票碼:");
int usr = in.nextInt();
Random sc = new Random();
int com = sc.Random(100);
if(usr>=10 && usr<=99){
if(usr==com){
System.out.println("恭喜你獲得獎金10000美元");
}
else if(com/10 == usr%10 && com%10 == usr/10)
{
System.out.println("恭喜你獲得獎金3000美元");
}
else if(com/10 == usr/10 || com/10 ==usr%10 || com%10 == usr/10 || com%10 == usr%10)
{
System.out.println("恭喜你獲得獎金1000美元");
}
else{
System.out.println("很遺憾你沒有獲獎");
}
}else{
System.out.println("您輸入的數字有誤,請重新輸入");
}
}
}
3.6剪刀,石頭,布的遊戲,編寫可以玩流行的剪刀,石頭,布遊戲的程序
數據:電腦隨機產生的一個數字 用戶輸入一個數字
步驟:
1.提示用戶輸入一個數字
2.計算機隨機產生一個數字
3.將兩個數字進行對比,分輸贏
平局 com==usr
用戶贏 usr=0 com=2 || usr=1 com=0 || usr=2 com=1
用戶輸 剩下的都是用戶輸
import java.util.Scanner;
pubilc class Test6{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入 剪刀0 石頭1 布2:");
int usr = scanner.nextInt();
Random random = new Random();
int com = random.nextInt(3);/*Random隨機函數*/
String usrStr="";
String comStr="";
switch(usr){
case 0:
usrStr = "剪刀";
break;
case1:
usrStr = "石頭";
break;
case 2:
usrStr = "布";
break;
}
swith(com){
case 0:
comStr = "剪刀";
break;
case 1:
comStr = "石頭";
break;
case 2:
comStr = "布";
break;
}
if(usr==com){
System.out.printf("用戶是%s,電腦是%s,平局",usrStr,comStr);
}else if(usr==0&&com==2 || usr==1&&com==0 || usr==2&&com==1){
System.out.printf("用戶是%s,電腦是%s,用戶贏",usrStr,comStr);
}else{
System.out.printf("用戶是%s,電腦是%s,用戶輸",usrStr,comStr);
}
}
}
3.7澤勒一致性是由克里斯汀.澤勒開發的用於計算某天星期幾的算法
步驟:1.你需要對1月和2月進行改變,因爲他說的是上一年的13和14
2.輸入年,月,日
import java.util.*;
pubilc class Test7{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("請輸入年份:");
int year = in.nextInt();
System.out.println("請輸入月份:");
int month = in.nextInt();
System.out.println("請輸入幾號:");
int day = in.nextInt();
switch (month){
case 1:month = 13;
year--;
break;
case 2:month = 14;
year--;
break;
}
int j = Math.abs (year/100),k = year%100;
int h = (day+(int)(26*(month+1)/10)+k+(int)(k/4)+(int)(j/4)+5*j)%7;
String dayOfweek = null;
switch (h){
case 2:dayOfweek = "Monday";
break;
case 3:dayOfweek = "Tuesday";
break;
case 4:dayOfweek = "Wednesday";
break;
case 5:dayOfweek = "Thursday";
break;
case 6:dayOfweek = "Friday";
break;
case 0:dayOfweek = "Saturday";
break;
case 1:dayOfweek = "Sunday";
break;
}
System.out.println ("Day of the week is "+dayOfweek);
}
}
3.8幾何,判斷點是否在圓內?
.
public class Test8{
pubilc static void mian(String[] args){
Scanner in =new Scanner(System.in);
System.out.println("請輸入點的座標:");
Double x = in.nextDouble();
Double y = in.nextDouble();
Double x0 = 0;
Double y0 = 0;
Double r = 10;
Double dis = Math.sqrt(Math.pow(x-x0,2)+Math.pow(y-y0,2));
if(dis > r){
System.out.println("點在圓外");
}
else if(dis = r){
System.out.println("點在圓上"):
}
else{
System.out.println("點在圓內");
}
}
}
3.9幾何:點是否在三角形內,假設一個直角三角形在一平面上
用到高中的知識點,那條直線的座標,Y=-1/2x+200
1.提示用戶輸入一個點的座標
2.先大致判斷一下座標的範圍
3.在精確的判斷座標的範圍
import java.util.Scanner;
public class Demo15 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter x,y:");
double x=scanner.nextDouble();
double y=scanner.nextDouble();
if(x>=0&&x<=200){
if((200-x)/y>=2){
System.out.println("in");
}
}
System.out.println("out");
}
}
3.10幾何:兩個矩形
/*
數據:大矩形的中點座標、寬、高;小矩形的寬、高、中點座標
步驟:
1.先輸入大矩形的中心,寬和高
2.再輸入小矩形的中心,寬和高
3.找小矩形在大矩形中極限
4.找小矩形與大矩形重疊的極限
*/
import java.util.Scanner;
class Demo03_10{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1.先輸入大矩形的中心,寬和高
System.out.print("請輸入第1個矩形的信息:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double w1=scanner.nextDouble();
double h1=scanner.nextDouble();
//2.再輸入小矩形的中心,寬和高
System.out.print("請輸入第2個矩形的信息:");
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double w2=scanner.nextDouble();
double h2=scanner.nextDouble();
//3.找小矩形在大矩形中極限
double inXMin=x1-(w1-w2)/2;
double inXMax=x1+(w1-w2)/2;
double inYMin=y1-(h1-h2)/2;
double inYMax=y1+(h1-h2)/2;
//4.找小矩形與大矩形重疊的極限
double outXMin=x1-(w1+w2)/2;
double outXMax=x1+(w1+w2)/2;
double outYMin=y1-(h1+h2)/2;
double outYMax=y1+(h1+h2)/2;
if(x2>=inXMin&&x2<=inXMax&&y2>=inYMin&&y2<=inYMax){
System.out.println("小矩形在大矩形裏面!");
}else if(x2<=outXMin||x2>=outXMax||y2<=outYMin||y2>=outYMax){
System.out.println("小矩形在大矩形外面!");
}else{
System.out.println("小矩形和大矩形相交!");
}
}
}
3.11統計正數和負數的個數,然後計算這些數字的平均值
步驟:1.輸入一個數字
2.判斷他的正負
3.進行相加
4.輸出
Class Test11{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int sum=0;//總和
int positives=0;//正數的個數
int negatives=0;//負數的個數
System.out.println("請輸入若干個個數字:");
While(true){
int num=in.nextInt();
if(num!=0){
sum+=num;
if(num>0){
positives++;
}else{
negatives++;
}
}else{
break; //跳出循環
}
}
if(positives+negatives==0){
System.out.println("沒有其他數字的輸入,除了0");
} else{
System.out.println("正數的個數"+postives);
System.out.println("負數的個數"+negatives);
System.out.println("輸入的總數"+positives+negatives);
System.out.println("平均值"+sum/(positives+negatives));
}
}
}
3.12設輸入的兩個整數位n1和n2.已知1是一個公約數,但是它
步驟:1.輸入兩個整數
2.判斷最大公約數,較方便的,需要以小的那個數爲基準
3.從小的數反着除,兩者同時取餘爲0,證明爲他們的最大公約數
4.輸出
Class Test12
{
public static void main (String[] args){
Scanner in =new Scanner(System.in)
System.out.println("請輸入兩個數字:");
int n1=in.nextInt();
int n2=in.nextInt();
int gcd=1;
for(int i=n1<n2?n1:n2;i>=1;i--){
if(n1%i==0&&n2%i==0){
gcd=i;
break;
}
}
System.out.println(gcd);
}
}
3.13
編寫一個程序,讀入一個整數,然後以升序現實它的所有最小因子,例如,輸入的整數是120,那麼輸出的就應是2,2,2,3,5
步驟:1.輸入一個整數
2.從2開始除,如果,爲0,就繼續除2,當除2有餘數之時,進行下一位的除,直到爲1
3.輸出
Class Test13{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("請輸入一個數字:");
int num = in.nextInt();
while(true){
for(int i=2;i<=unm;i++){
if(num%i==0){
System.out.println(i+"");
num=num/i;
break;
}
}
if(num==1){
break;
}
}
}
}