设计复制机模板:CopyMachine.java with
公共静态变量int totalNoOfCopy,记录从所有复制机制作的副本总数
带签名的方法: public String [] copy(String strText,int intNos),它将更新totalNoOfCopy值并返回一个String数组,其所有元素值为strText,数组大小为intNos。
设计程序UseCopyMachine.java:
完全不熟悉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();
}
答案 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