2017-06-04 88 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)