第四章 循環語句
循環語句:
while適合於循環結束條件已知 但是循環次數未知的情況
for 適合於循環次數已知的情況
for和while之間是可以完全互換的
4.1while循環
- while語句:
1.while(循環繼續條件){
需要被循環執行的代碼//循環體
}
2.while(true){
循環體
循環間距
if(循環結束條件){
break;
}
} - 猜數字
import java.util.Scanner;
/*
* while循環必備的條件
* 循環初始化
* 循環體
* 循環間距,改變初始化的值
*
* while (true)
* 循環體
* 循環間距
* if
* break*/
public class Demo_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Guess a magic number");
int number=(int)(Math.random()*101);
Scanner sc=new Scanner(System.in);
while(true){
System.out.print("Enter your guess:");
int num=sc.nextInt();
if(num==number){
System.out.print("yes,the number is"+number);
break;
}else if(num<number){
System.out.println("too low");
}else if(num>number){
System.out.println("too high");
}
}
}
}
4.2do_while循環
do-while:先執行 再判斷
do{
循環體;//循環初始化 循環間距
}while(循環結束條件)
//依然是猜數字用dowhile實現
import java.util.Scanner;
public class Demo_2 {
public static void main(String[]args){
System.out.println("Guess a magic number");
int number=(int)(Math.random()*101);
Scanner sc=new Scanner(System.in);
int num;
do{
System.out.print("Enter your guess:");
num=sc.nextInt();
if(num<number){
System.out.println("too low");
}else if(num>number){
System.out.println("too high");
}
}while(num!=number);
System.out.print("yes,the number is "+number);
}
}
4.3for循環
- for語句:
for( 循環初始化 ; 循環繼續條件 ; 循環間距){
循環體
}
循環初始化->循環繼續條件->循環體->循環間距-> - for嵌套語句:嵌套循環是由一個外層循環或多個內層循環組成的。每當重複執行一次外層循環時再次進入內部循環,然後重新開始。
- 示例:最大公約數
import java.util.Scanner;
public class Domo_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter:");
int num1=sc.nextInt();
int num2=sc.nextInt();
int min=Math.min(num1, num2);
for(int i=min;i>=1;i--){
if(num1%i==0&&num2%i==0){
System.out.print(i);
break;
}
}
}
}
4.4嵌套循環
- 打印直角三角形
public class Demo_test {
/*
*
* *
* * *
* * * *
* * * * * */
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();
}
}
}/
- 打印菱形
public class Demo_test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
*
* *
* * *
* * * *
* * *
* *
*
* */
for(int i=1;i<=8;i++){
for(int k=1;k<=Math.abs(4-i);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&i+j<=8;j++){
System.out.print("*"+" ");
}
System.out.println();
}
}
}
-打印空心菱形
*
* *
* *
* *
* *
* *
*
* */
for(int i=1;i<=8;i++){
for(int k=1;k<=Math.abs(4-i);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&i+j<=8;j++){
if(j==1||j==i||i+j==8){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
4.5break和continue
- break語句
- continue語句
- return語句:僅僅代表結束當前函數 如果有返回值 則在函數結束時 將數據進行返回
- 示例:判斷迴文串
import java.util.Scanner;
public class Demo_huiwen {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number:");
int num=sc.nextInt();
String str="";
int number=num;
while(true){
int n=number%10;
str+=n;
number/=10;
if(number==0){
break;
}
}
if(Integer.parseInt(str)==num){
System.out.print("shi hui wen");
}else{
System.out.print("bu shi hui wen");
}
}
}
- 示例:顯示素數
import java.util.Scanner;
public class IsSushu {
//除以一半本身所有的數都除不盡那就是素數
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
boolean flag=true;
for(int i=2;i<=num/2;i++){
if(num%i==0){
flag=false;
break;
}
}
if(flag==false){
System.out.println("bu shi su shu");
}else{
System.out.println("shi su shu");
}
}
}
小結
1.循環語句有三類: while 循環、 do-while 循環和 for 循環。
2.循環中包含重複執行的語句的部分稱爲循環體。
3.循環體執行一次稱爲循環的一次迭代。
4.無限循環是指循環語句被無限次執行。
5.在設計循環時, 既需要考慮循環控制結構, 還需要考慮循環體。
6.while 循環首先檢査循環繼續條件。 如果條件爲 true, 則執行循環體; 如果條件爲 false, 則循環結
束。
7.do-while 循環與 while 循環類似, 只是 do-while 循環先執行循環體, 然後再檢査循環繼續條件,
以確定是繼續還是終止。
8.while 和 do-while 循環常用於循環次數不確定的情況。
9.標記值是一個特殊的值, 用來標記循環的結束。
10.for 循環一般用在循環體執行次數固定的情況。
11.for 循環控制由三部分組成。 第一部分是初始操作, 通常用於初始化控制變量。 第二部分是循環繼
續條件, 決定是否執行循環體。 第三部分是每次迭代後執行的操作, 經常用於調整控制變量。 通常,
在控制結構中初始化和修改循環控制變量。
12.while 循環和 for 循環都稱爲前測循環( pretest loop), 因爲在循環體執行之前, 要檢測一下循環
繼續條件。
13.do-while 循環稱爲後測循環( posttest loop), 因爲在循環體執行之後, 要檢測一下這個條件。
14.在循環中可以使用 break 和 continue 這兩個關鍵字。
15.關鍵字 break 立即終止包含 break 的最內層循環。
16.關鍵字 continue 只是終止當前迭代。
代碼示例
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int positivescount=0;
int negativecount=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter an integer:");
int flag=0;
while(true){
int num=sc.nextInt();
if(num>0){
positivescount++;
}else if(num<0){
negativecount++;
}else{
break;
}
sum=sum+num;
}
double aver=sum/((positivescount+negativecount)*1.0);
System.out.println("The number of positive is"+positivescount);
System.out.println("The number of negative is"+negativecount);
System.out.println("The total of is "+sum);
System.out.println("The average is "+aver);
}
}=======================================
import java.util.Scanner;
public class Demo_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter num");
int num=sc.nextInt();
String firstname="";
int firstscore=0;
String secondname="";
int secondscore=0;
for(int i=0;i<num;i++){
System.out.print("Enter name and grade:");
String name=sc.next();
int score=sc.nextInt();
if(score>=firstscore){
secondscore=firstscore;
secondname=firstname;
firstscore=score;
firstname=name;
}else if(score>secondscore){
secondscore=score;
secondname=name;
}
}
System.out.print("第一名:"+firstname+firstscore);
System.out.print("第二名:"+secondname+secondscore);
}
}=======================================
public class Demo_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
for(int num=100;num<200;num++){
if((num%5==0||num%6==0)&&(num%5==0^num%6==0)){
System.out.print(num+" ");
count++;
if(count%10==0){
System.out.println();
}
}
}
}
=============================================}
public class Demo_4 {
//n^2大於12000的最小整數n
//n^2小於12000的最大整數n
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=1;
while(n*n<12000){
n++;
}
System.out.println(n);
int m=1;
while(m*m<12000){
m++;
}
System.out.println(m-1);
System.out.println((m-1)*(m-1));
}
}=========================================
import java.util.Scanner;
/*120/2=60----0
* 60/2=30----0
* 30/2=15----0
* 15/2=7----1
* 15/3=5----0
* 5/2=2----1
* 5/3=1----2
* 5/4
* 5*/
public class Demo_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("輸入一個數:");
int num=sc.nextInt();
while(true){
boolean flag=false;
for(int i=2;i<num/2;i++){
if(num%i==0){
System.out.print(i+" ");
num/=i;
flag=true;
break;
}
}
if(!flag){
System.out.print(num);
break;
}
}
}
}====================================
import java.util.Scanner;
/*
* 1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
*/
public class Demo_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
for(int i=1;i<=num;i++){
for(int k=0;k<num-i;k++){
System.out.print(" ");
}
for(int j=-i;j<=i;j++){
if(j!=0&&j!=1){
System.out.print((int)(Math.abs(j))+" ");
}
}
System.out.println();
}
}
}============================================
public class Demo_7 {
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
==========
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
==========
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
==========
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=6;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("==========");
for (int i = 1; i<=6 ; i++) {
for(int j=1;j<=7-i;j++){
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("==========");
for(int i=1;i<=6;i++){
for(int k=1;k<=6-i;k++){
System.out.print(" ");
}
for(int j=i;j>=1;j--){
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("==========");
for(int i=1;i<=6;i++){
for(int k=1;k<i;k++){
System.out.print(" ");
}
for(int j=1;j<=7-i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
==================================}===============
import java.util.Scanner;
public class Demo_8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("請輸入行數:");
int num=sc.nextInt();
for(int i=1;i<=num;i++){
for(int k=1;k<=num-i;k++){
System.out.print(" ");
}
for(int j=-(i-1);j<=(i-1);j++){
if(j<=0){
System.out.printf("%-4d",(int)(Math.pow(2,j+i-1)));
}else{
System.out.printf("%-4d",(int)(Math.pow(2,i-j-1)));
}
}
System.out.println();
}
}
}=======================================
public class Demo_9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum=0;
for(int i=0;i<=48;i++){
sum+=(2*i+1)/((2*i+3)*1.0);
}
System.out.println(sum);
}
}=======================================
public class Demo_10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum=0;
for(int i=1;i<=10000;i++){
sum+=((Math.pow(-1,i+1))/(2*i-1));
}
System.out.print(4*sum);
}
}=======================================
public class Demo_11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum=0.0;
double mult=1;
for(int i=1;i<10000;i++){
for(int j=1;j<=i;j++){
mult*=j;
}
sum+=(1.0/mult);
}
System.out.print(sum+1);
}
}=================================
public class Demo_13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
for(int i=101;i<=2100;i++){
if((i%4==0&&i%100!=0)||i%400==0){
System.out.print(i+" ");
count++;
}
if(count%10==0){
System.out.println();
}
}
}
}