mardi 31 mars 2020

Using loop to fill Array with 5 given strings

I am looking for a way to fill in the string array when array length is given in a test function. For example when test gives length 100, I want these 5 string values to be printed throughout the array in loop so that it prints until the end. So far I managed to fill the array with first 5, however, I cannot find way to loop this through and test fails when higher array length is given. Thank you for any help!

public class Sid {
    public static String howMuchILoveYou(int nb_petals) {

        String Petals[]=new String[7];
        Petals[0]="zero index"; //cannot skip this for some reason
        Petals[1]="I love you";
        Petals[2]="a little";
        Petals[3]="a lot";
        Petals[4]="passionately";
        Petals[5]="madly";
        Petals[6]="not at all";


        return Petals[nb_petals].toString();
    }
}

TEST

import static org.junit.Assert.assertEquals;
import java.util.Random;
import org.junit.Test;

public class SampleTest {
    @Test
    public void test1() {
        assertEquals("I love you", Sid.howMuchILoveYou(1));
        assertEquals("a little", Sid.howMuchILoveYou(2));
        assertEquals("not at all", Sid.howMuchILoveYou(6));
        assertEquals("I love you", Sid.howMuchILoveYou(343));    //THIS ONE FAILS
    }
}

Aucun commentaire:

Enregistrer un commentaire