我正在尝试接受多个用户输入(在这种情况下为字符串/名称),并将其存储到另一个类中的数组中。 根据我的研究,许多建议使用列表而不是数组,但是在这种情况下,指定使用数组。 通过使用本应接收整数作为输入的代码,我能够解决该问题,但是我已经进行了更改以接收字符串,但是,例如,在我输入的情况下,要输入3个名称,并且该代码提示我输入名称,只允许输入2个 这是数组初始化的类
public class Movie {
private String id;
private String name;
private String description;
private String[] genre;
private String[] actors;
private String[] language;
private String countryOfOrigin;
private Map<String, Integer> ratings;
//Constructor
public Movie(String id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings){
this.id = id;
this.name = name;
this.description = description;
this.genre = genre;
this.actors = actors;
this.language = language;
this.countryOfOrigin = countryOfOrigin;
this.ratings = ratings;
}
//setters
public void setid(String id){
this.id = id;
}
public void setname(String name){
this.name = name;
}
public void setDescription(String description){
this.description = description;
}
public void setGenre(String[] genre){
this.genre = genre;
}
public void setActors(String[] actors){
this.actors = actors;
}
//getters
public String getId(){
return id;
}
public String getName(){
return name;
}
public String getDescription(){
return description;
}
public String[] getGenre(){
return genre;
}
public String[] getActors(){
return actors;
}
这是带有供人输入姓名代码的主文件
public class Main{
public static void main(String[] args) {
//Scanner initialization
Scanner input = new Scanner(System.in);
System.out.println("How many actors star in this movie?: ");
int num = input.nextInt();
String array[] = new String[num];
System.out.println("Enter the " + num + " actors starred now");
for (int i = 0; i < array.length; i++){
array[i] = input.nextLine();
}
}
我的问题是我不确定是否需要先将值存储到本地数组“ array”中,然后将所述数组分配给类Movie actors数组,还是需要直接将值分配给类Movie actors数组。如果是这样,我不确定该怎么做。 我的另一个问题是上面提到的一个问题,如果我输入了3个要存储的名称,则只能输入2个
问题来源:Stack Overflow
我的问题是我不确定是否需要先将值存储到本地数组“ array”中,然后将所述数组分配给类Movie actors数组,还是需要直接将值分配给类Movie actors数组。如果是这样,我不确定该怎么做。
import java.util.Arrays;
import java.util.Scanner;
class Movie {
private String[] actors;
private String director;
private int year;
Movie() {
}
public Movie(String[] actors, String director, int year) {
this.actors = actors;
this.director = director;
this.year = year;
}
public String[] getActors() {
return actors;
}
public void setActors(String[] actors) {
this.actors = actors;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "Movie [actors=" + Arrays.toString(actors) + ", director=" + director + ", year=" + year + "]";
}
}
public class Main {
public static void main(String[] argv) throws Exception {
// Scanner initialization
Scanner input = new Scanner(System.in);
System.out.print("How many actors star in this movie?: ");
int num = Integer.parseInt(input.nextLine());
String array[] = new String[num];
System.out.println("Enter the " + num + " actors starred now");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextLine();
}
System.out.print("Enter the name of the director:");
String director = input.nextLine();
System.out.print("Enter the release year:");
int year = Integer.parseInt(input.nextLine());
Movie movie1 = new Movie(array, director, year);
// Alternatively
Movie movie2 = new Movie();
movie2.setActors(array);
movie2.setDirector(director);
movie2.setYear(year);
// Display
System.out.println(movie1);
}
}
我的另一个问题是上面提到的一个问题,如果我输入了3个要存储的名称,则只能输入2个
使用Scanner::nextLine代替Scanner::nextInt。您面临的问题是因为将int输入分配给后的换行符(Enter)array[0]。在使用next()或nextFoo()之后,检查扫描仪是否跳过nextLine()?了解更多信息。
运行示例:
How many actors star in this movie?: 2
Enter the 2 actors starred now
James
Bond
Enter the name of the director:Xyz
Enter the release year:2010
Movie [actors=[James, Bond], director=Xyz, year=2010]
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。