质数测试初学者Java

huangapple go评论75阅读模式
英文:

Prime Number test beginner Java

问题

public class Primes {
    public static void main(String args[]) {
        int num = 1;
        int i = 2;
        boolean flag = false;
        do {
            if (num % i == 0) {
                flag = true;
                break;
            }
            num++;
        } while (i <= num);
        
        if (flag == false) {
            System.out.println(num + " is a prime number");
        } else {
            System.out.println(num + " is not a prime number");
        }
    }
}
英文:

College assignment for testing a positive number (called num) is a Prime Number or not.
I must use a Do while loop
Ive attempted the code as follows but its failing the test

public class Primes {
public static main void (String args[]){
int num = 1;
int 2 = 0;
boolean flag = false;
do {
if (num%i == 1) {
flag = true;
break:
}
num ++

while (i&lt;=num);
{
if (flag = true);
{
System.out.println (num + &quot;is a prime number &quot;);
}
else
{
System.out.prinln (num + &quot;is not a prime number &quot;);
}
}
}
}

答案1

得分: 1

Sure, here is the translated content:

1. Main function signature

它应该是

public static void main(String args[])

而不是

public static main void (String args[])

2. you cannot start a variable name with a number

你不能以数字开头命名变量

int 2 = 0;

你可能想要

int i = 0;

3. do { ... } while(...);

do while 循环的格式是

do {
  something();
} while (condition);

4. Semicolon means end of statement

分号表示语句结束

while (condition); {
  something();
}

在这种情况下,something() 不在你的 while 循环中

5. Watch out for assignment in if

if 中要注意赋值

if (flag = true)

这会将 flag 赋值为 true。而且条件始终为真(因为赋值的结果是 true)。

6. Not System.out.prinln

应该是 System.out.println。使用集成开发环境(IDE)。

最终解决方案

public class Primes {
    public static void main(String args[]) {
        int num = 3;
        int i = 2;
        boolean flag = false;
        do {
            if (num % i == 0) {
                flag = true;
                break;
            }
            i++;
        } while (i < num);
        if (!flag) {
            System.out.println(num + " 是质数 ");
        } else {
            System.out.println(num + " 不是质数 ");
        }
    }
}

我还修复了一些逻辑问题,例如:

  1. 你可能应该递增 i 而不是 num
  2. while (i < num) 而不是 while (i<=num),否则一些(最后一个)i 总是等于 num,使得所有数都不是质数,
  3. 当你找到可以整除的数时,一个数不是质数。你可能应该颠倒 if 逻辑。当你找到可以整除的数时,flag 是真,意味着该数不是质数。

有更好的解决方案,但我保持了你的格式。

英文:

1. Main function signature

it should be

public static void main(String args[])

not

public static main void (String args[])

2. you cannot start a variable name with a number

int 2 = 0;

you probably mean

int i = 0;

3. do { ... } while(...);

The format of a do while loop is

do {
  something();
} while (condition);

4. Semicolon means end of statement

while (condition); {
  something();
}

in this case something() is not in your while loop

5. Watch out for assignment in if

if (flag = true)

this is assigning flag to true. And the condition is always true (since the resulting of the assignment is true).

6. Not System.out.prinln

It is System.out.println. Use an IDE.

Final solution

public class Primes {
    public static void main(String args[]) {
        int num = 3;
        int i = 2;
        boolean flag = false;
        do {
            if (num % i == 0) {
                flag = true;
                break;
            }
            i++;
        } while (i &lt; num);
        if (!flag) {
            System.out.println(num + &quot; is a prime number &quot;);
        } else {
            System.out.println(num + &quot; is not a prime number &quot;);
        }
    }
}

I also fixed some logical problem such as

  1. you should probably increment i instead of num,
  2. while (i &lt; num) instead of while (i&lt;=num), otherwise some (last) i always equals to num, making everything not a prime
  3. a number is not a prime when flag is true. you should probably invert the if logic. Flag is true when you find something that is evenly divisible, meaning the number is not a prime.

There are better solutions, but I stick with your format.

答案2

得分: 0

你有几个不同的问题:

首先,应该是:

public static void main(String[] args){

其次,你在第4行的变量没有名称。

你的do while语句的格式也不太正确。

另外,另一位用户在这里提供了一个关于do while循环的很好的示例:
https://stackoverflow.com/questions/28663628/do-while-loop-java-explanation

还要复习一下分号应该放在哪些位置的规则。

英文:

You have a couple of different issues:

Firstly, it should be:

public static void main(String[] args){

Second, your variable on line 4 doesn't have name.

Your format for your do while statement is also, not quite right.

Another poster has a good example of a do while loop here:
https://stackoverflow.com/questions/28663628/do-while-loop-java-explanation

Also, go over the rules for where semicolons should be placed.

huangapple
  • 本文由 发表于 2020年9月10日 08:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定