gpt4 book ai didi

java - 试图在 Java 中找到第 10,001 个素数(欧拉计划)

转载 作者:行者123 更新时间:2023-11-30 11:07:41 24 4
gpt4 key购买 nike

我是一名 Java 学生,我刚刚开始使用 project euler 来练习代码编写和一般打字。我目前卡在第 7 个问题上,找不到我的代码有什么问题。

     public static void main(String[] args)
{
int num, remainder, divisor;
boolean working = true;
num = 2;

for(int count = 0; count <= 10001; count++)
{

divisor = num - 1;
remainder = 1;
working = true;

while(working != true)
{
remainder = 1;
divisor = num - 1;

while(remainder!=0)
{
remainder = num%divisor;

if(divisor==1);
{working = false;}

divisor--;
}
num = num++;
}
}
//System.out.println(num -1);
}

我得到的输出是 1,我不明白为什么。我对编码很陌生,所以如果我的代码糟糕且效率低下,请不要对我的代码发疯 LOL。

最佳答案

以下导致您的代码出现意外结果。

    if(divisor==1);
{working = false;} // Note: {...} is a statement block and statement
// blocks do not need a terminating semicolon, this
// can be problematic because no syntax errors will
// occur

更改为:

    if(divisor==1) {
working = false;
}

if 语句末尾的分号导致条件语句为空语句。

The Empty Statement

An empty statement does nothing.

EmptyStatement:
;

Execution of an empty statement always completes normally

可在此处找到更多信息:Semicolon at end of if statement.

关于java - 试图在 Java 中找到第 10,001 个素数(欧拉计划),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com