dimanche 22 novembre 2020

returning a string from the string array lengths equal to the input integer

I trying to make a method that will return a String by having a sentence built from the eleloosp array up to j elements. So if j = 3 then the output would be " How about a nice ". I don't want to hardcode the if statements by doing something like if(k == 3) then output this specific thing. How would I do this so I can take the input integer and not hardcode the if statements.

public class TestForEachLoop
{
    private String[] eleloosp = {"How", "about", "a", "nice","tea"};

    public String getCF(int j){
        for(int i = 0; i < eleloosp.length; i++){
            if( j == eleloosp.length){
                System.out.println();
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire