import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int testCases = sc.nextInt();
        String[] bigNames = new String[testCases];

        while(testCases != 0)
        {
            int noOfNames = sc.nextInt();
            sc.nextLine();
            String[] names = new String[noOfNames];
            for(int i = 0; i<noOfNames; i++)
                names[i] = sc.nextLine();

            for(int i = 0; i<noOfNames-1;i++)
                if(names[i].length() > names[i+1].length())
                {
                    String temp = names[i];
                    names[i] = names[i+1];
                    names[i+1] = temp;
                }

            bigNames[testCases-1] = names[noOfNames-1];
            testCases--;
        }
        for(int i = testCases-1; i >= 0; i--)
            System.out.println(bigNames[i]);

    }
}


2
3
j
吉卜
甚高频
2
-gbhg
Bbb


程序在这里完成,无需最后打印循环
* / **

最佳答案

我不确定您要做什么,您应该告诉我们您希望这段代码应该做什么。
无论如何,我会尽力帮助您
while(testCases != 0)

将持续到testCases为0为止。

        for(int i = testCases-1; i >= 0; i--)
        System.out.println(bigNames[i]);


永远不会运行,因为在循环开始时我为-1。

您可以做的是:

    Scanner sc = new Scanner(System.in);

    int testCases = sc.nextInt();
    int j = testCases;
    String[] bigNames = new String[testCases];

    while(j != 0)
    {
        int noOfNames = sc.nextInt();
        sc.nextLine();
        String[] names = new String[noOfNames];
        for(int i = 0; i<noOfNames; i++)
            names[i] = sc.nextLine();

        for(int i = 0; i<noOfNames-1;i++)
            if(names[i].length() > names[i+1].length())
            {
                String temp = names[i];
                names[i] = names[i+1];
                names[i+1] = temp;
            }

        bigNames[j-1] = names[noOfNames-1];
        j--;
    }
    for(int i = testCases-1; i >= 0; i--)
        System.out.println(bigNames[i]);
}

关于java - 无法在屏幕上显示输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60761958/

10-10 22:45