2017-06-04 51 views
-4

我需要得到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; 
    } 
} 
+0

我嘗試了類似的東西,但我不知道如何寫下來:/ –

+0

請[編輯你的帖子](https://stackoverflow.com/posts/44357819/edit) –

+2

「共享3」是什麼意思?你的意思是3的倍數?另外,你最主要的問題是什麼?這非常廣泛。我發現你的代碼有很多潛在的問題,所以目前還不清楚究竟是什麼導致了問題。 – Carcigenicate

回答

0

,你可以這樣寫。

首先,您需要披露共享變量可能用於的內容。

接下來,您需要了解如何正確地使用iterate through your Integer Array。您需要了解Modulus Operator (%)以及如何使用IF statement within a FOR loop。你需要學習how to display a Integer Array Element within the system Console。這將在您的FOR循環內完成。

或者如果你很幸運....一些小丑會爲你做你的功課。

+1

謝謝你多數民衆贊成工作:)如果我有數組從10到100,需要得到所有的素數,可以由1整除,並由它自我? –

+0

你也可以編寫一個函數來檢查一個數是否爲素數。 – ps4

+0

我已經編輯我的答案 – ps4

0

int[] myList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, }; 


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/3; i/myList.length;) { 

     } 
    } 
    System.out.println(i/3); 

} 

} 
2

好吧我想你想要做的是有一組數字,然後你想讓你的程序把這些數字保存到一個數組中。然後在循環完成後打印該數組。

請記住,有了這些問題,你已經知道數組的大小,但是你不知道你會得到多少答案。所以我建議你使用List <>。他們的大小沒有設置,他們可以增長取決於你回來多少答案。

邊注:請確保您使用正確的進口

import java.util.ArrayList; 
import java.util.List; 



public static void main(String[] args) { 

    int [] listOfNumbers = {1,2,3,4,5,6,7,8,9}; // The list of numbers that you have 
    List<Integer> divisableBy3 = new ArrayList<Integer>(); 
    //The loop go through the array list and check the numbers. 

    for (int i = 1; i < listOfNumbers.length; i++) { // You already know that 0 is not divisable by 3. 
     int temp = listOfNumbers[i]; 

     if (temp %3 == 0) { //Checking that you number is divisible by 3 
      divisableBy3.add(temp); 
     } 
    } 

    //For each loop to print out the require information. 
    for (int num : divisableBy3) { 
     System.out.println("Number divisable by 3 : "+num); 
    } 
} 

記住檢查,看看有什麼數字是整除你不使用「/」符號,但您使用模運算符「 %」。

 temp %3 == 0 
1

您應該在嘗試編寫自己的程序代碼之前閱讀java教程和文檔。

您的代碼充滿語法錯誤和邏輯錯誤。

我不建議你開始瞭解核心編程概念之前編程的java:

變量聲明,如果其他條件,循環(while, for),閱讀輸入,輸出打印,邏輯運算符(&& , ||, !),算術運算符(+, -, *, /, %),關係運算符(==, !=, >, <),運營商優先...

這些概念是在幾乎所有的編程語言基礎知識

編程是一種邏輯思維不只是打字

檢查這些引用學習java:

爲了獲得在數組列表的3倍數

可以使用%模運算符來檢查數字一個數字是否能被3整除:

int[] myList = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
for(int i = 0; i < myList.length; i++){ 
    if(myList[i] % 3 == 0){ 
     System.out.println(myList[i]); 
    } 
} 

輸出:

3 
6 
9 
BUILD SUCCESSFUL (total time: 0 seconds)