英文:
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<=num);
{
if (flag = true);
{
System.out.println (num + "is a prime number ");
}
else
{
System.out.prinln (num + "is not a prime number ");
}
}
}
}
答案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 + " 不是质数 ");
}
}
}
我还修复了一些逻辑问题,例如:
- 你可能应该递增
i而不是num, while (i < num)而不是while (i<=num),否则一些(最后一个)i总是等于num,使得所有数都不是质数,- 当你找到可以整除的数时,一个数不是质数。你可能应该颠倒
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 < 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
- you should probably increment
iinstead ofnum, while (i < num)instead ofwhile (i<=num), otherwise some (last)ialways equals tonum, making everything not a prime- a number is not a prime when flag is true. you should probably invert the
iflogic. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论