NowCoder每天要给许多客户写电子邮件。正如你所知,如果一封邮件中包含多个收件人,收件人姓名之间会用一个逗号和空格隔开;如果收件人姓名也包含空格或逗号,则姓名需要用双引号包含。
现在给你一组收件人姓名,请你帮他生成相应的收件人列表。
输入包含多组数据。
每组数据的第一行是一个整数n (1≤n≤128),表示后面有n个姓名。
紧接着n行,每一行包含一个收件人的姓名。姓名长度不超过16个字符。
对应每一组输入,输出一行收件人列表。
3 Joe Quan, William Letendre,Bruce 2 Leon Kewell
Joe, "Quan, William", "Letendre,Bruce" Leon, Kewell
import java.util.Scanner; /* * 收件人列表 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String strNum = sc.nextLine(); int num = Integer.parseInt(strNum); for (int i = 0; i < num; i++) { String str = sc.nextLine(); if (i == num - 1) { if (str.contains(",") || str.contains(" ")) { System.out.print("\"" + str + "\""); } else { System.out.print(str+" "); } } else { if (str.contains(",") || str.contains(" ")) { System.out.print("\"" + str + "\"" + ", "); } else { System.out.print(str + ", "); } } } System.out.println(); } } }
// write your code here import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNextLine()){ int n=sc.nextInt(); for(int i=0;i<n;i++){ String str=sc.nextLine(); if(str.contains(" ")||str.contains(",")){ if(i==n-1){ System.out.print("\""+str+"\""); } else{ System.out.print("\""+str+"\""); System.out.print(", "); } } else{ if(i==n-1){ System.out.print(str); } else{ System.out.print(str+", "); } } } System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int n = Integer.parseInt(sc.nextLine()); String result = ""; for (int i = 0; i < n; i++) { String cur = sc.nextLine(); if (cur.contains(",") || cur.contains(" ")) cur = "\"" + cur + "\""; if(i != n-1) { result += cur; result += ", "; }else result += cur; } System.out.println(result); } } }2. 用数组存储(注意输出格式)
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int n = Integer.parseInt(in.nextLine()); String[] arr = new String[n]; for (int i = 0; i < n; i++) { String cur = in.nextLine(); if (cur.contains(",") || cur.contains(" ")) cur = "\"" + cur + "\""; arr[i] = cur; } for (int i = 0; i < n; i++) { if(i == n-1) System.out.println(arr[i]); else System.out.print(arr[i] + ", "); } } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); in.nextLine();//换行操作 List<String> list = new ArrayList<>(); for(int i = 0;i < n;i++){ String arr = in.nextLine(); if(arr.contains(",") || arr.contains(" ")){ list.add("\""+arr+"\""); }else{ list.add(arr); } } for(int i = 0;i < n - 1;i++){ System.out.print(list.get(i) + ", "); } System.out.println(list.get(n - 1)); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); sc.nextLine(); for(int i = 0;i < n;i++){ String s = sc.nextLine(); if(s.contains(" ") || s.contains(",")){ System.out.print("\"" + s + "\""); }else{ System.out.print( s ); } if(i == n - 1){ System.out.println(); }else{ System.out.print( ", " ); } } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ // 不用nextInt是因为它和nextLine连用会出问题 int n = Integer.parseInt(sc.nextLine()); String[] s = new String[n]; for(int i = 0; i < n; i++){ s[i] = sc.nextLine(); if(s[i].contains(" ") || s[i].contains(",")){ s[i] = "\"" + s[i] + "\""; } } for(int i = 0; i < n; i++){ if(i == n-1){ System.out.println(s[i]); } else{ System.out.print(s[i]+", "); } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int n = Integer.parseInt(sc.nextLine()); String[] arr = new String[n]; for (int i = 0; i < n; i ++ ) { String s = sc.nextLine(); if(s.contains(",") || s.contains(" ")) s = "\"" + s + "\""; arr[i] = s; } for (int i = 0; i < n; i ++ ) { if(i == n - 1) System.out.println(arr[i]); else System.out.print(arr[i] + ", "); } } } }