PTA 数组和字符串(Java) 您所在的位置:网站首页 java输入一串字符输出字符个数 PTA 数组和字符串(Java)

PTA 数组和字符串(Java)

2023-07-31 07:24| 来源: 网络整理| 查看: 265

文章目录 1. 字符串处理 (10 分)2.单词替换 (20 分)3.字符串处理 (10 分)4.通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 (10 分)5. jmu-Java-02基本语法-02-StringBuilder (10 分)6. 图书价格汇总 (20 分)7. jmu-Java-02基本语法-01-综合小测验 (20 分)

1. 字符串处理 (10 分)

编写一个程序,用户输入任意一个字符串,显示它的长度和第一个字符。

输入格式: 输入任意一个字符串。

输出格式: 显示它的长度和第一个字符,其间用,分隔。

输入样例: abc 4567

输出样例: 8,a

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String a= sc.nextLine(); int num= a.length(); System.out.println(num+","+a.charAt(0)); } } 2.单词替换 (20 分)

设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。

输入格式: 首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。

输出格式: 完成查找替换后的完整字符串

输入样例: 在这里给出一组输入。例如:

Although I am without you, I will always be ou you ou with

输出样例: 在这里给出相应的输出。例如:

Although I am without you, I will always be with you

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); String b = sc.nextLine(); String c = sc.nextLine(); String d = a.replace(" "+b+" "," "+c+" "); d = a.replace(" "+b," "+c); d = a.replace(b+" ",c+" "); System.out.println(d); } } 3.字符串处理 (10 分)

给定一个字符串。请去除串中的数字并反转。

输入格式: 原始串。

输出格式: 去除数字后的反转字符串。

输入样例: he11ll00o w0or8ld!

输出样例: !dlrow olleh

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); String b = a.replaceAll("\\d", ""); System.out.println(b = new StringBuffer(b).reverse().toString()); } } 4.通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 (10 分)

统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数

输入格式: 通过键盘输入一行字符(任意字符)

输出格式: 统计一行字符串中的中英文字母个数、空格个数、数字个数、其他字符个数

输入样例: rwrwewre2345asdJSJQI%^&(& *&sdf YY( 2342-k’

输出样例: 字母个数:22 数字个数:8 空格个数:5 其他字符个数:10

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); char [] b=a.toCharArray(); int l=0,d=0,z=0,o=0; for(int i=0;i l++; } else if(b[i]>'0'&&b[i] z++; } else{ o++; } } System.out.println("字母个数:"+l); System.out.println("数字个数:"+d); System.out.println("空格个数:"+z); System.out.println("其他字符个数:"+o); } } 5. jmu-Java-02基本语法-02-StringBuilder (10 分)

输入3个整数n、begin、end。 首先,使用如下代码:

for(int i=0;i Scanner sc=new Scanner((System.in)); while(sc.hasNext()){ int n=sc.nextInt(); int begin=sc.nextInt(); int end=sc.nextInt(); StringBuilder str=new StringBuilder(); for(int i=0;i public static void main(String[] args) { Scanner sc = new Scanner(System.in); int total = 0; String str; str = sc.nextLine(); boolean flag = false; int linshi = 0; for(int i=0;i System.out.println(); } else{ System.out.print(str.charAt(i)); } if(str.charAt(i) >= '0' && str.charAt(i) total += linshi; linshi = 0; } else{ total += linshi; linshi = 0; } } total += linshi; System.out.println(); System.out.println("总价格为"+total); sc.close(); } } class Book{ public String name; public int p; } 7. jmu-Java-02基本语法-01-综合小测验 (20 分)

运行程序后可以输入4个选项,分别为:fib,sort,search,getBirthDate

**fib:**根据输入n,打印斐波那契数列。比如输入:3,输出:1 1 2

sort:**输入一串数字,然后进行排序并输出,注意数组元素输出的格式为使用[ ]包括。提示:**可直接使用函数Arrays相关方法处理输出。

search:**如果找到返回所找到的位置,如果没找到,返回-1。提示:**可以先对数组排序,然后使用Arrays相关函数进行查找。

getBirthDate:输入n个身份证,然后把输入的n个身份号的年月日抽取出来,按年-月-日格式输出。

当输入不是这几个字符串(fib,sort,search,getBirthDate)的时候,显示exit并退出程序。

注意:在处理输入的时候,尽量全部使用Scanner的**nextLine()**方法接收输入,不要将nextLine()与其它next方法混用,否则可能会出现行尾回车换行未处理影响下次输入的情况。

参考:jdk文档的Arrays,String

输入格式: fib 3 sort -1 10 3 2 5 search -1 search 0 getBirthDate 1 330226196605054190 e

输出格式: 1 1 2 [-1, 2, 3, 5, 10] 0 -1 1966-05-05 exit

import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Scanner; public class Main { public static int count=0; public static void fib(int n){ int a=1; int b=1; if (n==1){ System.out.println("1"); return; }else if (n==2){ System.out.println("1 1"); return; }else if (n==0){ return; } else { System.out.print("1 1 "); } int tem; for (int i=2;i Scanner sc=new Scanner(System.in); int a[]=new int[1]; while (true){ String str=sc.nextLine(); if (str.equals("fib")){ count=0; int b=Integer.valueOf(sc.nextLine()); fib(b); }else if (str.equals("sort")){ String s[]=sc.nextLine().split(" "); a=new int[s.length]; for (int i=0;i int b=Integer.valueOf(sc.nextLine()); int flag=0; for (int i =0;i flag=1; System.out.println(i); break; } } if (flag==0){ System.out.println(-1); } }else if (str.equals("getBirthDate")){ int time=Integer.valueOf(sc.nextLine()); for (int i=0;i System.out.println("exit"); } } } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有