2016-03-03 41 views
-3

我是新来编码的,现在参加在线课程。现在我被卡住了,我无法找到任何帮助我们继续前进的东西。任务是随机打印提供给程序的数量。输入5,程序给你5个随机数字。之后,他们在奇数和偶数排序。这很好。阵列中的Java Count元素

我的问题是,我不知道如何计算每个数组中的数字(不是将它们加在一起),而是计算有多少奇数,以及有多少偶数。

寻求帮助和指导。

P.s对不起,如果有一个答案在那里,尽我所能找到它之前问。 d.s

import java.util.*; 
    public class RandomNbrs { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    Scanner scan = new Scanner(System.in); 

    System.out.println("How many random numbers do you want? (0-999)"); 
    int numb = scan.nextInt(); 

    System.out.println("Your random numbers:"); 

    int array[] = new int[numb]; 

    for(int i =0; i < numb; i++){ 

     array [i] = (int) (0 + 1000 * Math.random()); 
     System.out.print(array[i]); 
     System.out.print(" "); 


    } 
    System.out.println(); 
    System.out.println(); 
    System.out.println("Even numbers: "); 


    for(int j =0; j < numb; j++){ 

     if(array[j] %2 == 0){ 


      System.out.print(array[j]); 
      System.out.print(" "); 
     } 


    } 


    System.out.println(); 
    System.out.println(); 
    System.out.println("Odd numbers: "); 

    int oddNbr = 0; 

    for(int k =0; k < numb; k++){ 

     if(array[k] %2 == 1){ 


      System.out.print(array[k]); 
      System.out.print(" "); 

     } 

    } 


} 



} 
+0

那么你已经有奇数即'oddNbr'的计数器。您只需在检查条件的'if'条件内执行'oddNbr ++'来增加它。为偶数制作类似的计数器,并做同样的事情。 – user2004685

+0

欢迎来到SO。请修改您的帖子以使用正确的拼写和标点符号。这个和其他提示可以在[询问帮助页面](http://stackoverflow.com/help/how-to-ask)上找到。 –

+0

@ user2004685,谢谢!我已经尝试过了。但我似乎无法做到。我想要一个打印的数字为阵列的数量。问题是我做错了。 – forTheLoveOfJava

回答

0

我这是怎么解决的任务:

公共类TESTTEST {

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    int oddCounter=0; 
    int evenCounter=0; 

    Scanner scan = new Scanner(System.in); 

    System.out.println("How many random numbers do you want? (0-999)"); 
    int numb = scan.nextInt(); 

    System.out.println("Your random numbers:"); 

    int array[] = new int[numb]; 

    for(int i =0; i < numb; i++){ 

     array [i] = (int) (0 + 1000 * Math.random()); 

     System.out.print(array[i]); 
     System.out.print(" "); 


    } 
    System.out.println(); 
    System.out.println(); 


    for(int j =0; j < numb; j++){ 

     if(array[j] %2 == 0){ 
      evenCounter++; 

     } 

    } 

    System.out.println("Dessa " + evenCounter + " Even numbers: "); 
    for(int j =0; j < numb; j++){ 


     if(array[j] %2 == 0){ 


      System.out.print(array[j]); 
      System.out.print(" "); 

     } 

    } 


    System.out.println(); 
    System.out.println(); 




    for(int k =0; k < numb; k++){ 

     if(array[k] %2 == 1){ 
      oddCounter++; 

     } 

    } 
    System.out.println("Dessa " + oddCounter + " Odd numbers: "); 
    for(int k =0; k < numb; k++){ 

     if(array[k] %2 == 1){ 

      System.out.print(array[k]); 
      System.out.print(" "); 

     } 

    } 

} 

}

0

有几个概念你可能误解了。

  1. 你说你需要为奇数和偶数创建两个不同的数组。但是,在您提供的代码中,我只看到一个名为'array'的数组。请注意,数组[j]和数组[k]引用相同的数组。 j和k是变量。

所以,如果你真的需要两个不同的数组,两个数字,你需要创建两个更多的数组。 有几种方法,但是这是最简单的(不一定是最好的,虽然)通过你的代码去:

import java.util.*; 
public class RandomNbrs 
{public static void main(String[] args) { 
// TODO Auto-generated method stub 

Scanner scan = new Scanner(System.in); 

System.out.println("How many random numbers do you want? (0-999)"); 
int numb = scan.nextInt(); 

System.out.println("Your random numbers:"); 

int array[] = new int[numb]; 
int temp,countOdd=0,countEven=0; 

for(int i =0; i < numb; i++){ 
//I am storing the random generated number in a temporary variable 
//Then i check if it is odd or even and increase the count by using a counter 

    temp= (int) (0 + 1000 * Math.random()); 
    if (temp%2 == 0): 
     countEven++; 
    else 
     countOdd++; 
    array[i]=temp 

    System.out.print(array[i]); 
    System.out.print(" "); 


} 
System.out.println(); 
System.out.println(); 
//Here i am creating two new arrays as you specified. 
//if you just want to print the odd and even numbers AND their couns, you dont necessarily need to make these two arrays. 
int arrayOdd[] = new int[countOdd]; 
int arrayEven[]=new int[countEven]; 

//Counter for accessing the elements of the two new arrays 
int counter1=0,counter2=0; 


//Filling the two new arrays with odd and even numbers from the prev array 
for(int j =0; j < numb; j++){ 

    if(array[j] %2 == 0){ 

     arrayEven[counter1++]=array[j]; 
    } 

    else 
     arrayOdd[counter2++]=array[j]; 


} 

    //Print odd 
System.out.println("Odd:"); 

for(int k =0; k < countOdd; k++){ 

     System.out.print(arrayOdd[k]); 
     System.out.print(" "); 

} 
System.out.println("Total odd numbers="+countOdd); 
//or arrayOdd.length() also gives the count. 
//Similar for printing even numbers 
}} 

此外,您还可以使用ArrayList,您可以动态,并直接存储奇数和偶数产生2个数组中的随机数,并且不需要第三个公共数组。让我知道你是否想知道如何做到这一点。 :)

+0

谢谢! @CoderBC,当我重新编写代码时,我将使用这些示例。就像你说的我误解他们是变量而不是不同的数组。 – forTheLoveOfJava