循环语句

循环语句

do-while循环和while循环:do-while语句先执行循环体一次,然后判断循环继续条件
循环体:循环中重复执行的语句部分称为循环体
do-while和while语句:通常用于循环次数不确定的情况
for循环:一般用于循环次数已知的情况
break:立即终止包含break的最内层循环
continue:终止当前迭代

while(循环继续条件){
	循环体
	语句;  
}
do{
	循环体;
	语句;
}while(循环继续条件)
for(初始条件;循环继续条件;每次执行完循环体后的操作){
循环体;
语句;
}

嵌套循环:

package day04;
public class Test4_3 {
	public static void main(String[] args) {
		for(int i=1;i<=6;i++){
			for(int j=1;j<=i;j++){
				System.out.print("*");
			}
		System.out.print('\n');
		}
	}
}
//
//		*
//		**
//		***
//		****
//		*****
//		******
package day04;
public class Test4_3 {
	public static void main(String[] args) {
		for(int i=6;i>=1;i--){
			for(int j=1;j<=i;j++){
				System.out.print("*");
			}
			System.out.print('\n');
		}
		System.out.println("--------------------");
		for(int i=1;i<=11;i++){
			for(int j=1;j<=i&&(i+j)<=12;j++){
				System.out.print('*');
			}
			System.out.println();
		}
		System.out.println("--------------------");
		for(int i=1;i<=9;i++){
			for(int j=1;j<=Math.abs(i-5);j++){
				System.out.print(" ");
			}
			for(int k=1;k<=i&&(k+i<=10);k++){
				System.out.print("* ");
			}
			System.out.println();
		}
		System.out.println("--------------------");
//		
//		
		for(int i=1;i<=11;i++){
			for(int j=1;j<=i&&(i+j)<=12;j++){
				System.out.print('*');
			}
			System.out.println();
		}
		System.out.println("--------------------");
		for(int i=1;i<=9;i++){
			for(int j=1;j<=Math.abs(i-5);j++){
				System.out.print(" ");
			}
			for(int k=1;k<=i&&(k+i)<=10;k++){
				if(k==1||k+i==10||k==i){
					System.out.print("* ");
				}else{
					System.out.print("  ");
				}
				
			}
			System.out.println();	
		}	
	}
}
******
*****
****
***
**
*
--------------------
*
**
***
****
*****
******
*****
****
***
**
*
--------------------
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
--------------------
*
**
***
****
*****
******
*****
****
***
**
*
--------------------
    * 
   * * 
  *   * 
 *     * 
*       * 
 *     * 
  *   * 
   * * 
    * 

编程练习

在这里插入图片描述

/*
 * 读入整数的个数不确定
 * 正数个数
 * 负数的个数
 * 计算数的总和、平均值(浮点数)
 * 输入为0 程序结束
 * */
import java.util.Scanner;
public class Demo4_1{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter an interger,the input ends if it is 0:");
		int zhengshu=0;
		int fushu=0;
		int count=0;
		double sum=0;
		while(true){
			int number = scanner.nextInt();
			sum+=number;//sum=sum+number;
			count++;
			if(number>0){
				zhengshu++;
			}else if(number<0){
				fushu++;
			}else if(count==1&&number==0){
				System.out.println("No numbers are entered except 0");
			}
			else{
				count--;
				System.out.println("The number of positives is "+zhengshu);
				System.out.println("The number of negatives is "+fushu);
				System.out.println("The total is "+sum);
				System.out.println("The average is "+sum/count);
				return;
			}
		}
	}
} 

在这里插入图片描述

/*
 * 用户输入学生的个数
 * 姓名和分数
 * 显示:
 * 最高分 姓名
 * 次高分 姓名
 * */
import java.util.Scanner;
public class Demo4_2 {
	public static void main(String[] args) {
		String theBestname = "";//最高分的姓名
		String theSecondname = "";//次高分的姓名
		double theBestGrade = 0;//最高分
		double theSecondGrade = 0;//次高分
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the number of students:");
		int number = scanner.nextInt();
		while(number>0){
			System.out.println("Enter the name of student: ");
			String name = scanner.next();
			System.out.println("Enter the grade of student: ");
			double grade = scanner.nextDouble();
			number--;
			if(grade>theBestGrade){
				theSecondGrade = theBestGrade;
				theSecondname = theBestname;
				theBestGrade = grade;
				theBestname = name;
			}else if(grade<theBestGrade){
				if(grade>theSecondGrade){
					theSecondGrade = grade;
					theSecondname = name;
				}
			}
		}
		System.out.println("姓名和最高分:"+theBestname +" "+theBestGrade);
		System.out.println("姓名和次高分:"+theSecondname+" "+theSecondGrade);
	}
}

在这里插入图片描述

