2011-12-17 76 views
3

好的,所以我在Java中學習方法,我不得不調用一個方法10次來顯示10個不同的單詞(我已經有for循環調用方法)。我只是不知道如何讓它有10個不同的單詞。這是我迄今爲止所擁有的。我討厭那麼多的求助,但我已經被困了一天了。多次調用方法

public static void tenWords(int display){ 

} 

public static void main(String[] args) { 

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

} 
+0

好了,所以,做調用一個方法十倍。 ..你卡在哪裏?你有*十個不同的單詞,所以給定一個特定的索引,你可以顯示正確的一個? – 2011-12-17 22:27:02

+0

如果你有一個包含10個不同單詞的數組...... – Alan 2011-12-17 22:28:18

+0

我想到了這一點,雖然我們還沒有涉及類中的數組,但不知道它是否會被允許 – Archey 2011-12-17 22:44:14

回答

1

沒有給出完整的答案,因爲這看起來像一個家庭作業//學習問題?

從希望不可取:

  • 你可以有字的數組或列表,數組或列表中返回「顯示」日 項目?

  • 您也可以使用開關/殼體方法,並將 與顯示編號對應的字硬編碼。

  • 你也可以使用大的if/elseif/elsif格式。

+0

謝謝,switch語句永遠不會跨越我的想法,array確實,但我們還沒有知道。 – Archey 2011-12-17 23:23:28

4

只是嘗試:

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

+0

打我+1 :) – fireshadow52 2011-12-17 22:32:37

+0

這看起來像作業,我不認爲只是發佈代碼沒有任何解釋是一個很好的答案。 – 2011-12-17 22:45:40

+1

這不是功課,我其實是整整一章。我真的很困惑,我不想等到週一去問老師。 – Archey 2011-12-17 22:51:27

0

可以使用任何的循環(個人喜好)的一再呼籲的主要方法,但我用if語句來調用主要方法。這裏是我的示例代碼: 以此爲reference..you會發現它很方便:

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


    } 

} 

} 

希望這對你的作品...... :)

+0

我只是一個初學者。讓我知道我是否以錯誤的方式解釋它。 – mike20132013 2013-08-04 18:37:35