{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "517921eb39ac410daba02fb3929b3eda",
  "keywords": "算法高阶,数论算法,素数的测试,算法问题选编",
  "title": "打印1000以内的所有素数，并从键盘输入一个正整数，判断是否为素数。",
  "desc": [
    {
      "content": "\n<p>1、主类内至少两个方法&#xff0c;boolean prime(int p)方法和main()方法&#xff0c;prime方法判断参数p是否为素数。 \n2、1000以内的素数放入一个数组再打印。 \n3、单行风格,良好可读性,运行结果正确。</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "import java.util.*;\npublic class Main {\n\tpublic static void main(String[] args) {\n\t\tList<Integer> numList = new ArrayList<>();\n\t\tfor(int i= 2;i<=1000;i++){\n\t\t\tfor(int j=2;j<=i;j++){\n\t\t\t\tif(i%j==0&&j!=i){\n\t\t\t\t\tbreak;\n\t\t\t\t}else if(j==i){\n\t\t\t\t\tnumList.add(i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(\"1000以内的素数\");\n\t\tfor(Integer integer : numList){\n\t\t\tSystem.out.print(integer+\" \");\n\t\t}\n\t\tSystem.out.println();\n\t\tScanner scanner = new Scanner(System.in);\n\t\tint i = scanner.nextInt();\n\t\tfunction function = new function();\n\t\tSystem.out.println(function.prime(i));\n\t}\n}\nclass function{\n\tpublic boolean prime(int p){\n\t\tfor(int i = 2;i<=(p/2);i++){\n\t\t\tif(p%i==0&&p!=i){\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-b1d024a612ef4995986230770db68e62",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600469870"
}