如何将用户输入存储到另一个类中的数组? || Java

huangapple go评论153阅读模式
英文:

How do I take user input and store it into an array that is in another class? || Java

问题

以下是翻译好的内容:

我正在尝试获取多个用户输入在这种情况下是字符串/名称),并将其存储到另一个类中的数组中
根据我所研究的内容许多建议使用列表而不是数组但在这种情况下指定要使用数组
我能够通过使用旨在接收整数输入的代码来分解问题我已经对其进行了更改以接收字符串然而在我输入例如要输入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;

    // 构造函数
    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;
    }

    // 设置器
    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;
    }

    // 获取器
    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
       Scanner input = new Scanner(System.in);

       System.out.println("有多少位演员主演这部电影?:");
       int num = input.nextInt();

       String array[] = new String[num];
       System.out.println("现在输入这" + num + "位演员的姓名:");

       for (int i = 0; i < array.length; i++){
           array[i] = input.nextLine();
       }
    }
}

我的问题是我不确定是否需要首先将值存储到本地数组“array”中,然后将该数组分配给类Movie的演员数组,还是直接将值分配给类Movie的演员数组。如果是这样,我不确定如何做到这一点。
我上面提到的另一个问题是,如果我要求存储3个名称,它只允许我输入2个。

英文:

I'm trying to take multiple user inputs (which will be strings in this case / names) and store it into an array that is in another class
Based on what I've researched many advise to use a list instead of an array, but in this case an array was specified to be used.
I was able to breakdown the problem by using code that was meant to receive an integer as input and I had made changes to receive a string instead, however, in the case where I entered for example, 3 names were to be entered and the code prompted me to enter the names, only 2 were allowed to be entered
Here's the class where the array was initialized

    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&lt;String, Integer&gt; ratings;
//Constructor
public Movie(String id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map&lt;String, Integer&gt; 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;
}

Here's the main file with the code for the person to enter the names

public class Main{
public static void main(String[] args) {
//Scanner initialization
Scanner input = new Scanner(System.in);
System.out.println(&quot;How many actors star in this movie?: &quot;);
int num = input.nextInt();
String array[] = new String[num];
System.out.println(&quot;Enter the &quot; + num + &quot; actors starred now&quot;);
for (int i = 0; i &lt; array.length; i++){
array[i] = input.nextLine();
}
}

My problem is I'm not sure if I need to first store the values into the local array "array" and THEN assign said array to the class Movie actors array, or if I need to directly assign the values into the class Movie actors array. If so, I'm unsure how to do that.
My other problem is the one mentioned above where if I entered 3 names to be stored, it only allows me to enter 2

答案1

得分: 0

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);
    }
}

Use Scanner::nextLine instead of Scanner::nextInt. The problem you are facing is because the new line character (Enter) after the int input is being assigned to array[0]. Check https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo for more information.

A sample run:

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]

<details>
<summary>英文:</summary>
&gt; My problem is I&#39;m not sure if I need to first store the values into
&gt; the local array &quot;array&quot; and THEN assign said array to the class Movie
&gt; actors array, or if I need to directly assign the values into the
&gt; class Movie actors array. If so, I&#39;m unsure how to do that.
Do it as follows:
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 &quot;Movie [actors=&quot; + Arrays.toString(actors) + &quot;, director=&quot; + director + &quot;, year=&quot; + year + &quot;]&quot;;
}
}
public class Main {
public static void main(String[] argv) throws Exception {
// Scanner initialization
Scanner input = new Scanner(System.in);
System.out.print(&quot;How many actors star in this movie?: &quot;);
int num = Integer.parseInt(input.nextLine());
String array[] = new String[num];
System.out.println(&quot;Enter the &quot; + num + &quot; actors starred now&quot;);
for (int i = 0; i &lt; array.length; i++) {
array[i] = input.nextLine();
}
System.out.print(&quot;Enter the name of the director:&quot;);
String director = input.nextLine();
System.out.print(&quot;Enter the release year:&quot;);
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);
}
}
&gt; My other problem is the one mentioned above where if I entered 3 names
&gt; to be stored, it only allows me to enter 2
Use `Scanner::nextLine` instead of `Scanner::nextInt`. The problem you are facing is because the new line character (Enter) after the `int` input is being assigned to `array[0]`. Check https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo for more information.
**A sample run:**
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]
</details>
# 答案2
**得分**: 0
你可以用多种方法解决这个问题,因为你声明了私有数组 actors,所以你需要使用 getter 和 setter 方法来设置和获取数据。你还缺少一个构造函数。
```java
public class Movie
{
private String[] actors;
public Movie(String[] actors)
{
this.actors = actors;
}
public String[] getActors()
{
return actors;
}
public void setActors(String[] actors)
{
this.actors = actors;
}
}

然后你可以简单地创建一个新的 Movies 对象并将数组放入其中,就像这样:

Movies movies = new Movies(array);
System.out.println(Arrays.toString(anime.getActors()));

以获取存储在你的数组中的所有演员。

英文:

You can approach this problem in many ways, since you declared the array actors private you will need to use a getter and setter method to set and get the data. You also are missing a constructor.

 public class Movie
{
private String[] actors;
public Movie(String[] actors)
{
this.actors = actors;
}
public String[] getActors()
{
return actors;
}
public void setActors(String[] actors)
{
this.actors = actors;
}
}

Then you can simply create a new object Movies and put in your array like this:

 Movies movies = new Movies(array);
System.out.println(Arrays.toString(anime.getActors()));

to get all the actors stored in your array.

huangapple
  • 本文由 发表于 2020年3月15日 06:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60687821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定