当前位置:网站首页>实验七 常用类的使用
实验七 常用类的使用
2022-07-06 09:22:00 【文文喜欢郭子吖】
实验七 常用类的使用
实验目的
1. 了解类库的概念及API的使用方法。
2. 掌握常用类的使用方法。
实验学时 2学时
实验内容
1. 输入两个字符串str1、str2,统计字符串str2出现在str1中的次数。
如:str1=”aaas lkaaas” ,str2=” as” ,则应输出 2
提示:文本输入
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in); //定义扫描键盘输入的对象
String s = sc.nextLine(); //从键盘读入一行文本
...
sc.close();
}
package code71;
import java.util.Scanner;
public class code71 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int sum=0;
System.out.print("请输入第一个字符串:");
String str1=in.nextLine(); //输入第一个字符串
System.out.print("请输入第二个字符串:");
String str2=in.nextLine(); //输入第二个字符串
int a=str1.indexOf(str2); //搜索str2在str1中出现的位置
while(a!=-1) {
sum++;
a=str1.indexOf(str2,a+1); //统计次数
}
System.out.println("字符串str2出现在str1中的次数为:"+sum+"次!");
}
}
2. 从键盘输入一串字符,输出有多少个不同的字符、每个字符出现的次数。
package code72;
import java.util.Scanner;
public class code72 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.nextLine();
StringBuffer b=new StringBuffer(str); //插入字符串缓冲区StringBuffer
int length=b.length(); //字符总数
System.out.println("总共有"+length+"个字母!");
String a; //声明单个字符
int index,sum;
for(int i=0;i<b.length();) {
sum=0;
a=b.substring(0,1); //截取单个字符
index=b.indexOf(a); //搜索单个字符在整个字符串中出现的位置
while(index!=-1) {
sum++;
b.deleteCharAt(index); //删除字符
index=b.indexOf(a,index); //统计次数
}
System.out.println(a+"字母有"+sum+"个!");
}
}
}
3. 编写一个 Java 程序,将用户输入的句子当中每一个单词的第一个字母大写。
package code73;
import java.util.Scanner;
public class code73 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("请输入一个句子:");
Scanner in = new Scanner(System.in);
String str = in.nextLine(); //输入原字符串
String str1 = ""; //最后输出的字符串
String[] a=str.split(" "); //将字符串按空格分割
for(int i=0;i<a.length;i++){ //遍历字符串,将每个字符串数组中的每个单词首字母替换为大写
a[i]=a[i].replace(a[i].charAt(0),a[i].toUpperCase().charAt(0));
str1+=a[i]+" "; //将替换后的字符串拼接到空串后
}
System.out.println("更改后的句子为:"+str1.trim());
}
}
4. 输入一个八进制数字串(在整型数据范围内),分别以2进制、10进制、16进制输出。
提示:Integer.parseInt("100",8); //将8进制100转换成十进制数
Integer.toBinaryString(100); //将十进制整数100转换成二进制数
package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入一个八进制数字串(在整型数据范围内):");
String a=in.nextLine(); //输入原字符串
int b=Integer.parseInt(a); //原字符串转换为数字
System.out.println("2进制为:"+Integer.toBinaryString(b)); //将b转换为2进制数
System.out.println("8进制为:"+Integer.toOctalString(b)); //将b转换为8进制数
System.out.println("16进制为:"+Integer.toHexString(b)); //将b转换为16进制数
}
5. 编写一段可生成一个随机字母的程序。
package code75;
import java.util.Arrays; //导入Arrays
public class code75 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//建立一个包含26个大小写字母的数组
char ch[]=new char[1]; { //一个字母
for(int i=0;i<1;i++)
{
int b;
b=(int)(Math.random()*(a.length)); //生成一个随机数
ch[i]=a[b]; //根据位置找到相应字母
}
System.out.println(Arrays.toString(ch)); //数字类型转换为字符串
}
}
}
6. 编程求100天以后是几月几号星期几。
package code76;
import java.time.LocalDate; //导入LocalDate
public class code76 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String[] weekday = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; //建立星期数组
LocalDate today = LocalDate.now(); //获取当前时间
System.out.println("今天是:" + today + "," + weekday[today.getDayOfWeek().getValue()-1]); //输出今天的格式化时间
LocalDate hundredDayLater = LocalDate.now().plusDays(100); //获取100天后的时间
System.out.println("100天后是:" + hundredDayLater + "," + weekday[hundredDayLater.getDayOfWeek().getValue()-1]); //输出100天后的格式化时间
}
}
实验小结
- Java系统中有很多预定义类,根据其功能不同划分成不同的包,所有包合称类库;
- 搜素指定字符或者字符串在另一个字符串中出现的位置,可以用indexOf()方法;
- 在字符串中截取子字符串时使用方法substring();
- StringBuffer类表示的是一个本身内容可变的字符串对象,包含一个缓冲区,主要用于完成字符串的动态添加、插入和替换等操作;
- 数字和字符串之间的转换,主要使用parseInt()方法和toString()方法;
- toBinaryString()、toHexString()、toOctalString()方法,可以分别将一个值以二进制、十六进制和八进制形式转化成字符串;
- Math类中的Random()方法可以用来获取随机数。
边栏推荐
- [面试时]——我如何讲清楚TCP实现可靠传输的机制
- Using qcommonstyle to draw custom form parts
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- 7-11 机工士姆斯塔迪奥(PTA程序设计)
- C language to achieve mine sweeping game (full version)
- Set container
- This time, thoroughly understand the MySQL index
- 更改VS主题及设置背景图片
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
- Why use redis
猜你喜欢
SRC挖掘思路及方法
MySQL事务及实现原理全面总结,再也不用担心面试
About the parental delegation mechanism and the process of class loading
3. Input and output functions (printf, scanf, getchar and putchar)
Wei Pai: the product is applauded, but why is the sales volume still frustrated
7. Relationship between array, pointer and array
Differences among fianl, finally, and finalize
编写程序,模拟现实生活中的交通信号灯。
Relationship between hashcode() and equals()
The latest tank battle 2022 - Notes on the whole development -2
随机推荐
Wechat applet
ArrayList的自动扩容机制实现原理
2. C language matrix multiplication
[graduation season · advanced technology Er] goodbye, my student days
The difference between overloading and rewriting
C language to achieve mine sweeping game (full version)
There is always one of the eight computer operations that you can't learn programming
2.初识C语言(2)
Mortal immortal cultivation pointer-1
Programme de jeu de cartes - confrontation homme - machine
This time, thoroughly understand the MySQL index
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
Redis cache obsolescence strategy
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
Cookie和Session的区别
记一次猫舍由外到内的渗透撞库操作提取-flag
String ABC = new string ("ABC"), how many objects are created
[modern Chinese history] Chapter V test
【九阳神功】2018复旦大学应用统计真题+解析
4. Binary search