我有点困惑。我想使用while循环将所有从2开始的素数打印到java中输入的数字,并且不想使用n / 2逻辑。 我不知道我的代码有什么问题。请帮助我。
这里是代码:-
var data;
var request = new XMLHttpRequest();
request.open('GET', 'js/tender-total-data.json');
request.onreadystatechange = function () {
if (request.status === 200 && request.readyState === 4) {
data = JSON.parse(request.responseText);
data.stores.forEach(function(key, val){
console.log("The key is: ", key, "The value is; ", val);
val.attendants.forEach(a => console.log("Attendant Name: ",a.attendantName));
console.log(val.storeId);
})
}
};
request.send();
答案 0 :(得分:0)
更简单的方法:
public class Test {
// method to check if each number is prime
public static boolean isPrime(int x) {
for (int i = 2; i < x; i++)
if (x % i == 0) {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int num = scanner.nextInt();
// print out 2 as we wont be checking even numbers.
System.out.println(2);
// iterate till user input
for (int i = 3; i < num; i = i + 2) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
}