简体   繁体   English

多次调用方法

[英]Calling a method multiple times

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). 好吧,所以我在Java学习方法,我必须调用一个方法10次才能显示十个不同的单词(我已经有了for循环来调用方法)。 I just can't figure out how to get it to have 10 different words. 我只是无法弄清楚如何让它有10个不同的单词。 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);
    }

}

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

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. 您还可以使用大的if / elseif / elsif格式。

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. 您可以使用任何循环(您的首选项)一次又一次地调用main方法,但我使用if语句来调用main方法。 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... :) 希望这对你有用...... :)

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


        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM