如何返回字符串数组?

时间:2017-11-16 11:23:34

标签: java arrays string

  1. 设计复制机模板:CopyMachine.java with

    • 公共静态变量int totalNoOfCopy,记录从所有复制机制作的副本总数

    • 带签名的方法: public String [] copy(String strText,int intNos),它将更新totalNoOfCopy值并返回一个String数组,其所有元素值为strText,数组大小为intNos。

  2. 设计程序UseCopyMachine.java:

    • 从CopyMachine.javab
    • 创建对象canon和fujistu
    • 复制6"飞行!"在佳能机和副本8"高!"在富士通机器上
    • 从2台复印机向用户显示所有副本的内容,并打印出2台复印机的总份数。
  3. 完全不熟悉java,这是我的进步,有些建议下一步该做什么?

    public class CopyMachine {
    
        static int totalNoOfCopy;
    
        public String[] copy(String strText, int intNos)
        {
            String[] ac= new String[intNos];
            totalNoOfCopy += intNos;
            return ac;
        }
    }
    
    public class UseCopyMachine {
        CopyMachine canon = new CopyMachine();
        CopyMachine fujitsu = new CopyMachine();
     }
    

1 个答案:

答案 0 :(得分:0)

根据我的理解,我认为你需要类似的东西,

您必须使用循环迭代数组以插入和检索元素。

public class CopyMachine {

    public static int totalNoOfCopy;

    public String[] copy(String strText, int intNos) {
        String[] ac= new String[intNos];
        for (int i=0; i< intNos; i++) {
            ac[i] = strText;
        }
        totalNoOfCopy += intNos;
        return ac;
    }

    public static void main(String[] args) {
        CopyMachine canon = new CopyMachine();
        String[] canonCopys = canon.copy("Flying!", 6);
        CopyMachine fujitsu = new CopyMachine();
        String[] fujitsuCopys = fujitsu.copy("High!", 8);

        System.out.println("Copies from Canon");
        for(String str: canonCopys) {
            System.out.println(str);
        }

        System.out.println("Copies from fujitsu");
        for(String str: fujitsuCopys) {
            System.out.println(str);
        }

        System.out.println("Total copies:" + CopyMachine.totalNoOfCopy);
    }
}

打印,

Copies from Canon
Flying!
Flying!
Flying!
Flying!
Flying!
Flying!
Copies from fujitsu
High!
High!
High!
High!
High!
High!
High!
High!
Total copies:14