Reputation: 1342
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);
}
}
Upvotes: 3
Views: 34726
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 5425
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... :)
Upvotes: 0
Reputation: 148
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
Upvotes: 4
Reputation: 64429
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.
Upvotes: 1