Java StringBuffer无法打印出撇号

时间:2017-02-24 09:55:22

标签: java string java.util.scanner symbols stringbuffer

我有一个任务。任务是,我需要编写一个代码,要求2行字符串。然后再做一个。

我的意思是:第一行:kusk - 第二行:loope - 问题:kluosokpe 但是,如果我尝试这样做:这不是我,你可以给我+钱吗?

问题只是错误消息。我现在可以用它做什么?

我的代码:     import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String first = sc.nextLine();

       String second = sc.nextLine();

      StringBuilder builderem = new StringBuilder(" ");
       for(int i = 0; i < first.length(); i++){
           builderem.append(first.charAt(i));
           builderem.append(second.charAt(i));
       }
       String base = builderem.toString();
       System.out.println(base);
    }

}

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

汤姆,

问题是一个字符串比另一个字符串长。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   String elso = sc.nextLine();

   String masodik = sc.nextLine();

  String longestString = elso; 
  String shortestString = masodik; 
  if (shortestString.length() > longestString.length()){
    shortestString = elso;
    longestString = masodik; 
  }
  StringBuilder builderem = new StringBuilder(" ");
   for(int i = 0; i < shortestString.length(); i++){
       builderem.append(shortestString.charAt(i));
       builderem.append(longestString.charAt(i));
   }
   for(int i = shortestString.length(); i < longestString.length(); i++){
       builderem.append(longestString.charAt(i));
   }
   String alapba = builderem.toString();
   System.out.println(alapba);
}
}

请告诉我它是否正常工作!