我正在尝试构建一个程序,允许用户输入演员姓名和详细信息(年龄+地址)以及他们已经加入的2部电影。这些电影必须在主要方法中读入主阵列,但是每个演员的电影都必须复制到我演员类的指定数组中,以便单独存储演员电影。
我正在尝试在main方法的循环中读取值到我的数组中:
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
String code="";
System.out.println("How many actors would you like to enter?");
int amt = kbd.nextInt();
int noOfFilms = (amt*2);
Actor [] arrayOfActors = new Actor[amt];
//Array of ALL films, each actors films must be copied to seperate array in the actor class.
String [] allFilms = new String[noOfFilms];
kbd.nextLine();
int count = 1;
int i = 0;
do {
count++;
System.out.println("Enter the Details for actor "+(count-1)+"\n");
System.out.println("Enter actor name:"+"\n");
String name = kbd.nextLine();
System.out.println("Enter actor age:"+"\n");
int age = kbd.nextInt();
kbd.nextLine();
System.out.println("Enter actor address:"+"\n");
String address = kbd.nextLine();
//Read in the actors films
System.out.println("Enter film 1 for "+name+"\n");
String film1 = kbd.nextLine();
allFilms[i] = film1;
System.out.println("Enter film 2 for "+name+"\n");
String film2 = kbd.nextLine();
allFilms[i+1] = film2;
//Create an actor as array is full of references only.
arrayOfActors[i] = new Actor(name, address, age);
i++;
arrayOfActors[i-1].print();
} while (count <= amt);
System.out.println("This was in the films array: "+allFilms[1]);
}
}
显然,我现在的结构方式不会起作用,因为每次循环开始时,值只会被覆盖,而且存储的唯一细节将是最后输入的演员电影。
我很难尝试解决这个问题并阅读所有电影,然后需要深入复制到另一个电影中。 (在Actor类中)
这是一项大学任务,必须以这种方式完成。任何建议都会有很大的帮助。
答案 0 :(得分:0)
您可以在Actor
课程中添加数组字段。然后,您将修改Actor
类构造函数,以便包含此数组参数以进行初始化。
所以我会像这样在循环中处理这个部分:
String[] actorFilms = new String[2];
//Read in the actors films
System.out.println("Enter film 1 for "+name+"\n");
String film1 = kbd.nextLine();
allFilms[i] = film1; //not OK; read below
actorFilms[0] = film1;
System.out.println("Enter film 2 for "+name+"\n");
String film2 = kbd.nextLine();
allFilms[i+1] = film2; //not OK; read below
actorFilms[1] = film2;
//Create an actor as array is full of references only.
arrayOfActors[i] = new Actor(name, address, age, actorFilms);
我不确定您是否仍需要保留allFilms
数组,但如果需要,则必须根据count
值确定需要填充的索引。只需使用i
和i+1
,您就会覆盖数组中的相同位置。
其他一些评论:
- 我认为你没有正确使用count
;你用0初始化它,但你立即递增它(循环中的第一个语句);
- Java中的符号约定规定类名应该大写。
答案 1 :(得分:0)
我建议在集合中使用actor对象的集合。
集合actorDetails = new ArrayList&lt;&gt;();
DO { .... ...
...
actorDetails.add(actioObj)
}而(...)
最后使用actorDetails来检索信息
答案 2 :(得分:0)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
String code = "";
System.out.println("How many actors would you like to enter?");
int amt = kbd.nextInt();
int noOfFilms = (amt * 2);
Actor[] arrayOfActors = new Actor[amt];
String[] allFilms = new String[noOfFilms];
kbd.nextLine();
for (int count = 0; count < amt; count++) {
System.out.println("Enter the details for actor " + (count + 1));
System.out.prinln("Enter actor name:");
String name = kbd.nextLine();
System.out.println("Enter actor age:");
int age = kbd.nextInt();
kbd.nextLine();
System.out.println("Enter actor address:");
String address = kbd.nextLine();
System.out.println("Enter film 1 for " + name);
String film1 = kbd.nextLine();
allFilms[count * 2] = film1;
System.out.println("Enter film 2 for " + name );
String film2 = kbd.nextLine();
allFilms[(count * 2) + 1] = film2;
arrayOfActors[count] = new Actor(name, address, age);
arrayOfActors[count].print();
}
System.out.println("This was in the films array: " + allFilms[1]);
}
}
也许是这样的?我删除了 i 变量并仅使用 count 变量来跟踪数组索引。我假设你还没有开始使用Collections,否则你可以这样做。