Java循环嵌套练习题

//代码中row代表要输出的行数,如上述题中则为4,求素数时则为100.
import java.util.Scanner;
public class StarOne{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("please input row number:");
		int row = input.nextInt();

                
                //task1
		for(int i = 1; i <= row;i++){
			for(int j = 1;j <= row - i +1;j++){//output star
				System.out.print("*");
			}
			for(int j = 1;j <= i - 1;j++){//output space
				System.out.print(" ");
			}
			System.out.println();
		}


                //task2
		for(int i = 1; i <= row;i++){
			for(int j = 1;j <= i - 1;j++){//output space
				System.out.print(" ");
			}
			for(int j = 1;j <= row - i + 1;j++){//output star
				System.out.print("*");
			}
			System.out.println();
		}


                //task3
		for(int i = 1;i <= row;i++){
			for(int j = 1;j <=row - i;j++){
				System.out.print(" ");
			}
			for(int j= 1;j <= i;j++){
				System.out.print("* ");//输出不再是单个*,而是*$。$代指空格.
			}
			System.out.println();
		}


                //task4
		for(int i = 1; i <= row;i++){
			for(int j = 1;j <= row - i ;j++){//output space
				System.out.print(" ");
			}
			for(int j = 1;j <= 2 * i - 1;j++){//output star
				System.out.print("*");
			}
			System.out.println();
		}


                //task5
		for(int i = 1;i <= row;i++){
			if(i ==1){
				for(int j = 1;j <= 2 *row - 1;j++){
				System.out.print("*");
				}
			}else{
				for(int j = 1;j <= row - i + 1;j++){
					System.out.print("*");
				}
				for(int j = 1;j <= 2 * (i - 1) - 1;j++){
					System.out.print(" ");
				}
				for(int j = 1;j <= row - i + 1;j++){
					System.out.print("*");
				}
			}
			System.out.println();

		}


                //task6
		for(int i = 1;i <=row;i++){
			for(int j = 1;j <=row - i;j++){
				System.out.print(" ");
			}
			for(int j = 1;j <= i;j++){
				System.out.print(j);
			}
			for(int j = i - 1;j>=1;j--){
				System.out.print(j);
			}
			//for(int j = 1;j <=row - i;j++){
			//	System.out.print("\t");
			//}(可不写这几行,对结果无影响)
			System.out.println();
		}


                //task7
		for(int i = 1;i <= row * row;i++){
			System.out.print(i + "\t");//\t制表符
			if(i % row == 0){
				System.out.println();
			}
		}


                //task8
		for(int i = 0;i < row;i = i + 2){//(也可分奇偶数行来实现)
			for(int j = row * i + 1 ;j <= row * ( i + 1);j++){
				System.out.print(j + "\t");//\t制表符
				if(j % row ==0){
					System.out.println();
				}
			}//本方法思路为将第一行第二行看成一体。
			for(int j = row * (i + 2);j > row * (i + 1);j--){
				System.out.print(j + "\t");
				if(j == row * (i + 1) + 1){
					System.out.println();
				}
			}
		}


                //task8-1
                for(int i = 1;i <= row;i++){
                    if(i % 2 != 0){
                        for(int j = row * (i - 1) + 1;j <= row * i;j++){
                            System.out.print(j + "\t");
                        }
                    }else{
                        for(int j = row * i;j >= row * (i - 1) + 1;j--){
                            System.out.print(j + "\t");
                        }
                    }
                    System.out.println();
                }


                //task9
		for(int i = 1;i <= 9;i++){
			for(int j = 1;j <= i;j++){
				System.out.print(j + "*" + i +"="+ j * i + "\t");
			}
			System.out.println();
		}
		

                //task10
                int count = 0;
		for(int i = 2;i <=row;i++){
			boolean check = true;
			for(int j = 2;j <= Math.sqrt(i);j++){
				if(i % j == 0){
					System.out.print(i + "不是素数");
					check = false;
					break;
				}
			}
			if(check == true){
				System.out.print(i + "是素数");
				count++;
			}
			System.out.println();
		}
		System.out.print("2到"+ row +"间素数个数为:" + count);
	}
}
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值