【问题标题】:How do I return string array and string to the array of multispell?如何将字符串数组和字符串返回到多重拼写数组?
【发布时间】:2020-07-14 00:14:49
【问题描述】:

使用下面的 main,创建将给出 String[] 结果的方法。 请在 main 中编写循环以打印多重拼写的数组。

public static void main(String[] args) {
// Create array here

// use your array to create new array here 
String[] multispell = Attack(“your array”, “ball”);

输出:

Cast Fire ball!!!
Cast Ice ball!!!
Cast Earth ball!!!
Cast Lightning ball!!!
Cast Wind ball!!

我的程序:

public class Paper {
    public static void main (String args[]) {

        String[] kind = new String[5];

        String [] multispell = Attack4 ( kind , "ball" );

        for (int i = 0 ; i< multispell.length ; i++) {

            System.out.println ("Cast " + multispell[i]+ "!!!");

    }   
    }

    public static String[] Attack4() {
        String [] kind111 = {"Fire", "Ice", "Earth", "Lightning", "Wind"};
        return kind111 ;

    }
}

【问题讨论】:

  • 您使用的是哪种编程语言,请添加为标签
  • java编程语言
  • 对不起,我不明白你在问什么
  • 第一部分是我的输出分配问题。你能帮我解决这个问题以获得输出吗?

标签: java arrays string methods return


【解决方案1】:

这是你要找的吗?

public static void main(String[] args) {

    String[] kind = new String[] {
        "Fire", "Ice", "Earth", "Lightning", "Wind"
    };

    String[] multispell = Attack(kind, "ball");
    for (int i = 0; i < multispell.length; i++) {
        System.out.println("Cast " + multispell[i] + "!!!");
    }

}

public static String[] Attack(String[] orig, String attach) {
    String[] result = new String[orig.length];
    for (int i = 0; i < orig.length; i++) {
        result[i] = orig[i] + " " + attach;
    }
    return result;
}

【讨论】:

    猜你喜欢
    • 2013-04-15
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多