数组

时间:2016-03-03 14:41:47

标签: java arrays

我刚接触编码,目前正在上线。现在我被卡住了,我无法找到任何帮助我继续前进的事情。任务是随机打印给予程序的数量。输入5,程序为您提供5个随机数。之后将它们排成奇数和偶数。这一切都很好。

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

寻求帮助和指导。

抱歉,如果有答案,请在询问之前尽力找到答案。 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(" ");

        }

    }


}



 }

2 个答案:

答案 0 :(得分:0)

您可能误解了一些概念。

  1. 你说你需要为奇数和偶数创建两个不同的数组。但是,在您提供的代码中,我只看到一个名为&#39; array&#39;的数组。注意,array [j]和array [k]指的是同一个数组。 j和k是变量。
  2. 因此,如果您确实需要两个不同的数组用于这两个数字,则需要再创建两个数组。 有几种方法,但这将是您的代码最简单的(可能不一定是最好的):

    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个数组中存储奇数甚至生成的随机数,而不需要第三个公共数组。如果您想知道如何使用这个,请与我们联系。 :)

答案 1 :(得分:0)

这就是我解决任务的方法:

public class 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(" ");

        }

    }

}

}