0

我正在尝试编写一个程序,该程序将返回数组中包含等于插槽索引的值的插槽数,生成随机数后,然后在单独的行上打印数组的每个元素阿斯特里克斯 (假设看起来像这样:)

Enter the array size: 4
a[0] = 2
a[1] = 1 *
a[2] = 0
a[3] = 2
There are 1 slots where the index is the same as the value.

但我失败得很惨,任何朝着正确方向轻推都会受到赞赏。这是我到目前为止所拥有的:

import java.util.Scanner;

public class RandomArray {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

            int i;
    System.out.println("Enter the array size: ");
            i = in.nextInt();

}

/**
 * Fills an array with random integers in the range from zero to one less
 * than the array size.
 * 
 * @param array
 *            the array to fill with random integers
 */
public static void fillArrayWithRandomInts(int array[]) {
    java.util.Random random = new java.util.Random(13);
    for (int i = 0; i < array.length; ++i)
        array[i] = (int) (random.nextDouble() * array.length);
}

public static int countSlotsWithIndexEqualToValue(int array[]) {

    int[] value = new int[13];

    for (int i = 0; i < array.length; i++) {

        if (value[13] == array[13])
            ;

    }
    return value[13];

}

public static void printArray(int array[]) {

}

}
4

3 回答 3

2

您需要以相同的方法将支票和打印结合起来。如果您需要计算有多少这样的组合,您需要添加一个计数器。

public static void countSlotsAndPrint(int[] a) {
    int counter = 0;
    for (int i = 0; i < a.length; i++) {
        System.out.print("a[" + i + "] = " + a[i]); // print the array as per required
        if (i == array[i]){
            System.out.println(" *"); // if index is equal to the value, then print the *            
            counter++;
        } else {
            System.out.println(); // blank line to go to the next line
        }
    }
    System.out.println("There are " + counter + " slots where the index is the same as the value.");
}
于 2013-11-08T07:31:10.723 回答
0
import java.util.*;

public class RandomArray {

public static void main(String[] args) {
    int k;
    int[] arrayOfValues;
    ArrayList<Integer> answer;

    answer = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the array size: ");
    k = in.nextInt();

    // create an array
    arrayOfValues = new int[k];
    // fill an array
    for (int i = 0; i < arrayOfValues.length; ++i)
        arrayOfValues[i] = (int) (Math.random() * arrayOfValues.length);
    // to see an array -> uncomment
    /*
    System.out.println("Random array");
    for(int i : arrayOfValues) {
        System.out.println(i);
    }
    */
    // check an array
    for (int i = 0; i < arrayOfValues.length; i++) {
        if(arrayOfValues[i] == i) {
            answer.add(i);
        }
    }
    // printing result
    System.out.println("Your answer is:");
    for(int i : answer) {
        System.out.println(i);
    }
  }
}

我想,你正在寻找这样的东西?

于 2013-11-08T07:51:39.053 回答
0

对于你想要做的事情,你甚至不需要一个数组(除非这是练习的重点,在这种情况下练习是有缺陷的)。

public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);

        int inputSize;
        System.out.println("Enter the array size: ");
        inputSize = in.nextInt();

        if (inputSize > 0)
        {    
            Random random = new Random();
            int randomNumber;
            int count = 0;
            for (int i = 0; i < inputSize; i++)
            {           
                randomNumber = random.nextInt() % inputSize;

                System.out.print("a[" + i + "] = " + randomNumber);

                if (randomNumber == i)
                {
                    System.out.println(" *");
                    count++;
                }
                else
                {
                    System.out.println();
                }
            }

            System.out.println("There are " + count + " slots where the index is the same as the value.");
        }
        else
        {
            System.out.println("Invalid Input");
        }

    }
于 2013-11-08T08:02:22.107 回答