/*
 * 100<=number<=200
 * number%5==0
 * number%6==0
 * 不能被5和6同时整除
 * 10数为一行 
 * */
public class Demo4_3 {
	public static void main(String[] args) {
		int count = 0;
		for(int number=100;number<=200;number++){
			if((number%5==0||number%6==0)&&(!(number%5==0&&number%6==0))){
				System.out.print(number+" ");
				count++;
				if(count%10==0){
					System.out.println();
				}
			}
		}
	}
}

在这里插入图片描述

public class Demo4_4 {
	public static void main(String[] args) {
		int n=1;
		while(n*n<=12000){
			n++;
		}
		System.out.println(n);
		System.out.println("=========");
		int m=1;
		while(m*m*m<=12000){
			m++;
		}
		m=m-1;
		System.out.println(m);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_5 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the number:");
		int number = scanner.nextInt();
		while(true){
			boolean flag = false;
			for(int i=2;i<=number/2;i++){
				if(number%i==0){
					System.out.print(i+" ");
					number/=i;
					flag=true;
					break;
				}
			}
			if(!flag){
				System.out.println(number);
				break;
			}
		}	
	}
}

4.6
在这里插入图片描述

import java.util.Scanner;
public class Demo4_6 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the line:");
		int line = scanner.nextInt();
		for(int i=1;i<=line;i++){
			for(int k=1;k<=line-i;k++){
				System.out.print("   ");
			}
			for(int j=-i;j<=i;j++){
				if(j!=0&&j!=1){
					System.out.printf("%3d",Math.abs(j));
				}
			}
			System.out.println();
		}
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_8 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the line:");
		int line = scanner.nextInt();
		for(int i=1;i<=line;i++){
			for(int k=1;k<=line-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, -j+i-1));
				}
			}
			System.out.println();
		}
	}
}

在这里插入图片描述

public class Demo4_10 {
	public static void main(String[] args) {
		double PI = 1.0;
		//方法一:
//		for(int i=1;i<=10000;i++){
//			if(i%2==1){
//				PI = PI-1.0/(2*i+1);
//			}else{
//				PI = PI+1.0/(2*i+1);
//			}
//		}
//		方法二:
//		double flag = -1.0;
//		for(int i=1;i<=10000;i++){
//			PI = PI+flag/(2*i+1);
//			flag = -flag;
//		}
//		方法三:
		for(int i=1;i<=10000;i++){
			PI = PI-Math.pow(-1, i+1)/(2*i+1);
		}
		PI*=4;
		System.out.println(PI);
	}
}

在这里插入图片描述

public class Demo4_11 {
	public static void main(String[] args) {
		double e=1.0;
		for(int i=1;i<=10000;i++){
			double res=1;
			for(int j=1;j<=i;j++){
				res*=j;
			}
			e=e+1/res;
		}
		System.out.println(e);
	}
}

在这里插入图片描述

public class Demo4_12 {
	public static void main(String[] args) {
		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();
				}
			}
		}
		System.out.println("\n"+count);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_13 {
	public static void main(String[] args) {
		int year;
		int week;
		Scanner scanner = new Scanner(System.in);
		year = scanner.nextInt();
		week = scanner.nextInt();
		int m,y,k,j,h;
		String monthStr="";
		String weekStr=""; 
		for(int month = 1;month<=12;month++){
			m=month;
			y=year;
			if(m==1||m==2){
				m+=12;
				y--;
			}
			k=y%100;
			j=y/100;
			h=(1+26*(month+1)/10+k+k/4+j/4+5*j)%7;
			switch(month){
				case 1:
					monthStr = "January";
					break;
				case 2:
					monthStr = "February";
					break;
				case 3:
					monthStr = "March";
					break;
				case 4:
					monthStr = "April";
					break;
				case 5:
					monthStr = "May";
					break;
				case 6:
					monthStr = "June";
					break;
				case 7:
					monthStr = "July";
					break;
				case 8:
					monthStr = "Augest";
					break;
				case 9:
					monthStr = "September";
					break;
				case 10:
					monthStr = "October";
					break;
				case 11:
					monthStr = "November";
					break;
				case 12:
					monthStr = "December";
					break;
			}
			switch(h){
				case 0:
					weekStr = "Sunday";
					break;
				case 1:
					weekStr = "Monday";
					break;
				case 2:
					weekStr = "Tuesday";
					break;
				case 3:
					weekStr = "Wednessday";
					break;
				case 4:
					weekStr = "Thursday";
					break;
				case 5:
					weekStr = "Friday";
					break;
				case 6:
					weekStr = "Saturday";
					break;
			}
			System.out.println(monthStr+" 1, "+year+" is "+weekStr);
		}
	}
}

在这里插入图片描述

