-4

I need to get all number that multiples of 3. Can someone help me to write it? Here is what I have so far:

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 );

}

}
Mark Stewart
  • 2,046
  • 4
  • 22
  • 32

4 Answers4

2

Okay What I think you want to do is have an array with a set of numbers, then you want your program take those numbers and save them into an array. Then print that array after the loop is done.

Remember that with these kinds of problems you have the known size of the array, but you don't know how many answers you are going to get. So I recommend that you use List<>. Their sizes are not set and they can grow depending on how many answers you are getting back.

Side Note : Make sure that you use the correct imports

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);
    }
}

Remember that checking to see what a number is divisible by you don't use the "/" symbol but you use the modulus operator "%".

       temp %3 == 0 
1

You should read java Tutorials and Documentation before trying to write your own program code.

Your code is full of syntax errors and logic mistakes.

I don't advice you to begin programming java before understanding core programming concepts:

Declaring variables , if else conditions, loops (while, for), reading inputs, printing outputs, logical operators (&& , ||, !), arithmetic operators(+, -, *, /, %), relational operators(==, !=, >, <) , operators precedence ....

These concepts are basics in almost every programming language

Programming is a logic thinking not just typing

Check these references to learn java:

In order to get numbers that multiples of 3 in the array list

you can use % mod operator to check if a number is divisible by 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]);
     }
}

Output:

3
6
9
BUILD SUCCESSFUL (total time: 0 seconds)
Oghli
  • 2,200
  • 1
  • 15
  • 37
0

you can write like this:

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;
    }
}
prisar
  • 3,041
  • 2
  • 26
  • 27
0

:) You are definitely new to this.

To begin with you need to disclose what the share variable is possibly used for.

Next you need learn how to properly iterate through your Integer Array.

You will also need to learn about the Modulus Operator (%) and how to utilize the IF statement within a FOR loop.

You will also need to learn how to display a Integer Array Element within the system Console. This would nomaly be done within your FOR loop.

Or if you're lucky....some clown will do your homework for you.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22