如何在Java中打印字符串数组的偶数和奇数位置字符? [英] How to print even and odd position characters of an array of strings in Java?

查看:235
本文介绍了如何在Java中打印字符串数组的偶数和奇数位置字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

给出长度为N(从0到N-1索引)的字符串S,将它的偶数索引和奇数索引字符打印为一行上两个空格分隔的字符串. 假设输入从索引位置0(被认为是偶数)开始

Given a string S of length N, that is indexed from 0 to N-1, print it's even indexed and odd indexed characters as 2 space separated strings on a single line. Assume input starts at index position 0(which is considered even)

输入

第一行包含一个整数T(测试用例的数量). T个后续行中的每行i都包含一个字符串S.

The first line contains an integer, T (the number of test cases). Each line i of the T subsequent lines contain a String, S.

输出

对于每个字符串S,打印为偶数索引字符,后跟空格,后跟奇数索引字符.

样本输入

2

黑客

排名

示例输出

Hce akr

Hce akr

Rn ak

我编写的代码

public static void main(String[] args)
{
    Scanner scan    =   new Scanner(System.in);
    int T   =   scan.nextInt();
    scan.nextLine();

    for(int i=0 ; i<T ; i++)
    {
        String  myString    =   scan.nextLine();

        int evn =   0,
            odd =   0,
            len =   myString.length();

        char    strE[]  =   new char[50],
                strO[]  =   new char[50];

        for(int j=0 ; j<len ; j++)
        {
            if(j%2 == 0)
            {
                strE[evn]   =   myString.charAt(j);
                evn++;
            }
            if(j%2 == 1)
            {
                strO[odd]   =   myString.charAt(j);
                odd++;
            }
        }
        System.out.print(strE);
        System.out.print(" ");
        System.out.println(strO);
    }
}

我的输出

Hce akr

Hce akr

Rn ak

问题

如您所见,我的程序成功满足了测试用例和其他测试用例(使用自定义输入),但是每次HackerRank编译器告诉我我的程序都不满足测试用例时.

很明显,我的程序正在生成所需的输出,但是每次HackerRank编译器告诉我解决方案都是错误的.

有人可以告诉我我在哪里犯错吗?

其他修改

然后我决定将print语句的最后3行更改为一条语句,如下所示:

System.out.println(strE + " " + strO);

但是,这次程序没有产生所需的输出,而是打印了一些垃圾值,如下所示:

However, this time the program did not produce the desired output and rather printed some garbage values as follows:

[C @ 5c3f3b9b [C @ 3b626c6d

[C@5c3f3b9b [C@3b626c6d

[C @ 3abc8690 [C @ 2f267610

[C@3abc8690 [C@2f267610

我的疑问

1.在第一种情况下,当我使用2条打印语句分别打印两个字符串时,每次都获得正确的输出,但是HackerRank编译器拒绝了它. 为什么?

1. In the first case, when I was printing the two strings separately using 2 print statements, I was getting a correct output everytime but the HackerRank compiler rejected it. Why?

2.在第二种情况下,当我通过使用一个print语句而不是3来修改程序以获得所需的结果时,该程序给出了完全不同的输出,而是打印了垃圾值! 为什么?

2. In the second case, when I modified the program by using one print statement instead of 3 to get the desired result, the program gave a completely different output and rather printed garbage values! Why?

此处是指向HackerRank问题的链接,以获取更多信息: hackerrank.com/challenges/30-review-loop

非常感谢所有帮助和指导,并在此先感谢!

推荐答案

int T = scan.nextInt();

这将读取我们将要处理的大量测试用例.

This reads quantity of test cases, which we're going to process.

String string[] = new String[T];
for(int i = 0; i<T; i++){
  string[i] = scan.next();

} 接下来,我们将创建一个名为字符串"的数组(BTW,这是变量/对象的坏名),其大小为T,并且在for循环中从输入的T次读取测试用例并将其保存在数组中.

} Next we're creating an array named "string" (BTW, this a bad name for variables/objects) which has size T and in the for loop reading test cases from the input T times and saving them in the array.

for(int temp = 0; temp<T; temp++){

现在,对于每个测试用例,我们都执行以下操作...

Now, for each of test cases we do the following...

for(int j = 0; j<string[temp].length(); j = j+2)
{
    System.out.print(string[temp].charAt(j));
}

我们创建一个局部变量j,该变量仅在此for循环中可见. j包含我们正在处理的字符串的索引(= string [temp]).因此,我们要在位置j上打印一个字符(通过使用String类的标准方法"charAt",该方法返回给定字符串索引的字符),然后将其增加2.因此,此代码将打印每个偶数字符.对于字符串"example",它将显示"eape"(j = 0,j = 2,j = 4,j = 6).

We create a local variable j, which is visible only in this for loop. j holds index of the string (=string[temp]), which we're processing. So, we're printing a character on position j (by using standard method "charAt" of String class, which returns character of given index of the string) and then increasing it by 2. So, this code will print every even character. For string "example", it will print "eape" (j=0, j=2, j=4, j=6).

System.out.print(" ");

用空格分隔序列.

for(int j = 1; j<string[temp].length(); j = j+2){
    System.out.print(string[temp].charAt(j));
}

System.out.println();

我们正在做同样的事情(创建索引j,通过字符串的所有字符运行),但是从"1"开始,因此它将打印字符串的所有奇数字符.对于字符串"example",它将为您提供"xml"(j = 1,j = 3,j = 5).然后,它将结束字符串.希望对您有所帮助. :)

We're doing the same (creating index j, running though all characters of the string), but starting from "1", so it will print all odd characters of the string. For string "example", it will give you "xml" (j=1, j=3, j=5). and After this, it will end the string. I hope, it will help you to understand. :)

这篇关于如何在Java中打印字符串数组的偶数和奇数位置字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