public class Demo4_15{
	public static void main(String[] args) {
		int sum=0;
		for(int i=1;i<=10000;i++){
			for(int j=1;j<=i/2;j++){
				if(i%j==0){
					sum+=j;
				}
			}
			if(sum==i){
				System.out.println(sum);
			}
			sum=0;
		}
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_16 {
	public static void main(String[] args) {
		int pCount=0;
		int cCount=0;
		Scanner scanner = new Scanner(System.in);
		while(true){
			System.out.println("请输入:0:剪刀 1:石头 2:布");
			int c = scanner.nextInt();
			int p = (int)(Math.random()*3);
			if(c==p){
				System.out.println("平局,请继续!");
			}
			if(p==0&&c==1){
				System.out.println("电脑赢");
				cCount++;
			}
			if(p==0&&c==2){
				System.out.println("玩家赢");
				pCount++;
			}
			if(p==1&&c==0){
				System.out.println("玩家赢");
				pCount++;
			}
			if(p==1&&c==2){
				System.out.println("电脑赢");
				pCount++;
			}
			if(p==2&&c==0){
				System.out.println("电脑赢");
				pCount++;
			}
			if(p==2&&c==1){
				System.out.println("玩家赢");
				pCount++;
			}
			if(pCount==2||cCount==2){
				break;
			}
		}
		if(pCount==2){
			System.out.println("最终玩家获胜!");
		}else{
			System.out.println("最终电脑获胜!");
		}
	}
}

在这里插入图片描述

public class Demo4_17 {
	public static void main(String[] args) {
		double sum=0;
		for(int i=1;i<=624;i++){
			sum=sum+1.0/(Math.sqrt(i)+Math.sqrt(i+1));
		}
		System.out.println(sum);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_18 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个十进制整数:");
		int number = scanner.nextInt();
		String str = "";
//		while(true){
			十进制到二进制
//			str = number%2+str;
//			number/=2;
//			if(number==0){
//				break;
//			}
//		}
//		System.out.println(str);
//		while(true){
			十进制到八进制
//			str = number%8+str;
//			number/=8;
//			if(number==0){
//				break;
//			}
//		}
//		System.out.println(str);
		while(true){
			int num = number%16;
			if(num>=10){
				switch(num){
					case 10:
						str="A"+str;
						break;
					case 11:
						str="B"+str;
						break;
					case 12:
						str="C"+str;
						break;
					case 13:
						str="D"+str;
						break;
					case 14:
						str="E"+str;
						break;
					case 15:
						str="F"+str;
						break;
				}
			}else{
				str=num+str;
			}
			number/=16;
			if(number==0){
				break;
			}
		}
		System.out.println("0X"+str);
	}
}

4.19
在这里插入图片描述

import java.util.Scanner;
public class Demo4_19 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter numbers:");
		int max=0;
		int count=0;
		while(true){
			int number = scanner.nextInt();
			if(number==0){
				break;
			}
			if(number>max){
				max=number;
				count=1;
				
			}else if(number==max){
				count++;
			}
		}
		System.out.println("The largest number is:"+max);
		System.out.println("The occurrence count of the largest number id :"+count);
	}
}

在这里插入图片描述

public class Demo4_20 {
	public static void main(String[] args) {
		int count=0;
		for(int i=1;i<=7;i++){
			for(int j=i+1;j<=7;j++){
				System.out.println(i+" "+j);
				count++;
			}
		}
		System.out.println("The total number of all combinations id :"+count);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_21 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		double sum=0;
		double powsum=0;
		System.out.println("Enter 10 numbers:");
		for(int i=1;i<=10;i++){
			 double num = scanner.nextDouble();
			 sum+=num;
			 powsum+=num*num;
		}
		double mean = sum/10;
		double sd = Math.sqrt(powsum-sum*sum/10)/9;
		System.out.println("mean:"+mean);
		System.out.println("sd:"+sd);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_22 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the first string:");
		String s1 = scanner.nextLine();
		System.out.println("Enter the second string:");
		String s2 = scanner.nextLine();
		int i=0;
		for(;i<=Math.min(s1.length(), s2.length());i++){
			if(s1.charAt(i)!=s2.charAt(i))
				break;
		}
		System.out.println("["+s1.substring(0,i)+"]");
	}
}

4.23
在这里插入图片描述

import java.util.Scanner;
public class Demo4_23 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter a string:");
		String str = scanner.nextLine();
		int i=0;
		int count=0;
		for(;i<str.length();i++){
			if(str.charAt(i)>=65&&str.charAt(i)<=90){
				count++;
			}
		}
		System.out.println(count);
	}
}

在这里插入图片描述

import java.util.Scanner;
public class Demo4_24 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the String:");
		String str = scanner.nextLine();
		for(int i=0;i<str.length();i++){
			if(i%2==0){
				System.out.print(str.charAt(i));
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值