3

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);
    }

}
3
  • Okay, so that does call a method ten times... where are you stuck? Do you have the ten different words, so that given a particular index you can display the right one? Commented Dec 17, 2011 at 22:27
  • What if you had an array containing ten different words... Commented Dec 17, 2011 at 22:28
  • I thought of that, though we haven't touched on arrays in class yet, not sure if it would be allowed Commented Dec 17, 2011 at 22:44

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This looks like homework, and I don't think just handing out the code without any explanation is a good answer.
It's not homework, I'm actually a whole chapter ahead . I'm really just baffled, and I don't want to wait until Monday to ask the teacher.
1

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.

1 Comment

Thanks, switch statement never cross my mind, array did, but we haven't learned that yet.
0

You can call the main method again and again by using any of the loops(your preference), but I used if statement to call the main method. Here's my sample code: Use this as a reference..you will find it handy:

import java.util.Scanner;

public class NewTest {

public static void main(String[] args) {
    Scanner src = new Scanner(System.in);
    System.out.println("Enter the Value:");
    int a = src.nextInt();
    char result;

    if (a >= 90) {
        result = 'A';
    } 

    else if (a >= 80) {
        result = 'B';

    } 
    else if (a >= 70) {
        result = 'C';
    } 
    else if (a >= 60) {
        result = 'D';
    }

    else {
        result = 'F';
    }

    if(result == 0){

        System.out.println("Do Nothing");
    }

    else{

        NewTest i = new NewTest();
        if(i!= null){

            System.out.println(result);
        }
                    //Here it goes to the main method again and runs it.
        i.main(args);


    }

}

}

Hope this works for you... :)

1 Comment

I am just a beginner. let me know if I am explaining it in a wrong way.
0

how about this

public class PepsiMaxIsBetterThanDietCoke{

  public static void main(String[] args){
  String [] Words =     { "horse", "Cow", "bullet", "jenifer", "maypole", "dumbbell", "dog", "playstaion"        , "xbox", "ciggerette"};



for (String x : Words)
  System.out.println(x);


        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.