itBulls

java关键字break和continue的标签使用,break默认还是跳出当前循环,continue默认还是结束当次循环,如果加上标签就变了,跳到对应标签的地方。

质数的两种求法:

第一种:

public class SelfAndOne 
{
    public static void main(String[] args)
    {
        //求100 以内的质数
        //从2开始
        //一个大于1的自热数,除了1和它自身外,不能被其他自然数整除的数
        long start = System.currentTimeMillis();
        for(int i = 2;i <= 1000000; i++) 
        {    
            //标志 为 假
            boolean flag = false;
            //第一次 i= 2 ;j=2 j=i 不进入第二层循环
            //,第二次循环条件满足才进入
            for(int j = 2; j <= Math.sqrt(i) ;j++) 
            {
                if( i % j == 0 )
                {
                    //说明不是质数
                    flag = true;
                    break;
                }
            }
            //不是质数的 flag 变为true 
            //所有flag 是false 的才是质数
            if(! flag) 
            {                
                System.out.println(i);
            }
        }
        long end = System.currentTimeMillis();
        
        System.out.println("运行时间是:"+(end - start));
        
    }
}

 

第二种:

public class SelfAndOne2 
{
    public static void main(String[] args)
    {
        //求100 以内的质数
        long start = System.currentTimeMillis();
        lable:for(int i = 2;i <= 10; i++) 
        {    
            tag:for(int j = 2; j <= Math.sqrt(i) ;j++) 
            {
                if( i % j == 0 )
                {
                    //说明不是质数
                    continue lable;
//                    break tag;
                }
            }
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        
        System.out.println("运行时间是:"+(end - start));
        
    }
}

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-06-15
  • 2022-03-05
  • 2022-03-02
  • 2022-02-25
  • 2021-04-28
猜你喜欢
  • 2022-01-06
  • 2022-12-23
  • 2021-07-23
  • 2023-03-22
  • 2022-01-07
  • 2022-03-08
  • 2022-12-23
相关资源
相似解决方案