英文:
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="happy" str2= "sadly" arr="hyalpdpays"
, 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<(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]);
}
}
}
答案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<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]);
}
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 = "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论