• Java求素数时出现错误


    Java求素数时出现错误


    1、具体错误如下

    No enclosing instance of type Prime is accessible. Must qualify the allocation with an enclosing instance of type Prime (e.g. x.new A() where x is an instance 
     of Prime).


    2、错误原因

    class PrimNumber
    	{
    		public boolean isPrim(int a)
    		{
    			for(int i=1;i<a/2;i++)
    				if(a%i == 0)
    					return false;
    			return true;
    		}
    	}
           在Java中,类中的静态方法不能直接调用动态方法。

         只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。


    3、解决办法

    public static class PrimNumber
    	{
    		public boolean isPrim(int a)
    		{
    			for(int i=1;i<a/2;i++)
    				if(a%i == 0)
    					return false;
    			return true;
    		}
    	}

    在类名前加上“public static”


  • 相关阅读:
    request、bs4爬虫
    1031 查验身份证
    1029 旧键盘
    1028 人口普查
    1027 打印沙漏
    1026 程序运行时间
    1025 反转链表
    1024 科学计数法
    1022 D进制的A+B
    1021 个位数统计
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13315320.html
Copyright © 2020-2023  润新知