正向循环还是反向循环取决于生成的随机数?

时间:2014-02-09 21:51:52

标签: java

我正在努力解决这个问题,但似乎无法实现。我很欣赏这方面的任何指示。我想要一个方法来生成一个 1到10之间的随机数,然后返回一个本地String []数组“numArr”的四个元素的数组,这是该方法的本地成员。所以,如果 生成的随机数为“8”,然后我将从“8”开始反向循环并返回:

“八” “七” “六” “五”

我有一个局部变量“len”设置为4,“len”变量确定需要返回多少个“numArr”元素。我确保生成随机 number小于(numArr.length - len)所以如果生成随机数是8,我的逻辑将确定并使用从8开始的反向循环。因为如果a 调用forward循环,它只会被执行两次,因为“numArr”只有10个元素。

我正在运行反向循环:

如果当前随机数> (numArr.length - len)。因此,如果随机数是7,8,9或10,逻辑将反向循环运行。

我目前没有返回任何内容,我只是想在返回任何内容之前确保逻辑完全正常。

public static void geNums(){

    //LENGTH IS SET TO 4
    int len = 4;

    //COUNTER
    int counter = 0;

    //LOCAL ARRAY WITH JUS 10 ELEMENTS
    String[] numArr = {"one","two","three","four","five","six","seven","eight","nine","ten"};       

    //RANDOM NUMBER IS GENERATED BETWEEN 1 AND 10
    Random randNum = new Random();      
    int curRandom = randNum.nextInt(9) + 0; //0 TO 9 || 1 TO 10

    //CHOICE ARRAY
    String[] choices = new String[ len ];   //LENGTH IS SET TO 4

    //DISPLAY CURRENTLY GENERATED RANDOM NUMBER
    System.out.println("Current Random Number: " + curRandom);
    System.out.println("-------------------------");

    if(curRandom > (numArr.length - len)){

        //REVERSE LOOP
        for(int i = curRandom; i >= len; i--){

        //BREAK IF COUNTER IS MORE THAN LEN
        if(counter > len){
            --counter;
            break;
        }
        choices[i] = numArr[i];     //POPULATE CHOICE ARRAY
        ++counter;
    }
    else{
        //FORWARD LOOP
        for(int i = curRandom; i < len; i++){
            choices[i] = numArr[i]; //POPULATE CHOICE ARRAY
        }       
    }

    //DISPLAY CHOICE ARRAY ELEMENTS FOR DUBUGGING
    for(int j = 0; j < choices.length; j++){
        System.out.println(choices[j]);
    }

}//METHOD

1 个答案:

答案 0 :(得分:1)

根据我的理解,你会在1..10之间产生一个数字。如果它大于5,则将其字符串名称保存到数组中。我已经修改了你的代码,希望你能理解。 我发现的第一个问题是(int i = curRandom; i&gt; = len; i--).... choices[i] = numArr [i]。记住你的数组(选项)是长度为4的声明:String[] choices = new String[ len ]; //LENGTH IS SET TO 4那么,如果生成数字8怎么办?您从8开始i,当您致电choices[i]时,choices[8]等于IndexOutOfBoundException 它会给//LENGTH IS SET TO 4 int len = 4; //COUNTER int counter = 0; //LOCAL ARRAY WITH JUS 10 ELEMENTS String[] numArr = {"one","two","three","four","five","six","seven","eight","nine","ten"}; //RANDOM NUMBER IS GENERATED BETWEEN 1 AND 10 Random randNum = new Random(); int curRandom = randNum.nextInt(9) + 0; //0 TO 9 || 1 TO 10 //CHOICE ARRAY String[] choices = new String[ len ]; //LENGTH IS SET TO 4 //DISPLAY CURRENTLY GENERATED RANDOM NUMBER System.out.println("Current Random Number: " + curRandom); System.out.println("-------------------------"); if(curRandom > 5){ int aux = curRandom; for(int i = 0;i<len;i++) { choices[i] = numArr[aux]; aux--; } } else { int aux = curRandom; for(int i = 0;i<len;i++) { choices[i] = numArr[aux]; aux++; } } //DISPLAY CHOICE ARRAY ELEMENTS FOR DUBUGGING for(int j = 0; j < choices.length; j++) System.out.println(choices[j]); ,因为你的数组只有4.所以这是一个解决方案:

Current Random Number: 2
-------------------------
three
four
five
six

输出:

{{1}}