简体   繁体   English

素数测试初学者Java

[英]Prime Number test beginner Java

College assignment for testing a positive number (called num) is a Prime Number or not.用于测试正数(称为 num)的大学作业是否为质数。 I must use a Do while loop Ive attempted the code as follows but its failing the test我必须使用 Do while 循环我尝试了如下代码,但测试失败

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<=num);
{
if (flag = true);
{
System.out.println (num + "is a prime number ");
}
else
{
System.out.prinln (num + "is not a prime number ");
}
}
}
}

1. Main function signature 1.主函数签名

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 2.变量名不能以数字开头

int 2 = 0;

you probably mean你可能是说

int i = 0;

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

The format of a do while loop is do while 循环的格式是

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

4. Semicolon means end of statement 4. 分号表示语句结束

while (condition); {
  something();
}

in this case something() is not in your while loop在这种情况下, something()不在您的 while 循环中

5. Watch out for assignment in if 5. 注意分配if

if (flag = true)

this is assigning flag to true .这是将flag分配给true And the condition is always true (since the resulting of the assignment is true).并且条件始终为真(因为赋值的结果为真)。

6. Not System.out.prinln 6. 不是System.out.prinln

It is System.out.println .它是System.out.println Use an IDE.使用 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 < num);
        if (!flag) {
            System.out.println(num + " is a prime number ");
        } else {
            System.out.println(num + " is not a prime number ");
        }
    }
}

I also fixed some logical problem such as我还修复了一些逻辑问题,例如

  1. you should probably increment i instead of num ,你应该增加i而不是num
  2. while (i < num) instead of while (i<=num) , otherwise some (last) i always equals to num , making everything not a prime while (i < num)而不是while (i<=num) ,否则 some (last) i总是等于num ,使一切都不是素数
  3. a number is not a prime when flag is true.当 flag 为真时,数字不是素数。 you should probably invert the if logic.您可能应该反转if逻辑。 Flag is true when you find something that is evenly divisible, meaning the number is not a prime.当您找到可整除的东西时,Flag 为真,这意味着该数字不是素数。

There are better solutions, but I stick with your format.有更好的解决方案,但我坚持你的格式。

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.其次,第 4 行的变量没有名称。

Your format for your do while statement is also, not quite right.你的 do while 语句的格式也不太正确。

Another poster has a good example of a do while loop here: Do-while loop java explanation另一张海报在这里有一个很好的 do while 循环示例: Do-while 循环 java 解释

Also, go over the rules for where semicolons should be placed.此外,请查看分号放置位置的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM