为什么没有任何输出显示?在这里,我尝试在交替位置显示两个字符串。

huangapple go评论76阅读模式
英文:

Why is there no output getting displayed? Here I am trying to display two strings in alternate positions

问题

在下面的代码中,我试图在单个字符数组中交替位置打印两个字符串,其中第二个字符串应以相反的顺序存储。
例如:str1="happy" str2="sadly" arr="hyalpdpays",其中两个字符串的大小应相同。

import java.util.*;

//*在这里进行Main类的声明*
class Main{
public static void main (String args[])
{
    char[] arr=new char[100];  
    int flen=0;
    Scanner sc=new Scanner(System.in);
    String str1=sc.nextLine();
    String str2=sc.nextLine();
    if(str1.length()==str2.length())
    {
        flen = str1.length()+str2.length();
        for(int i=0, j=flen, k=0; i<(flen/2) && j>=0 && k<flen; i=i+2 , j=j-2, k++)
        {
            arr[k]=str1.charAt(i);
            k=k+1;
            arr[k]=str2.charAt(i);
        }
    }
    for(int i=0; i<flen; i++)
    {
        System.out.println(arr[i]);
    }
}
}
英文:

In the below code I am trying to print two strings in a single char array in alternate positions, where second string should be stored in the reverse order.
For example: str1=&quot;happy&quot; str2= &quot;sadly&quot; arr=&quot;hyalpdpays&quot;, where both strings should be of the same size.

import java.util.*;

//*class Main declaration done here*
class Main{
public static void main (String args[])
{
    char[] arr=new char[100];  
    int flen=0;
    Scanner sc=new Scanner(System.in);
    String str1=sc.nextLine();
    String str2=sc.nextLine();
    if(str1.length()==str2.length())
    {
        flen = str1.length()+str2.length();
        for(int i=0, j=flen, k=0; i&lt;(flen/2) &amp;&amp; j&gt;=0 &amp;&amp; k&lt;flen; i=i+2 , j=j-2, k++)
        {
            arr[k]=str1.charAt(i);
            k=k+1;
            arr[k]=str2.charAt(i);
        }
    }
    for(int i=0; i&lt;flen; i++)
    {
        System.out.println(arr[i]);
    }
}
}

答案1

得分: 2

我对你的代码进行了一些更改,现在它在我的系统上能够正常工作。

if (str1.length() == str2.length()) {
    flen = str1.length() + str2.length();
    int lastIndex = (str2.length() - 1);
    for (int i = 0, j = lastIndex, k = 0; i < flen; j--, k++, i++) {
        arr[i] = str1.charAt(k);
        i++;
        arr[i] = str2.charAt(j);
    }
}
for (int i = 0; i < flen; i++) {
    System.out.print(arr[i]);
}

现在它会从字符串1的索引0开始,然后是字符串2的最后一个索引,依此类推。输出将会如你所述:"hyalpdpays"。

英文:

I did some changes to your code this works fine with me

if(str1.length()==str2.length())
{
    flen = str1.length()+str2.length();
    int lastIndex = (str2.length()-1);
    for(int i=0, j=lastIndex, k=0; i&lt;flen; j--, k++,i++)
    {
        arr[i]=str1.charAt(k);
        i++;
        arr[i]=str2.charAt(j);
     
    }
}
for(int i=0; i&lt;flen; i++)
{
    System.out.print(arr[i]);
}

so it would start in the index 0 of string 1 then the last index of string 2 and so on. the output will be as u mentioned "hyalpdpays".

答案2

得分: 1

以下是翻译好的部分:

public class Main {
    public static void main(String[] args) {
        String str1 = "happy";
        String str2 = "sadly";
        StringBuilder sb = new StringBuilder();
        if (str1.length() == str2.length()) {
            for (int i = 0; i < str1.length(); i++) {
                sb.append(str1.charAt(i));// Chars of str1 from beginning
                sb.append(str2.charAt(str2.length() - i - 1));// Chars of str2 from end
            }
        } else {
            System.out.println("Strings are of different lengths");
        }

        System.out.println(sb);
    }
}

Output:

hyalpdpays
英文:

You can do it simply as follows:

public class Main {
	public static void main(String[] args) {
		String str1 = &quot;happy&quot;;
		String str2 = &quot;sadly&quot;;
		StringBuilder sb = new StringBuilder();
		if (str1.length() == str2.length()) {
			for (int i = 0; i &lt; str1.length(); i++) {
				sb.append(str1.charAt(i));// Chars of str1 from beginning
				sb.append(str2.charAt(str2.length() - i - 1));// Chars of str2 from end
			}
		} else {
			System.out.println(&quot;Strings are of different lengths&quot;);
		}

		System.out.println(sb);
	}
}

Output:

hyalpdpays

huangapple
  • 本文由 发表于 2020年9月25日 03:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64053028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定