2D数组,存储和调用

时间:2017-11-12 15:10:28

标签: java multidimensional-array

我想我可能会误解如何将我的信息存储到我的数组中然后调用它以供以后使用。 以下代码是询问用户他们有多少个Cd,并允许他们输入流派,艺术家和标题。然后,如果有多个cd,则返回循环。我相信我的一切都是正确的,但我的输出是不对的。每当输入2个Cd时,我将被允许输入CD 1中的前三个项目,但它将打印CD 2并直接进入程序结束。

- 我已经发布了代码以及输入1张CD和2张CD时发生的情况

import java.util.Scanner;

公共类RiverCD {

public static void main (String args[])
{
    Scanner scnr = new Scanner (System.in);
    int cdTotal = 0; // user controlled, controls the number of rows.
    int col = 3;    // constant that controls array col.
    String value = " "; // holds input for array.
    int intervals = 1;  

    String [][] list = new String [cdTotal][col];

    System.out.println("The River Catalog:\n");
    System.out.print("How many CD's do you have:  ");
    cdTotal = scnr.nextInt();

    do 
    {
        if (cdTotal <= 0)               // checking for bad data.
        {
            System.out.print("Must be a number greater than 0.\n");
            System.out.print("How many CD's do you have:  ");
            cdTotal = scnr.nextInt();
        }

    }while(cdTotal <= 0);               // set up to catch more bad data.

    System.out.print("\nFor each CD, enter the genre, then the artist, then the title.\n");

    for (int i = 1; i <= cdTotal; i++) // incrementing cd based on user input.
    {
        System.out.print("\nFor CD " + i + ":\n");

        while (intervals <= 3)// condition to exit while loop for next cd.
        {

            System.out.print("Enter:  "); // outside of for-loop to Enter three items.
            value = scnr.nextLine();
            scnr.next();                 // clearing buffer so as to to print a double Enter.

            for (int r = 0; r < list.length ; r++) // enters row according to how many cdTotal equals. 
            {   
                for (int c = 0; c < list[col].length; c++) // enters col in each row up to three.
                {
                    list[r][c] = value;// storing array in value for later use.
                }
            }

            intervals++;                // incrementing so as to break loop and print next cd statement.
        }
    }


    System.out.print("\nHere is your catalog:\n");
    System.out.printf("\n%-20s" + "%-20s" + "%-20s\n","Genre","Artist","Title");
    System.out.println();

    for (int r = 0; r < list.length; r++)// accessing list rows
    {
        for (int c = 0; c < list[r].length; c++)// accessing list col to pull out individual parts of data.
        {
            System.out.printf("%-20s","%-20s","%-20s",list[r][c],list[r][c],list[r][c]); // constant to check for data in array- 
                                                                                               //not working
            //System.out.printf("%-20s","%-20s","%-20s",list[1][0],list[1][1],list[1][2]);
        }
    }
    scnr.close(); // turning off scanner to allow no more input.
}

}

1CD- 河流目录:

你有多少张CD:1

对于每张CD,请输入流派,然后输入艺术家,然后输入标题。

对于CD 1: 输入:top 输入:cami 输入:havana

这是您的目录:

流派艺术家名称

2CD- 河流目录:

你有多少张CD:2

对于每张CD,请输入流派,然后输入艺术家,然后输入标题。

对于CD 1: 输入:top 输入:cami 输入:havana

对于CD 2:

这是您的目录:

流派艺术家名称

1 个答案:

答案 0 :(得分:0)

看起来这段代码是个问题:

        for (int r = 0; r < i ; r++) // enters row according to how many cdTotal equals. 
        {

            for (int c = 0; c <= col-1; c++) // enters col in each row up to three.
            {
                list[r][c] = value;  // storing array in value for later use.
            }
        }

如果value很大,此代码会将数组中的每个值设置为i。并且你没有在循环中更改value,因此数组中的所有内容都是完全相同的。