Why the code prints the extra line in the beginning and also after accepting 2 string it takes 0 to 2 which is 3 iteration to print the output?

huangapple 未分类评论58阅读模式
英文:

Why the code prints the extra line in the beginning and also after accepting 2 string it takes 0 to 2 which is 3 iteration to print the output?

问题

  1. import java.io.*;
  2. import java.util.*;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. for (int i = 0; i <= n; i++) {
  8. String s1 = "", s2 = "";
  9. String str;
  10. str = sc.nextLine();
  11. int l = str.length();
  12. for (int k = 0; k < l; k++) {
  13. if (k % 2 == 0)
  14. s1 = s1 + str.charAt(k);
  15. else if (k % 2 != 0)
  16. s2 = s2 + str.charAt(k);
  17. }
  18. System.out.println(s1 + " " + s2);
  19. }
  20. }
  21. }
英文:

Why does this code print an extra line in the beginning, and also after accepting 2 strings it takes 0 to 2, which is 3 iterations to print the output?

Why the code prints the extra line in the beginning and also after accepting 2 string it takes 0 to 2 which is 3 iteration to print the output?

  1. import java.io.*;
  2. import java.util.*;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. int n=sc.nextInt();
  7. for(int i=0;i&lt;=n;i++)
  8. { String s1=&quot;&quot;,s2=&quot;&quot;;
  9. String str;
  10. str= sc.nextLine();
  11. int l=str.length();
  12. for (int k=0;k&lt;l;k++)
  13. {
  14. if (k%2==0)
  15. s1=s1+str.charAt(k);
  16. else if(k%2!=0)
  17. s2=s2+str.charAt(k);
  18. }
  19. System.out.println(s1+&quot; &quot;+s2);
  20. }
  21. }
  22. }

答案1

得分: 0

我稍微调整了你的解决方案。它应该可以工作。

  1. Scanner sc = new Scanner(System.in);
  2. int n = Integer.parseInt(sc.nextLine());
  3. for (int i = 0; i < n; i++) {
  4. String s1 = "", s2 = "";
  5. String str;
  6. str = sc.nextLine();
  7. int l = str.length();
  8. for (int k = 0; k < l; k++) {
  9. if (k % 2 == 0)
  10. s1 = s1 + str.charAt(k);
  11. else if (k % 2 != 0)
  12. s2 = s2 + str.charAt(k);
  13. }
  14. System.out.println(s1 + " " + s2);
  15. }
  16. sc.close();

编辑:
此外,你在迭代时多了一次(n+1)次。

英文:

I tweaked your solution a bit. It should work.

  1. Scanner sc=new Scanner(System.in);
  2. int n=Integer.parseInt(sc.nextLine());
  3. for(int i=0;i&lt;n;i++)
  4. { String s1=&quot;&quot;,s2=&quot;&quot;;
  5. String str;
  6. str= sc.nextLine();
  7. int l=str.length();
  8. for (int k=0;k&lt;l;k++)
  9. {
  10. if(k%2==0)
  11. s1=s1+str.charAt(k);
  12. else if(k%2!=0)
  13. s2=s2+str.charAt(k);
  14. }
  15. System.out.println(s1+&quot; &quot;+s2);
  16. }
  17. sc.close();

Edit:
Also, you were iterating (n+1) times

huangapple
  • 本文由 发表于 2020年7月26日 18:45:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/63099081.html
匿名

发表评论

匿名网友

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

确定