Studio 不会在我测试时打印数组元素。 (Java)

Studio won't print array elements when I test. (Java)

我正在使用 eclipse,当我希望程序通过索引打印所有数组元素或仅打印单个数组元素时。当我 运行 程序时,它不会 return 错误甚至地址。

导入java.util.Scanner;

public class FavoriteListMaker {

public static void main(String[] args)
{

    Scanner inputDevice = new Scanner(System.in);
    System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
    int ListSize = inputDevice.nextInt();
    String [] TopXA = new String [ListSize + 1];
    int [] TopXB = new int[ListSize + 1];

    for (int x = 0; x < TopXA.length; ++ x)
    {
        System.out.println("Please enter an item to be organized on the list");
        String item = inputDevice.nextLine();
        TopXA[x] = item;
        System.out.println("You have " + (ListSize - x) + " items left to fill on the list.");
    }

    System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
    System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
    System.out.println("At the end the item with the most points wins.");

    for (int y = 0; y < TopXA.length; ++ y) 
        for (int z = 0; z < TopXA.length; ++ z)
        {
            String compareA = TopXA[y];
            String compareB = TopXA[z];

            System.out.println("Do you prefer "  + compareA + " or " + compareB + " .");
            System.out.println("If you prefer " + compareA + "Please press 1. If you prefer " + compareB + " please press 2.");
            int choice = inputDevice.nextInt();

            switch(choice)
            {
            case 1:
                TopXB[y] =+ 1;
                break;
            case 2:
                TopXB[z] =+ 2;
                break;
            default:
                System.out.print("I'm sorry but that is not a valid input.");
            }
        }

当我运行代码时它会打印出来。 "Do you prefer or ." 而不是数组元素。

免责声明 - 我没有 eclipse,我没有在 Java

中编程

请检查两件事 1) y 的 for 循环没有大括号 2) 请检查数组 'TopXA' 以查看 'item' 是否在您输入后填充到数组中。 3) CompareA 和 CompareB 是否被赋值?

编辑 - 发现问题

添加scanner.nextLine();在 nextINt 行之后。

您的问题是 nextInt() 方法没有使用换行符(参见 other question)。这导致列表中的第一个元素为空。在嵌套 for 循环的第一次迭代 (y=0, z=0) 中,这个空元素被打印两次。

尝试在每次 nextInt() 调用后添加 inputDevice.nextLine(),您会发现它正在运行;)

并且请:不要将变量命名为大写!

import java.util.Scanner;

public class FavoriteListMaker {

public static void main(String[] args)
{

    Scanner inputDevice = new Scanner(System.in);
    System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
    int listSize = inputDevice.nextInt();
    inputDevice.nextLine();
    String [] topXA = new String [listSize];
    int [] topXB = new int[listSize];

    for (int x = 0; x < topXA.length; ++x)
    {
        System.out.println("Please enter an item to be organized on the list");
        String item = inputDevice.nextLine();
        topXA[x] = item;
        System.out.println("You have " + (listSize - x) + " items left to fill on the list.");
    }

    System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
    System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
    System.out.println("At the end the item with the most points wins.");

    for (int y = 0; y < topXA.length; ++y) 
    {
        for (int z = 0; z < topXA.length; ++z)
        {
            String compareA = topXA[y];
            String compareB = topXA[z];

            System.out.println("Do you prefer "  + compareA + " or " + compareB + " .");
            System.out.println("If you prefer " + compareA + "Please press 1. If you prefer " + compareB + " please press 2.");
            int choice = inputDevice.nextInt();
            inputDevice.nextLine();

            switch(choice)
            {
            case 1:
                topXB[y] =+ 1;
                break;
            case 2:
                topXB[z] =+ 2;
                break;
            default:
                System.out.print("I'm sorry but that is not a valid input.");
            }
        }
    }
}
}