java控制语句习题03

在这里插入图片描述

public static void main(String[] args) {
	/*1.首先遍历100-1000
	 * 判断
	 * 	可以被5和6整除的数打印出来
	 * 	并且每行10个
	 * */
	int n=0;//每行的数量
	for(int i=100;i<=1000;i++) {
		if(i%5==0&&i%6==0) {
			System.out.print(i+" ");
			n++;
			if(n==10) {
				System.out.println();
				n=0;
			}
		}
	}
}

在这里插入图片描述

public static void main(String[] args) {
	/*问题一:创建变量n从0开始自增,当n²>12000时打印n
	 *问题二:创建变量n从0开始自增,当n²<12000时打印n-1
	 * */
	int n1=0;
	int n2=0;
	while(true) {
		if(Math.pow(n1, 2)>12000) {
			System.out.println(n1);
			break;
		}
		n1++;
	}
	while(true) {
		if(Math.pow(n2, 2)>12000) {
			System.out.println(n2-1);
			break;
		}
		n2++;
	}
}

在这里插入图片描述

public static void main(String[] args) {
	Scanner scanner=new Scanner(System.in);
	System.out.println("please enter a number of 1-15");
	int n=scanner.nextInt();
	for(int i=1;i<=n;i++) {
		for(int k=1;k<=n-i;k++) {
			System.out.print("  ");
		}
		for(int j=-(i-1);j<=i-1;j++) {
			System.out.print(Math.abs(j)+1+" ");
		}
		System.out.println();
	}
}

在这里插入图片描述

public static void main(String[] args) {
	//图案一
	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=6;i>=1;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 k=6-i;k>0;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-1;k++) {
			System.out.print("  ");
		}
		for(int j=1;j<=7-i;j++) {
			System.out.print(j+" ");
		}
		System.out.println();
	}
}

在这里插入图片描述

public static void main(String[] args) {
	for(int i=1;i<=8;i++){
		for(int k=1;k<=8-i;k++){
			System.out.print("    ");
		}
		for(int j=-(i-1);j<=i-1;j++){
			System.out.printf("%-4d",(int)(Math.pow(2,i-1-Math.abs(j))));
		}
		System.out.println();
	}
}
发布了9 篇原创文章 · 获赞 0 · 访问量 394

猜你喜欢

转载自blog.csdn.net/qq_44586668/article/details/104920986