【问题标题】:How to store multiple movie titles in an array in java using a for loopjava - 如何使用for循环将多个电影标题存储在java中的数组中
【发布时间】:2015-04-07 18:25:32
【问题描述】:

我目前正在为学校做一个项目,其中我必须提示多个电影标题,以及每部电影的副本数量并将它们存储在一个数组中。但是,似乎每次我要求一个新标题时,它都会覆盖上一个标题。我目前有两个文件rentalplace.javavideo.java(两者的内容都包含在底部)。目前我只提示输入 3 部电影。它看起来像这样:

视频标题?
狮子王
份数?
3
视频标题?
寻找 nemo
份数?
5
视频标题?
Monsters inc.
数量 副本?
1

当您输入这些答案时会发生什么是在最后一次迭代中 (我在 "Monsters inc." 中输入的位置)将打印数组中的所有空格。像这样:

Title ----------#copies------#Available

怪物 inc-----1----------------1

怪物 inc-----1----------------1

怪物 公司-----1----------------1

我需要找到一种方法来做到这一点,而不会覆盖数组中的先前值,以便第一个位置 [0] 将显示为 “狮子王”,第二个位置 [1]将显示为“海底总动员”

我的代码:rentalplace.java

package videolibrary;

import java.util.Scanner;

public class rentalplace {
    public static void printAll(int current, video[] varray) {
        System.out.println("Title" + "\t" + "Number of" + "\t" + "Number   Available");
        for (int i = 0; i <= varray.length; i++) {
            varray[i].print();
        }
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int current = 0, max = 10;
        video[] varray = new video[max];
        video v = new video();
        for (int j = 0; j < 3; j++) {
            v.addVideo();
            varray[j] = v;
            current++;
        }
        printAll(current, varray);
    }
}

video.java:

package videolibrary;

import java.util.Scanner;

public class video {
    private String title;
    private int NumberOf;
    private int NumAvailable;
    private Scanner keyboard = new Scanner(System.in);

    public void addVideo() {
        System.out.println("Title of video?");
        title = keyboard.nextLine();
        System.out.println("Number of copies?");
        NumberOf = keyboard.nextInt();
        String nothing = keyboard.nextLine();
        NumAvailable = NumberOf;
    }

    public void addNum(int NumToAdd) {
        NumberOf = NumberOf + NumToAdd;
        NumAvailable = NumAvailable + NumToAdd;
    }

    public void removeNum(int NumToRem) {
        NumberOf = NumberOf - NumToRem;
        NumAvailable = NumAvailable - NumToRem;
    }

    public void checkOut() {
        if (NumAvailable > 0)
            NumAvailable--;
        else
            System.out.println("Sorry " + title + " is not available at this time.");
    }

    public void print() {
        System.out.println(title + " \t " + NumberOf + " \t " + NumAvailable);

    }
}

【问题讨论】:

  • 另请阅读Java Naming Conventions。例如,您的类名应以大写字母开头:RentalplaceVideo,变量(如方法)应以小写字母开头,例如:numberOf i> 或 numAvailable.

标签: java arrays eclipse for-loop


【解决方案1】:

video v = new video(); 应该在循环内。否则,您将多次将相同的 video 实例添加到数组中。

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int current=0, max = 10;
    video[] varray = new video[max];
    for(int j = 0; j < 3; j++) {
        video v = new video();
        v.addVideo();
        varray[j]=v;
        current++;  
    }
    printAll(current, varray);  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2012-03-24
    • 1970-01-01
    相关资源
    最近更新 更多