我需要得到3的倍數的所有數字。 有人可以幫我寫嗎?以下是我迄今爲止::)你肯定是新來這個是3的倍數的數組編號
import java.io.*;
public class Main {
public static void main(String[] args) {
int[] myList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
int share = myList[0];
for (int i = 0; i < myList.length; i++) {
if (myList[i] % 3 == 0) {
System.out.println(myList[i]); // numbers that are divisible by three
}
if (isPrime(myList[i]) == 1) {
System.out.println(myList[i]); //print prime no.s
}
}
}
static int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i*i <= n; i++) {
if (n%i == 0) {
return 0;
}
}
return 1;
}
}
我嘗試了類似的東西,但我不知道如何寫下來:/ –
請[編輯你的帖子](https://stackoverflow.com/posts/44357819/edit) –
「共享3」是什麼意思?你的意思是3的倍數?另外,你最主要的問題是什麼?這非常廣泛。我發現你的代碼有很多潛在的問題,所以目前還不清楚究竟是什麼導致了問題。 – Carcigenicate