文章目录
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<b.length;i++){
if(b[i]>='A'&&b[i]<='Z'||b[i]>='a'&&b[i]<='z'){
l++;
}
else if(b[i]>'0'&&b[i]<'9'){
d++;
}
else if(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<n;i++)
将从0到n-1的数字拼接为字符串str。如,n=12,则拼接出来的字符串为01234567891011
最后截取字符串str从begin到end(包括begin,但不包括end)之间的字符串,并输出。
输入样例:
10
5
8
1000
800
900
输出样例:
567
0330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
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<n;i++){
str.append(i);
}
System.out.println(str.substring(begin,end));
}
}
}
6. 图书价格汇总 (20 分)
图书价格汇总
输入格式:
假设某图书馆中图书记录的格式为“Java程序设计: 34;Web程序设计: 56;JSP程序设计:20”(每本书的价格是整数,每本书的价格与下一本书的名字之间有一个中文;)。
输出格式:
编写一个类的方法,能够接受键盘录入的符合上述格式的图书内容字符串,输出图书记录中所有书目的总价格。
输入样例:
Java程序设计:34 ;Web程序设计: 56;JSP程序设计:20
输出样例:
Java程序设计:34
Web程序设计: 56
JSP程序设计:20
总价格为110
import java.util.Scanner;
public class Main{
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<str.length();++i){
if(str.charAt(i) == ';'){
System.out.println();
}
else{
System.out.print(str.charAt(i));
}
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
linshi = linshi * 10 + str.charAt(i) - '0';
flag = true;
}
else if(i + 1<str.length()){
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<n-1;i++){
tem=a+b;
System.out.print(tem+" ");
a=b;
b=tem;
}
System.out.println(a+b);
return;
}
public static void main(String[] args) {
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<s.length;i++){
a[i]=Integer.valueOf(s[i]);
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}else if (str.equals("search")){
int b=Integer.valueOf(sc.nextLine());
int flag=0;
for (int i =0;i<a.length;i++){
if (a[i]==b){
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<time;i++){
String bir=sc.nextLine();
String bem=bir.substring(6,14);
System.out.println(bir.substring(6,10)+"-"+bir.substring(10,12)+"-"+bir.substring(12,14));
}
}else {
System.out.println("exit");
}
}
}
}