在Java中处理数组中的索引值时出现问题

时间:2018-11-21 20:38:59

标签: java arrays for-loop

这段代码要求用户输入4首歌曲,然后询问他们各自获得了多少次下载,然后输出数组,以及下载次数最多的曲目和4个组合曲目的总下载量。

当操作arraay的第二个索引(总下载)时,会发生错误。同样,当输出下载最多的曲目时,它会循环播放,并且在输出超出for循环时会输出不同的结果。错误的一个例子是;

下载次数最多的曲目是“再见”

下载最多的曲目是“忘记了”

下载最多的曲目是“忘记了”

实际下载最多的曲目是“忘记了”。

它也不允许操纵totalDownloads:

如果曲目分别具有3000、2000、5000、8000次下载。

这将使总数变成3000200050008000。我理解这是因为im添加字符串值,但是当我尝试将它们转换为整数时会发生错误

import java.util.Scanner;
import java.util.Arrays;

public class arrays{

  public static void main(String args[]){

    String[][] array = new String[4][2];
    Scanner sc = new Scanner(System.in);
    Int totalDownloads;

    for (int i = 0; i < 4; i++) {

        System.out.println("What is track " + (i + 1));
        array[i][0] = sc.nextLine();
        System.out.println("How many thousands of times has it been downloaded? ");
        array[i][1] = Integer.parseInt(sc.nextInt() * 1000);
        totalDownloads = totalDownloads + array[i][1];

    }

    sc.close();

    String mostDownloads = "";

    for (int i = 1; i < 5; i++) {
        if(Integer.parseInt(array[1][1]) >= Integer.parseInt(array[i][1])){
           mostDownloads = array[1][0];
        } else if(Integer.parseInt(array[2][1]) >= Integer.parseInt(array[i][1])) {
              mostDownloads = array[2][0];
        } else if(Integer.parseInt(array[3][1]) >= Integer.parseInt(array[i][1])) {
              mostDownloads = array[3][0];
        } else if(Integer.parseInt(array[4][1]) >= Integer.parseInt(array[1][1])) {
              mostDownloads = array[4][0];



}

    System.out.println("The track downloaded the most is " +mostDownloads);
    System.out.println("The total number of downloads of these 4 tracks was " + totalDownloads + "000");
    System.out.println("The details of the downloads are");
    System.out.println(Arrays.toString(array[0]));
    System.out.println(Arrays.toString(array[1]));
    System.out.println(Arrays.toString(array[2]));
    System.out.println(Arrays.toString(array[3]));
}

}
}

1 个答案:

答案 0 :(得分:0)

您的array包含字符串,因此即使要输入的下载数字也必须由String.valueOf()转换为字符串才能插入。
不要将下载次数最多的歌曲标题保存下来,而是将索引保存在数组中。
下载次数最多的循环索引不能超过array.length
细节的最后打印可以通过另一个循环进行。
因此,请检查以下内容:

public static void main(String[] args) {
    String[][] array = new String[4][2];
    Scanner sc = new Scanner(System.in);
    int totalDownloads = 0;

    for (int i = 0; i < array.length; i++) {
        System.out.println("What is track " + (i + 1));
        array[i][0] = sc.nextLine();
        System.out.println("How many thousands of times has it been downloaded? ");
        int downloads = Integer.parseInt(sc.nextLine()) * 1000;
        array[i][1] = String.valueOf(downloads);
        totalDownloads += downloads;
    }

    sc.close();

    int mostDownloadsIndex = 0;

    for (int i = 1; i < array.length; i++) {
        if (Integer.parseInt(array[i][1]) > Integer.parseInt(array[mostDownloadsIndex][1]))
            mostDownloadsIndex = i;
    }

    System.out.println("The track downloaded the most is " + array[mostDownloadsIndex][0]);
    System.out.println("The total number of downloads of these 4 tracks was " + totalDownloads);
    System.out.println("The details of the downloads are");
    for (int i = 0; i < array.length; i++) {
        System.out.println(Arrays.toString(array[i]));
    }
}