Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method multiple times

Tags:

java

methods

Alright, So I'm learning method in Java, I have to call on a method 10 times to display ten different words (I already have the for loop to call on the method). I just can't figure out how to get it to have 10 different words. This is what I have so far. I hate asking for help so much, but I've been stumped for over a day now.

public static void tenWords(int display){

}

public static void main(String[] args) {

    for(int i=0;i<10;i++){
        tenWords(i);
    }

}
like image 322
Archey Avatar asked May 04 '26 07:05

Archey


2 Answers

just try that:

public class Main{
    private static String[] words = new String[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    public static void tenWords(int display){
            System.out.println(words[display]);
    }

    public static void main(String[] args) {

        for(int i=0;i<10;i++){
            tenWords(i);
        }
    }
}

ice

like image 149
m42e Avatar answered May 05 '26 20:05

m42e


Not giving complete answers, as this looks like a homework // learning question?

From desirable to undesirable:

  • You could have an array or list of words, and return the "display"th item in the array or list?

  • You could also use a switch/case method and hardcode the words that correspond with the display number.

  • You could also use a big if/elseif/elsif format.

like image 24
Nanne Avatar answered May 05 '26 22:05

Nanne