L1-7 同一定名规范(java)

[复制链接]
发表于 2025-10-14 10:20:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
你地点的公司刚刚招收了几位步伐员,然而这些步伐员之前在差别的公司工作,以是他们风俗的变量定名规范大概存在差别,必要让他们都风俗公司要求的定名规范,然而如许大概会低落他们的工作服从。
你的上司找到了你,渴望你能编写同一定名规范的步伐,确保服从不低落的条件下,同一项目变量的定名规范。
现在已经知道公司中只会存在以下三种定名规范:

  • 驼峰(Camel),单词之间没有分隔符,第一个单词首字母小写,别的单词首字母大写,别的字母小写,比方 userName;
  • 帕斯卡(Pascal),单词之间没有分隔符,全部单词首字母大写,别的字母小写,比方 UserName ;
  • 下划线(Snake),单词之间用下划线 _ 分隔,全部字母小写,比方 user_name 。
输入格式:

第一行输入一个整数 N(1≤N≤100) 和一个字符串 T ,分别表现变量名的数目和末了同一的定名规范,此中定名规范只存在 Camel,Pascal,Snake 三种。
接下来 N 行每行输入一个只包罗巨细写字母和下划线的字符串 Si​(1≤∣Si​∣≤50) ,表现第 i 个变量名,定名方式为 Camel,Pascal,Snake 中的一种。
输特别式:

每一行输出一个字符串,表现第 i 个变量同一成定名规范 T 后的变量名。
输入样例:
  1. 4 Snake
  2. userName
  3. Password
  4. background_color
  5. text
复制代码
输出样例:
  1. user_name
  2. password
  3. background_color
  4. text
复制代码
代码长度限定
16 KB
时间限定
400 ms
内存限定
64 MB
栈限定
8192 KB
代码示例:
  1. import java.util.*;
  2. public class Main{
  3.     public static void main(String args[]){
  4.         Scanner reader = new Scanner(System.in);
  5.         int n = reader.nextInt();
  6.         String type = reader.next();
  7.         for(int i=0;i<n;i++){
  8.             String s = reader.next();
  9.             List<String> words = toWords(s);
  10.             System.out.println(toLines(words,type));
  11.         }
  12.     }
  13.     private static List<String> toWords(String s) {
  14.         List<String> words = new ArrayList<>();
  15.         if(s.contains("_")){
  16.             String[] split = s.split("_");
  17.             words.addAll(Arrays.asList(split));
  18.         } else {
  19.             if(!s.isEmpty()){
  20.                 int index = 0;
  21.                 for(int i=1; i<s.length(); i++){
  22.                     if(Character.isUpperCase(s.charAt(i))){
  23.                         words.add(s.substring(index,i));
  24.                         index = i;
  25.                     }
  26.                 }
  27.                 words.add(s.substring(index));
  28.             }
  29.         }
  30.         return words;
  31.     }
  32.     private static String toLines(List<String> words,String type) {
  33.         StringBuilder s = new StringBuilder();
  34.         switch (type) {
  35.             case "Snake" -> {
  36.                 for (int i = 0; i < words.size(); i++) {
  37.                     if (i >= 1) {
  38.                         s.append("_");
  39.                     }
  40.                     s.append(words.get(i).toLowerCase());
  41.                 }
  42.             } case "Camel" -> {
  43.                 s.append(words.get(0).toLowerCase());
  44.                 for (int i = 1; i < words.size(); i++) {
  45.                     s.append(Character.toUpperCase(words.get(i).charAt(0)));
  46.                     s.append(words.get(i).substring(1).toLowerCase());
  47.                 }
  48.             }
  49.             case "Pascal" -> {
  50.                 for (String word : words) {
  51.                     s.append(Character.toUpperCase(word.charAt(0)));
  52.                     s.append(word.substring(1).toLowerCase());
  53.                 }
  54.             }
  55.         }
  56.         return s.toString();
  57.     }
  58. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表