7-17 String的格式判断与内容提取 (10 分)
不错的题
| import java.util.Arrays; |
| import java.util.Scanner; |
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| |
| int l=str.length(); |
| int flag=0; |
| if(l%8!=0) { |
| System.out.println("Wrong Format"); |
| return; |
| } |
| for(int i=0;i<l;i+=8) { |
| String k=str.substring(i,i+8); |
| if(k.startsWith("202017")||k.startsWith("202061")) { |
| if(flag==0) |
| { |
| flag++; |
| System.out.print(k.substring(4,8)); |
| } |
| else System.out.print(" "+k.substring(4,8)); |
| } |
| } |
| |
| |
| sc.close(); |
| } |
| |
| } |
7-11 单词替换 (10 分)
代码不严谨,如果查找的字符后面有标点,就错误了。但是程序测试案例较少,可以通过
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| String k=sc.next(); |
| String a=sc.next(); |
| String[] strs=str.split(" "); |
| for(int i=0;i<strs.length;i++) { |
| if(strs[i].equals(k)) { |
| strs[i]=a; |
| } |
| } |
| for(int i=0;i<strs.length;i++) { |
| if(i==0)System.out.print(strs[i]); |
| else System.out.print(" "+strs[i]); |
| } |
| sc.close(); |
| } |
| |
| }import java.util.Scanner; |
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| String k=sc.next(); |
| String a=sc.next(); |
| String[] strs=str.split(" "); |
| for(int i=0;i<strs.length;i++) { |
| if(strs[i].equals(k)) { |
| strs[i]=a; |
| } |
| } |
| for(int i=0;i<strs.length;i++) { |
| if(i==0)System.out.print(strs[i]); |
| else System.out.print(" "+strs[i]); |
| } |
| sc.close(); |
| } |
| |
| }ava |
| import java.util.Scanner; |
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| String k=sc.next(); |
| String a=sc.next(); |
| String[] strs=str.split(" "); |
| for(int i=0;i<strs.length;i++) { |
| if(strs[i].equals(k)) { |
| strs[i]=a; |
| } |
| } |
| for(int i=0;i<strs.length;i++) { |
| if(i==0)System.out.print(strs[i]); |
| else System.out.print(" "+strs[i]); |
| } |
| sc.close(); |
| } |
| |
| } |
7-10 数组元素交换 (10 分)
只能先转化成数组在写,
因为
案例:2 9 0 10,
结果是10 9 2 0
| import java.util.Arrays; |
| import java.util.Scanner; |
| |
| public class Main |
| { |
| public static void main(String args[]) { |
| Scanner sc=new Scanner(System.in); |
| String s=sc.nextLine(); |
| String[] k=s.split(" "); |
| int[] ans=new int[k.length]; |
| int i=0; |
| for(i=0;i<k.length;i++) { |
| ans[i]=Integer.parseInt(k[i]); |
| } |
| |
| int maxid=0; |
| int miniid=0; |
| for(i=0;i<k.length;i++) { |
| System.out.println(ans[i]); |
| if(ans[i]>ans[maxid]) maxid=i; |
| else |
| if(ans[i]<ans[miniid]) miniid=i; |
| } |
| |
| |
| for(i=0;i<k.length;i++) { |
| if(i==0)System.out.println(ans[maxid]); |
| else if(i==k.length-1) System.out.println(ans[miniid]); |
| else if(i==miniid) System.out.println(ans[k.length-1]); |
| else if(i==maxid) System.out.println(ans[0]); |
| else System.out.println(ans[i]); |
| } |
| |
| |
| sc.close(); |
| |
| |
| } |
| } |
| |
7-1 sdut-String-3 字符串字母大小写转换逆序输出 (10 分)
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| String s=""; |
| for(int i=0;i<str.length();i++) { |
| char a=str.charAt(i); |
| if('a'<=a&&a<='z') { |
| a-=32; |
| } |
| else if('A'<=a&&a<='Z') { |
| a+=32; |
| } |
| s+=a; |
| } |
| StringBuffer stf=new StringBuffer(s); |
| System.out.println(stf.reverse()); |
| |
| sc.close(); |
| } |
| } |
| |
7-2 sdut-String-4 去除字符串中数字字符逆序输出 (10 分)
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| String[] t=str.split("\\d+"); |
| String s=""; |
| for(int i=0;i<t.length;i++) { |
| s+=t[i]; |
| } |
| StringBuffer stf=new StringBuffer(s); |
| System.out.println(stf.reverse()); |
| |
| sc.close(); |
| } |
| } |
| |
7-3 sdut-String-1 识蛟龙号载人深潜,立科技报国志(I) (10 分)
正确代码
| import java.util.Scanner; |
| |
| public class Main{ |
| public static void main(String []args) { |
| Scanner sc =new Scanner(System.in); |
| String s = null; |
| s=sc.nextLine(); |
| while(!s.equals("end")) { |
| int sum=0; |
| |
| for(int i=0;i<s.length();i++) { |
| char ch=s.charAt(i); |
| if(ch>='0'&&ch<='9') { |
| sum+=(ch-'0'); |
| } |
| } |
| System.out.println(sum); |
| s=sc.nextLine(); |
| } |
| sc.close(); |
| } |
| } |
错误代码
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| |
| Scanner sc = new Scanner(System.in); |
| String str=sc.nextLine(); |
| while(!str.equals("end")) { |
| |
| String[] t=str.split("\\D+"); |
| int ans=0; |
| for(int i=0;i<t.length;i++) { |
| System.out.println(t[i]); |
| |
| int cnt=Integer.parseInt(t[i]); |
| int cnt2=0; |
| while(cnt>0) { |
| int c=cnt%10; |
| cnt2+=c; |
| cnt/=10; |
| |
| } |
| ans+=cnt2; |
| |
| } |
| System.out.println(ans); |
| str=sc.nextLine(); |
| } |
| sc.close(); |
| } |
| } |
| |
7-6 sdut-String+array1(Collection-Map)-1 读中国载人航天史,汇航天员数量,向航天员致敬(字符串+数组、LinkedMap ) (10 分)
| import java.util.LinkedHashMap; |
| import java.util.Map; |
| import java.util.Map.Entry; |
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String []args) { |
| Scanner sc =new Scanner(System.in); |
| Map<String,Integer> map=new LinkedHashMap<String,Integer>(); |
| String str=sc.next(); |
| while(!str.equals("end")) { |
| if(!map.containsKey(str)) { |
| map.put(str, 1); |
| } |
| else { |
| map.put(str, map.get(str)+1); |
| } |
| |
| |
| str=sc.next(); |
| } |
| for(Entry<String, Integer> t :map.entrySet()) { |
| |
| System.out.println(t.getKey()+" "+t.getValue()); |
| } |
| sc.close(); |
| } |
| } |
| |
7-7 sdut-String-6 Java判断合法标识符 (10 分)
| import java.util.LinkedHashMap; |
| import java.util.Map; |
| import java.util.Map.Entry; |
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String []args) { |
| Scanner sc =new Scanner(System.in); |
| while(sc.hasNext()) { |
| boolean flag=true; |
| String str=sc.nextLine(); |
| for(int i=0;i<str.length();i++) { |
| char ch=str.charAt(i); |
| if(i==0) { |
| flag=Character.isJavaIdentifierStart(ch); |
| if(flag==false) { |
| System.out.println(flag); |
| break; |
| } |
| } |
| else { |
| flag=Character.isJavaIdentifierPart(ch); |
| if(flag==false) { |
| System.out.println(flag); |
| break; |
| } |
| } |
| } |
| if(flag==true) { |
| System.out.println(flag); |
| } |
| |
| } |
| sc.close(); |
| |
| } |
| } |
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15571154.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)